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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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

  if error.response[:status].zero? || error.response[:status] == 502
    raise BadGatewayError.new(
      'Failed to reach ForestAdmin server. Are you online?',
      details: { status: error.response[:status] },
      cause: error
    )
  end

  if error.response[:status] == 404
    raise NotFoundError.new(
      'ForestAdmin server failed to find the project related to the envSecret you configured. Can you check that you copied it properly in the Forest initialization?',
      details: { status: error.response[:status] }
    )
  end

  if error.response[:status] == 503
    raise ServiceUnavailableError.new(
      'Forest is in maintenance for a few minutes. We are upgrading your experience in the forest. We just need a few more minutes to get it right.',
      details: { status: error.response[:status] },
      cause: error
    )
  end

  raise InternalServerError.new(
    'An unexpected error occurred while contacting the ForestAdmin server. Please contact [email protected] for further investigations.',
    details: { status: error.response[:status], message: error.message },
    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