Sha256: eedca90014266cff8b466cfb3ddfbe56d7f0cd54f46bb5bc78cee79173265b18

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

require 'net/http'
module SolanaRpcRuby
  ##
  # ApiClient class serves as a client for solana JSON RPC API.
  # @see https://docs.solana.com/developing/clients/jsonrpc-api
  class ApiClient
    # Determines which cluster will be used to send requests.
    # @return [String]
    attr_accessor :cluster

    # Default headers.
    # @return [Hash]
    attr_accessor :default_headers

    # Initialize object with cluster address where requests will be sent.
    #
    # @param cluster [String]
    def initialize(cluster = nil)
      @cluster = cluster || SolanaRpcRuby.cluster

      message = 'Cluster is missing. Please provide default cluster in config or pass it to the client directly.'
      raise ArgumentError, message unless @cluster
    end

    # Sends request to the api.
    #
    # @param body [Hash]
    # @param http_method [Symbol]
    # @param params [Hash]
    #
    # @return [Object] Net::HTTPOK
    def call_api(body:, http_method:, params: {})
      uri = URI(@cluster)
      rpc_response = Net::HTTP.public_send(
        http_method,
        uri,
        body,
        default_headers,
      )

      rpc_response

    rescue Timeout::Error,
           Net::HTTPError,
           Net::HTTPNotFound,
           Net::HTTPClientException,
           Net::HTTPFatalError,
           Net::ReadTimeout => e
      fail ApiError.new(message: e.message)
    rescue StandardError => e

      message = "#{e.class} #{e.message}\n Backtrace: \n #{e.backtrace}"
      fail ApiError.new(message: message)
    end

    private

    def default_headers
      { "Content-Type" => "application/json" }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
solana_rpc_ruby-1.1.1 lib/solana_rpc_ruby/api_client.rb
solana_rpc_ruby-1.1.0 lib/solana_rpc_ruby/api_client.rb
solana_rpc_ruby-1.0.1 lib/solana_rpc_ruby/api_client.rb
solana_rpc_ruby-1.0.0 lib/solana_rpc_ruby/api_client.rb