Sha256: 3da7890e72452465221e2bb8bcabf2518c0adb2869a4b31677865e6105935eb6

Contents?: true

Size: 1.26 KB

Versions: 53

Compression:

Stored size: 1.26 KB

Contents

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

53 entries across 53 versions & 1 rubygems

Version Path
alula-ruby-0.62.0 lib/alula/rpc_resource.rb
alula-ruby-0.61.0 lib/alula/rpc_resource.rb
alula-ruby-0.60.0 lib/alula/rpc_resource.rb
alula-ruby-0.59.0 lib/alula/rpc_resource.rb
alula-ruby-0.58.0 lib/alula/rpc_resource.rb
alula-ruby-0.57.0 lib/alula/rpc_resource.rb
alula-ruby-0.56.0 lib/alula/rpc_resource.rb
alula-ruby-0.55.0 lib/alula/rpc_resource.rb
alula-ruby-0.54.0 lib/alula/rpc_resource.rb
alula-ruby-0.53.0 lib/alula/rpc_resource.rb
alula-ruby-0.52.0 lib/alula/rpc_resource.rb
alula-ruby-0.51.0 lib/alula/rpc_resource.rb
alula-ruby-0.50.1 lib/alula/rpc_resource.rb