Class: Lago::Api::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/lago/api/connection.rb

Constant Summary collapse

RESPONSE_SUCCESS_CODES =
[200, 201, 202, 204].freeze
DEFAULT_MAX_RETRIES =
3
INITIAL_BACKOFF =
1
BACKOFF_MULTIPLIER =
2
MAX_RETRY_DELAY =
20

Instance Method Summary collapse

Constructor Details

#initialize(api_key, uri, max_retries: DEFAULT_MAX_RETRIES, retry_on_rate_limit: true, on_rate_limit_info: nil) ⇒ Connection

Returns a new instance of Connection.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lago/api/connection.rb', line 14

def initialize(
  api_key,
  uri,
  max_retries: DEFAULT_MAX_RETRIES,
  retry_on_rate_limit: true,
  on_rate_limit_info: nil
)
  @api_key = api_key
  @uri = uri
  @max_retries = max_retries
  @retry_on_rate_limit = retry_on_rate_limit
  @on_rate_limit_info = on_rate_limit_info
end

Instance Method Details

#destroy(path = uri.path, identifier:, options: nil) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/lago/api/connection.rb', line 59

def destroy(path = uri.path, identifier:, options: nil)
  method = 'DELETE'
  uri_path = path
  uri_path += "/#{CGI.escapeURIComponent(identifier)}" if identifier
  uri_path += "?#{URI.encode_www_form(options)}" unless options.nil?
  execute_request(method:) do
    http_client.send_request(method, uri_path, prepare_payload(nil), headers)
  end
end

#get(path = uri.path, identifier:) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/lago/api/connection.rb', line 51

def get(path = uri.path, identifier:)
  method = 'GET'
  uri_path = identifier.nil? ? path : "#{path}/#{CGI.escapeURIComponent(identifier)}"
  execute_request(method:) do
    http_client.send_request(method, uri_path, prepare_payload(nil), headers)
  end
end

#get_all(options, path = uri.path) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/lago/api/connection.rb', line 69

def get_all(options, path = uri.path)
  method = 'GET'
  uri_path = options.empty? ? path : "#{path}?#{URI.encode_www_form(options)}"

  execute_request(method:) do
    http_client.send_request(method, uri_path, prepare_payload(nil), headers)
  end
end

#patch(path = uri.path, identifier:, body:) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/lago/api/connection.rb', line 43

def patch(path = uri.path, identifier:, body:)
  method = 'PATCH'
  uri_path = identifier.nil? ? path : "#{path}/#{CGI.escapeURIComponent(identifier)}"
  execute_request(method:) do
    http_client.send_request(method, uri_path, prepare_payload(body), headers)
  end
end

#post(body, path = uri.path) ⇒ Object



28
29
30
31
32
33
# File 'lib/lago/api/connection.rb', line 28

def post(body, path = uri.path)
  method = 'POST'
  execute_request(method:) do
    http_client.send_request(method, path, prepare_payload(body), headers)
  end
end

#put(path = uri.path, identifier:, body:) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/lago/api/connection.rb', line 35

def put(path = uri.path, identifier:, body:)
  method = 'PUT'
  uri_path = identifier.nil? ? path : "#{path}/#{CGI.escapeURIComponent(identifier)}"
  execute_request(method:) do
    http_client.send_request(method, uri_path, prepare_payload(body), headers)
  end
end