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-1.9.5 lib/alula/rpc_resource.rb
alula-ruby-1.9.4 lib/alula/rpc_resource.rb
alula-ruby-1.9.3 lib/alula/rpc_resource.rb
alula-ruby-1.9.2 lib/alula/rpc_resource.rb
alula-ruby-1.9.1 lib/alula/rpc_resource.rb
alula-ruby-1.9.0 lib/alula/rpc_resource.rb
alula-ruby-1.8.1 lib/alula/rpc_resource.rb
alula-ruby-1.8.0 lib/alula/rpc_resource.rb
alula-ruby-1.7.0 lib/alula/rpc_resource.rb
alula-ruby-1.6.0 lib/alula/rpc_resource.rb
alula-ruby-1.5.0 lib/alula/rpc_resource.rb
alula-ruby-1.4.0 lib/alula/rpc_resource.rb
alula-ruby-1.3.0 lib/alula/rpc_resource.rb
alula-ruby-1.2.4 lib/alula/rpc_resource.rb
alula-ruby-1.2.3 lib/alula/rpc_resource.rb
alula-ruby-1.2.2 lib/alula/rpc_resource.rb
alula-ruby-1.2.1 lib/alula/rpc_resource.rb
alula-ruby-1.2.0 lib/alula/rpc_resource.rb
alula-ruby-1.1.0 lib/alula/rpc_resource.rb
alula-ruby-1.0.2 lib/alula/rpc_resource.rb