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) ⇒ Connection

Returns a new instance of Connection.



14
15
16
17
18
19
# File 'lib/lago/api/connection.rb', line 14

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

Instance Method Details

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



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

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

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



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

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

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



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/lago/api/connection.rb', line 82

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

  execute_request do
    http_client.send_request(
      'GET',
      uri_path,
      prepare_payload(nil),
      headers
    )
  end
end

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



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lago/api/connection.rb', line 44

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

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



21
22
23
24
25
26
27
28
29
30
# File 'lib/lago/api/connection.rb', line 21

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

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



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

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