Class: Alula::RpcResource

Inherits:
Object
  • Object
show all
Defined in:
lib/alula/rpc_resource.rb

Class Method Summary collapse

Class Method Details

.ok?(response) ⇒ Boolean

RPC endpoints tend to return a 200 OK even if the response failed. Examine the response body to determine if there was an error.

Returns:

  • (Boolean)


27
28
29
30
31
32
# File 'lib/alula/rpc_resource.rb', line 27

def self.ok?(response)
  error = response.data['error'] && !response.data['error'].empty?
  errors = response.data['errors'] && !response.data['errors'].empty?

  !(error || errors)
end

.request(http_method:, path:, payload:, handler:, wrap: true, opts: {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/alula/rpc_resource.rb', line 5

def self.request(http_method:, path:, payload:, handler:, wrap: true, opts: {})
  response = Alula::Client.request(http_method, path, wrap ? wrap_payload(payload) : payload, opts)

  unless response.ok? && ok?(response)
    error = Alula::AlulaError.for_response(response)

    #
    # Some error classifications are critical and should be raised for visibility
    # These errors do not contain meaningful data that an end-user can use to correct
    # the error.
    raise error if [Alula::RateLimitError, Alula::BadRequestError, Alula::ForbiddenError,
                    Alula::UnknownError, Alula::InsufficientScopeError].include?(error.class)

    return error
  end

  handler.new(response)
end

.wrap_payload(payload) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/alula/rpc_resource.rb', line 34

def self.wrap_payload(payload)
  {
    id: SecureRandom.uuid,
    jsonrpc: '2.0',
    params: payload
  }
end