Method: Hive::RPC::HttpClient#rpc_execute
- Defined in:
- lib/hive/rpc/http_client.rb
#rpc_execute(api_name = @api_name, api_method = nil, options = {}, &block) ⇒ Object
This is the main method used by API instances to actually fetch data from the remote node. It abstracts the api namespace, method name, and parameters so that the API instance can be decoupled from the protocol.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/hive/rpc/http_client.rb', line 59 def rpc_execute(api_name = @api_name, api_method = nil, = {}, &block) reset_timeout response = nil loop do = .dup request = http_post(api_name) request_object = if !!api_name && !!api_method put(api_name, api_method, ) elsif !! && defined?(.delete) .delete(:request_object) end if request_object.size > JSON_RPC_BATCH_SIZE_MAXIMUM raise JsonRpcBatchMaximumSizeExceededError, "Maximum json-rpc-batch is #{JSON_RPC_BATCH_SIZE_MAXIMUM} elements." end request.body = if request_object.class == Hash request_object elsif request_object.size == 1 request_object.first else request_object end.to_json response = catch :http_request do; begin; http_request(request) rescue *TIMEOUT_ERRORS => e retry_timeout(:http_request, e) and redo end; end if response.nil? retry_timeout(:tota_cera_pila, 'response was nil') and redo end case response.code when '200' response = catch :parse_json do; begin; JSON[response.body] rescue *TIMEOUT_ERRORS => e throw retry_timeout(:parse_json, e) end; end response = case response when Hash Hashie::Mash.new(response).tap do |r| evaluate_id(request: request_object.first, response: r, api_method: api_method) end when Array Hashie::Array.new(response).tap do |r| request_object.each_with_index do |req, index| evaluate_id(request: req, response: r[index], api_method: api_method) end end else; response end timeout_detected = false timeout_cause = nil [response].flatten.each_with_index do |r, i| if defined?(r.error) && !!r.error if !!r.error. begin rpc_method_name = "#{api_name}.#{api_method}" rpc_args = [request_object].flatten[i] raise_error_response rpc_method_name, rpc_args, r rescue *TIMEOUT_ERRORS => e timeout_detected = true timeout_cause = JSON[e.]['error'] + " while posting: #{rpc_args}" rescue e.to_s break # fail fast end else raise Hive::ArgumentError, r.error.inspect end end end if timeout_detected retry_timeout(:tota_cera_pila, timeout_cause) and redo end yield_response response, &block when '504' # Gateway Timeout retry_timeout(:tota_cera_pila, response.body) and redo when '502' # Bad Gateway retry_timeout(:tota_cera_pila, response.body) and redo else raise UnknownError, "#{api_name}.#{api_method}: #{response.body}" end break # success! end response end |