Class: ERPC::HttpJsonRpcTransport
- Inherits:
-
Object
- Object
- ERPC::HttpJsonRpcTransport
- Defined in:
- lib/erpc_sdk/transport.rb
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#max_batch_size ⇒ Object
readonly
Returns the value of attribute max_batch_size.
Instance Method Summary collapse
- #batch(calls) ⇒ Object
-
#initialize(api_key:, endpoint:, headers:, timeout:, adapter:, max_batch_size: 256) ⇒ HttpJsonRpcTransport
constructor
A new instance of HttpJsonRpcTransport.
- #request(method, params = nil) ⇒ Object
Constructor Details
#initialize(api_key:, endpoint:, headers:, timeout:, adapter:, max_batch_size: 256) ⇒ HttpJsonRpcTransport
Returns a new instance of HttpJsonRpcTransport.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/erpc_sdk/transport.rb', line 62 def initialize(api_key:, endpoint:, headers:, timeout:, adapter:, max_batch_size: 256) @api_key = api_key @endpoint = endpoint @headers = headers @timeout = timeout @adapter = adapter @max_batch_size = max_batch_size @next_id = 0 @id_mutex = Mutex.new end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
60 61 62 |
# File 'lib/erpc_sdk/transport.rb', line 60 def endpoint @endpoint end |
#max_batch_size ⇒ Object (readonly)
Returns the value of attribute max_batch_size.
60 61 62 |
# File 'lib/erpc_sdk/transport.rb', line 60 def max_batch_size @max_batch_size end |
Instance Method Details
#batch(calls) ⇒ Object
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 |
# File 'lib/erpc_sdk/transport.rb', line 80 def batch(calls) return [] if calls.empty? if calls.length > max_batch_size raise InvalidResponseError, "A batch may contain at most #{max_batch_size} calls" end requests = calls.map do |call| method = call.fetch(:method) { call.fetch("method") } item = { "jsonrpc" => "2.0", "id" => next_id, "method" => method } if call.key?(:params) || call.key?("params") item["params"] = call.key?(:params) ? call[:params] : call["params"] end item rescue KeyError raise ConfigError, "batch calls require a method" end response = post(requests) unless response.is_a?(Array) raise_rpc(response["error"]) if response.is_a?(Hash) && response["error"].is_a?(Hash) raise InvalidResponseError, "ERPC returned a non-array batch response" end by_id = {} response.each do |item| unless item.is_a?(Hash) && valid_id?(item["id"]) raise InvalidResponseError, "ERPC returned an invalid batch item" end raise InvalidResponseError, "ERPC returned a duplicate batch id" if by_id.key?(item["id"]) by_id[item["id"]] = item end results = requests.map do |request| item = by_id.delete(request["id"]) raise InvalidResponseError, "ERPC omitted a batch response" unless item unwrap(item, request["id"]) end raise InvalidResponseError, "ERPC returned an unexpected batch response id" unless by_id.empty? results end |
#request(method, params = nil) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/erpc_sdk/transport.rb', line 73 def request(method, params = nil) request_id = next_id body = { "jsonrpc" => "2.0", "id" => request_id, "method" => method } body["params"] = params unless params.nil? unwrap(post(body), request_id) end |