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 =
{
  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
DEFAULT_ERROR_MESSAGES =
{
  400 => 'Bad Request', 401 => 'Unauthorized', 403 => 'Forbidden',
  404 => 'Not Found', 409 => 'Conflict', 422 => 'Unprocessable Entity'
}.freeze
HTTP_NOT_MODIFIED =
304
NotModified =
Class.new
DEFAULT_TIMEOUT =

seconds

30
DEFAULT_OPEN_TIMEOUT =

seconds

10

Instance Method Summary collapse

Constructor Details

#initialize(api_url, auth_secret) ⇒ RpcClient



28
29
30
31
# File 'lib/forest_admin_datasource_rpc/Utils/rpc_client.rb', line 28

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

Instance Method Details

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

rubocop:disable Metrics/ParameterLists



34
35
36
37
38
39
40
# File 'lib/forest_admin_datasource_rpc/Utils/rpc_client.rb', line 34

def call_rpc(endpoint, caller: nil, method: :get, payload: nil, symbolize_keys: false, if_none_match: nil)
  resp = call_rpc_raw(endpoint, caller: caller, method: method, payload: payload,
                                symbolize_keys: symbolize_keys, if_none_match: if_none_match)
  return resp if resp == NotModified

  resp.body
end

#call_rpc_raw(endpoint, caller: nil, method: :get, payload: nil, symbolize_keys: false, if_none_match: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/forest_admin_datasource_rpc/Utils/rpc_client.rb', line 42

def call_rpc_raw(endpoint, caller: nil, method: :get, payload: nil, symbolize_keys: false, if_none_match: nil)
  response = make_request(endpoint, caller: caller, method: method, payload: payload,
                                    symbolize_keys: symbolize_keys, if_none_match: if_none_match)
  return NotModified if response.status == HTTP_NOT_MODIFIED

  raise_appropriate_error(response) unless response.success?

  response
end

#fetch_schema(endpoint, if_none_match: nil) ⇒ Object

rubocop:enable Metrics/ParameterLists



53
54
55
# File 'lib/forest_admin_datasource_rpc/Utils/rpc_client.rb', line 53

def fetch_schema(endpoint, if_none_match: nil)
  call_rpc(endpoint, method: :get, symbolize_keys: true, if_none_match: if_none_match)
end