Module: Arcade::Api::Primitives
- Included in:
- Arcade::Api
- Defined in:
- lib/arcade/api/primitives.rb
Instance Method Summary collapse
-
#begin_transaction(database) ⇒ Object
—————————— transaction ————————————————- #.
-
#commit(database, session_id = @session_id) ⇒ Object
—————————— commit ————————————————- #.
-
#get_data(command) ⇒ Object
—————————— get data ——————————————————– #.
-
#http ⇒ Object
—————————— http ———————————————————— # persistent http handle to the database.
-
#post_data(command, payload) ⇒ Object
—————————— post data ——————————————————– #.
-
#post_transaction(command, params, session_id = @session_id) ⇒ Object
—————————— post transaction ————————————————- #.
-
#rollback(database, session_id = @session_id, publish_error = true) ⇒ Object
—————————— rollback ————————————————- #.
Instance Method Details
#begin_transaction(database) ⇒ Object
—————————— transaction ————————————————- #
53 54 55 56 57 |
# File 'lib/arcade/api/primitives.rb', line 53 def begin_transaction database result = http.post Config.base_uri + "begin/#{database}" @session_id = result.headers["arcadedb-session-id"] # returns the session-id end |
#commit(database, session_id = @session_id) ⇒ Object
—————————— commit ————————————————- #
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/arcade/api/primitives.rb', line 72 def commit database, session_id = @session_id http_a = http.with( headers: { "arcadedb-session-id" => session_id } , debug_level: 1) response = http_a.post( Config.base_uri + "commit/#{database}" ) response.raise_for_status @session_id = nil response.status # returns 204 --> success # 403 --> incalid credentials # 500 --> Transaction not begun end |
#get_data(command) ⇒ Object
—————————— get data ——————————————————– #
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/arcade/api/primitives.rb', line 19 def get_data command response = http.get( Config.base_uri + command ) response.raise_for_status JSON.parse( response.body, symbolize_names: true )[:result] # alternative to `raise for status ` # case response = http.basic_auth(auth[:username], auth[:password]).get( Config.base_uri + command ) # in { status: 200..203, body: } # puts "success: #{JSON.parse(body, symbolize_names: true)[:result]}" # in { status: 400..499, body: } # puts "client error: #{body.json}" # in { status: 500.., body: } # puts "server error: #{body.to_s}" # in { error: error } # puts "error: #{error.message}" # else # raise "unexpected: #{response}" # end # puts "result : #{response}" # puts "code: #{response.status}" # analyse_result(response, command) end |
#http ⇒ Object
—————————— http ———————————————————— # persistent http handle to the database
10 11 12 13 14 15 16 |
# File 'lib/arcade/api/primitives.rb', line 10 def http break_on = -> (response) { response.status == 500 } @http ||= HTTPX.plugin(:basic_auth).basic_auth(auth[:username], auth[:password]) .plugin(:persistent) .plugin(:circuit_breaker) # .plugin(:circuit_breaker, circuit_breaker_break_on: break_on) end |
#post_data(command, payload) ⇒ Object
—————————— post data ——————————————————– #
43 44 45 46 47 48 49 |
# File 'lib/arcade/api/primitives.rb', line 43 def post_data command, payload # http = HTTPX.plugin(:basic_auth) # .basic_auth(auth[:username], auth[:password]) response = http.post( Config.base_uri + command, json: payload ) response.raise_for_status JSON.parse( response.body, symbolize_names: true )[:result] end |
#post_transaction(command, params, session_id = @session_id) ⇒ Object
—————————— post transaction ————————————————- #
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/arcade/api/primitives.rb', line 60 def post_transaction command, params, session_id= @session_id # http = HTTPX.plugin(:basic_auth) # .basic_auth(auth[:username], auth[:password]) # .with( headers: { "arcadedb-session-id"=>session }, debug_level: 1) http_a = http.with( headers: { "arcadedb-session-id" => session_id } , debug_level: 1) response = http_a.post( Config.base_uri + command, json: params ) response.raise_for_status JSON.parse( response.body, symbolize_names: true )[:result] end |
#rollback(database, session_id = @session_id, publish_error = true) ⇒ Object
—————————— rollback ————————————————- #
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/arcade/api/primitives.rb', line 84 def rollback database, session_id = @session_id, publish_error=true # http = HTTPX.plugin(:basic_auth) # .basic_auth(auth[:username], auth[:password]) # .with( headers: { "arcadedb-session-id"=>session_id }, debug_level: 1) http_a = http.with( headers: { "arcadedb-session-id" => session_id } , debug_level: 1) response = http_a.post( Config.base_uri + "rollback/#{database}" ) response.raise_for_status @session_id = nil logger.error "A Transaction has been rolled back" # if publish_error response.status end |