class SolanaRpcRuby::ApiClient

ApiClient class serves as a client for solana JSON RPC API. @see docs.solana.com/developing/clients/jsonrpc-api

Attributes

cluster[RW]

Determines which cluster will be used to send requests. @return [String]

default_headers[RW]

Default headers. @return [Hash]

Public Class Methods

new(cluster = nil) click to toggle source

Initialize object with cluster address where requests will be sent.

@param cluster [String]

# File lib/solana_rpc_ruby/api_client.rb, line 18
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

Public Instance Methods

call_api(body:, http_method:, params: {}) click to toggle source

Sends request to the api.

@param body [Hash] @param http_method [Symbol] @param params [Hash]

@return [Object] Net::HTTPOK

# File lib/solana_rpc_ruby/api_client.rb, line 32
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