Sha256: d03a62b4d6b29387aaeec04dede977a3be67d20815f61d16db80f78794b2dad9

Contents?: true

Size: 1.29 KB

Versions: 28

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Alula
  class RpcResource
    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

    ##
    # RPC endpoints tend to return a 200 OK even if the response failed. Examine
    # the response body to determine if there was an error.
    def self.ok?(response)
      error = response.data['error'] && !response.data['error'].empty?
      errors = response.data['errors'] && !response.data['errors'].empty?

      !(error || errors)
    end

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

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
alula-ruby-2.6.1 lib/alula/rpc_resource.rb
alula-ruby-2.6.0 lib/alula/rpc_resource.rb
alula-ruby-2.5.0 lib/alula/rpc_resource.rb
alula-ruby-2.4.0 lib/alula/rpc_resource.rb
alula-ruby-2.3.0 lib/alula/rpc_resource.rb
alula-ruby-2.2.0 lib/alula/rpc_resource.rb
alula-ruby-2.1.2 lib/alula/rpc_resource.rb
alula-ruby-2.1.1 lib/alula/rpc_resource.rb
alula-ruby-2.1.0 lib/alula/rpc_resource.rb
alula-ruby-2.0.0 lib/alula/rpc_resource.rb
alula-ruby-1.10.5 lib/alula/rpc_resource.rb
alula-ruby-1.10.4 lib/alula/rpc_resource.rb
alula-ruby-1.10.3 lib/alula/rpc_resource.rb
alula-ruby-1.10.2 lib/alula/rpc_resource.rb
alula-ruby-1.10.1 lib/alula/rpc_resource.rb
alula-ruby-1.10.0 lib/alula/rpc_resource.rb
alula-ruby-1.9.17 lib/alula/rpc_resource.rb
alula-ruby-1.9.16 lib/alula/rpc_resource.rb
alula-ruby-1.9.15 lib/alula/rpc_resource.rb
alula-ruby-1.9.14 lib/alula/rpc_resource.rb