Class: ForestAdminDatasourceRpc::Utils::RpcClient

Inherits:
Object
  • Object
show all
Defined in:
lib/forest_admin_datasource_rpc/Utils/rpc_client.rb

Constant Summary collapse

ERROR_STATUS_MAP =

Map HTTP status codes to Forest Admin exception classes

{
  400 => ForestAdminAgent::Http::Exceptions::ValidationError,
  401 => ForestAdminAgent::Http::Exceptions::AuthenticationOpenIdClient,
  403 => ForestAdminAgent::Http::Exceptions::ForbiddenError,
  404 => ForestAdminAgent::Http::Exceptions::NotFoundError,
  409 => ForestAdminAgent::Http::Exceptions::ConflictError,
  422 => ForestAdminAgent::Http::Exceptions::UnprocessableError
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_url, auth_secret) ⇒ RpcClient

Returns a new instance of RpcClient.



31
32
33
34
# File 'lib/forest_admin_datasource_rpc/Utils/rpc_client.rb', line 31

def initialize(api_url, auth_secret)
  @api_url = api_url
  @auth_secret = auth_secret
end

Instance Method Details

#call_rpc(endpoint, method: :get, payload: nil, symbolize_keys: false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/forest_admin_datasource_rpc/Utils/rpc_client.rb', line 36

def call_rpc(endpoint, method: :get, payload: nil, symbolize_keys: false)
  client = Faraday.new(url: @api_url) do |faraday|
    faraday.request :json
    faraday.response :json, parser_options: { symbolize_names: symbolize_keys }
    faraday.adapter Faraday.default_adapter
    faraday.ssl.verify = !ForestAdminRpcAgent::Facades::Container.cache(:debug)
  end

  timestamp = Time.now.utc.iso8601(3)
  signature = generate_signature(timestamp)

  headers = {
    'Content-Type' => 'application/json',
    'X_TIMESTAMP' => timestamp,
    'X_SIGNATURE' => signature
  }

  response = client.send(method, endpoint, payload, headers)

  handle_response(response)
end