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.69.11 lib/alula/rpc_resource.rb
alula-ruby-1.0.1 lib/alula/rpc_resource.rb
alula-ruby-0.69.10 lib/alula/rpc_resource.rb
alula-ruby-1.0.0 lib/alula/rpc_resource.rb
alula-ruby-0.69.9 lib/alula/rpc_resource.rb
alula-ruby-0.69.8 lib/alula/rpc_resource.rb
alula-ruby-0.69.7 lib/alula/rpc_resource.rb
alula-ruby-0.69.6 lib/alula/rpc_resource.rb
alula-ruby-0.69.5 lib/alula/rpc_resource.rb
alula-ruby-0.69.4 lib/alula/rpc_resource.rb
alula-ruby-0.69.3 lib/alula/rpc_resource.rb
alula-ruby-0.69.2 lib/alula/rpc_resource.rb
alula-ruby-0.69.1 lib/alula/rpc_resource.rb
alula-ruby-0.69.0 lib/alula/rpc_resource.rb
alula-ruby-0.68.0 lib/alula/rpc_resource.rb
alula-ruby-0.67.0 lib/alula/rpc_resource.rb
alula-ruby-0.66.0 lib/alula/rpc_resource.rb
alula-ruby-0.65.0 lib/alula/rpc_resource.rb
alula-ruby-0.64.0 lib/alula/rpc_resource.rb
alula-ruby-0.63.0 lib/alula/rpc_resource.rb