Class: ForestAdminAgent::Http::ForestAdminApiRequester

Inherits:
Object
  • Object
show all
Includes:
Exceptions, ForestAdminDatasourceToolkit::Exceptions
Defined in:
lib/forest_admin_agent/http/forest_admin_api_requester.rb

Instance Method Summary collapse

Constructor Details

#initializeForestAdminApiRequester

Returns a new instance of ForestAdminApiRequester.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/forest_admin_agent/http/forest_admin_api_requester.rb', line 9

def initialize
  @headers = {
    'Content-Type' => 'application/json',
    'forest-secret-key' => Facades::Container.cache(:env_secret)
  }
  @client = Faraday.new(
    Facades::Container.cache(:forest_server_url),
    {
      headers: @headers,
      ssl: { verify: !Facades::Container.cache(:debug) }
    }
  )
end

Instance Method Details

#get(url, params = nil) ⇒ Object



23
24
25
# File 'lib/forest_admin_agent/http/forest_admin_api_requester.rb', line 23

def get(url, params = nil)
  @client.get(url, params)
end

#handle_response_error(error) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/forest_admin_agent/http/forest_admin_api_requester.rb', line 31

def handle_response_error(error)
  # Re-raise if it's already a BusinessError
  raise error if error.is_a?(ForestAdminAgent::Http::Exceptions::BusinessError)
  raise error if error.is_a?(ForestException)

  if error.response[:message]&.include?('certificate')
    raise InternalServerError.new(
      'ForestAdmin server TLS certificate cannot be verified. Please check that your system time is set properly.',
      details: { error: error.message },
      cause: error
    )
  end

  raise_for_status(error.response[:status], cause: error)
end

#post(url, params = nil) ⇒ Object



27
28
29
# File 'lib/forest_admin_agent/http/forest_admin_api_requester.rb', line 27

def post(url, params = nil)
  @client.post(url, params)
end

#raise_for_response!(response) ⇒ Object

Faraday does not raise on HTTP error statuses here (no raise_error middleware configured), so a response has to be checked explicitly to turn e.g. an invalid envSecret (404) into the same typed errors as handle_response_error.



50
51
52
53
54
# File 'lib/forest_admin_agent/http/forest_admin_api_requester.rb', line 50

def raise_for_response!(response)
  return if response.success?

  raise_for_status(response.status, message: response.reason_phrase)
end