lib/arcade/api/primitives.rb in arcadedb-0.4 vs lib/arcade/api/primitives.rb in arcadedb-0.5.0

- old
+ new

@@ -6,21 +6,29 @@ # # ------------------------------ http ------------------------------------------------------------ # # persistent http handle to the database def http - break_on = -> (response) { response.status == 500 } +# 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 # ------------------------------ get data -------------------------------------------------------- # def get_data command - response = http.get( Config.base_uri + command ) - response.raise_for_status + case response = http.get( Config.base_uri + command ) + in {status: 200..299} + # success + JSON.parse( response.body, symbolize_names: true )[:result] + in {status: 400..} + raise Arcade::QueryError.new **response.json( symbolize_names: true ) + else + # # http error + raise response + end 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: } @@ -39,59 +47,79 @@ # analyse_result(response, command) end # ------------------------------ post data -------------------------------------------------------- # 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] + case response = http.post( Config.base_uri + command, json: payload ) + in {status: 200..299} + # success + JSON.parse( response.body, symbolize_names: true )[:result] + in {status: 400..} + detail = response.json( symbolize_names: true )[:detail] + if detail =~ /Please retry the operation/ + logger.error "--------------------------------" + logger.error " ----> Operation repeated <---- " + logger.error detail + logger.error "The query --> #{payload.inspect}" + logger.error "--------------------------------" + sleep 1 + post_data command, payload + else + raise Arcade::QueryError.new **response.json( symbolize_names: true ) + end + else + # # http error + raise response + end + # response.raise_for_status end # ------------------------------ transaction ------------------------------------------------- # # + # The payload, optional as a JSON, accepts the following parameters: + # isolationLevel: READ_COMMITTED (default) or REPEATABLE_READ. (not implemented) + # def begin_transaction database result = http.post Config.base_uri + "begin/#{database}" - @session_id = result.headers["arcadedb-session-id"] # returns the session-id + result.headers["arcadedb-session-id"] end # ------------------------------ post transaction ------------------------------------------------- # - 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) + def post_transaction command, params, session_id: 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] - + case response = http_a.post( Config.base_uri + command, json: params ) + in {status: 200..299} + # success + JSON.parse( response.body, symbolize_names: true )[:result] + in {status: 400..} + ## debug +# puts "Command: #{command}" +# puts "params: #{params}" +# puts response.json( symbolize_names: true ) + raise Arcade::QueryError.new **response.json( symbolize_names: true ) + else + # # http error + raise response + end end # ------------------------------ commit ------------------------------------------------- # - def commit database, session_id = @session_id + def commit database, 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 + # 403 --> invalid credentials # 500 --> Transaction not begun end # ------------------------------ rollback ------------------------------------------------- # - 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) + def rollback database, session_id: , log: true 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 + logger.info "A Transaction has been rolled back" if log + response.status # returns 500 ! end end end end