Class: Finlight::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/finlight/api_client.rb

Overview

Performs authenticated REST requests with retry and exponential backoff (500ms * 2^(attempt-1)), mirroring the sibling clients: retries on 429 and transient 5xx.

Constant Summary collapse

RETRYABLE_STATUSES =
[429, 500, 502, 503, 504].freeze
BASE_RETRY_DELAY =
0.5

Instance Method Summary collapse

Constructor Details

#initialize(config, logger: Logging.default) ⇒ ApiClient

Returns a new instance of ApiClient.



15
16
17
18
# File 'lib/finlight/api_client.rb', line 15

def initialize(config, logger: Logging.default)
  @config = config
  @logger = logger
end

Instance Method Details

#get(path, query = {}) ⇒ Object

Returns the decoded JSON response.

Returns:

  • (Object)

    the decoded JSON response



21
22
23
24
25
# File 'lib/finlight/api_client.rb', line 21

def get(path, query = {})
  uri = URI.parse(@config.base_url + path)
  uri.query = URI.encode_www_form(query) unless query.empty?
  perform(uri, Net::HTTP::Get.new(uri))
end

#post(path, body) ⇒ Object

Returns the decoded JSON response.

Returns:

  • (Object)

    the decoded JSON response



28
29
30
31
32
33
34
# File 'lib/finlight/api_client.rb', line 28

def post(path, body)
  uri = URI.parse(@config.base_url + path)
  request = Net::HTTP::Post.new(uri)
  request["Content-Type"] = "application/json"
  request.body = JSON.generate(body)
  perform(uri, request)
end