Class: Infisical::HTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/infisical/http_client.rb

Overview

Thin wrapper around Net::HTTP that knows how to talk to the Infisical API: bearer token auth, JSON (de)serialization, and retrying transient failures with exponential backoff + jitter.

Constant Summary collapse

USER_AGENT =
"infisical-ruby-sdk/v#{VERSION}".freeze
DEFAULT_TIMEOUT =

seconds

10
DEFAULT_MAX_RETRIES =
4
DEFAULT_INITIAL_DELAY =

seconds

1.0
DEFAULT_BACKOFF_FACTOR =
2
JITTER_RATIO =

+/- 20%

0.2
RETRYABLE_EXCEPTIONS =
[
  Errno::ECONNRESET,
  Errno::ECONNREFUSED,
  Errno::ETIMEDOUT,
  Net::OpenTimeout,
  Net::ReadTimeout,
  SocketError,
  IOError
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES, initial_delay: DEFAULT_INITIAL_DELAY, backoff_factor: DEFAULT_BACKOFF_FACTOR, sleeper: ->(seconds) { sleep(seconds) }) ⇒ HTTPClient

Returns a new instance of HTTPClient.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/infisical/http_client.rb', line 36

def initialize(base_url:, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES,
               initial_delay: DEFAULT_INITIAL_DELAY, backoff_factor: DEFAULT_BACKOFF_FACTOR,
               sleeper: ->(seconds) { sleep(seconds) })
  @base_url = base_url.end_with?("/") ? base_url : "#{base_url}/"
  @timeout = timeout
  @max_retries = max_retries
  @initial_delay = initial_delay
  @backoff_factor = backoff_factor
  @sleeper = sleeper
  @access_token = nil
end

Instance Attribute Details

#access_token=(value) ⇒ Object (writeonly)



48
49
50
# File 'lib/infisical/http_client.rb', line 48

def access_token=(value)
  @access_token = value
end

Class Method Details

.backoff_delay(attempt, initial_delay: DEFAULT_INITIAL_DELAY, backoff_factor: DEFAULT_BACKOFF_FACTOR) ⇒ Object

Computes the (pre-jitter) backoff delay for a given retry attempt (0-indexed). Exposed for testing.



70
71
72
# File 'lib/infisical/http_client.rb', line 70

def self.backoff_delay(attempt, initial_delay: DEFAULT_INITIAL_DELAY, backoff_factor: DEFAULT_BACKOFF_FACTOR)
  initial_delay * (backoff_factor**attempt)
end

Instance Method Details

#delete(path, body: nil, params: {}) ⇒ Object



64
65
66
# File 'lib/infisical/http_client.rb', line 64

def delete(path, body: nil, params: {})
  request(:delete, path, body: body, params: params)
end

#get(path, params: {}) ⇒ Object



50
51
52
# File 'lib/infisical/http_client.rb', line 50

def get(path, params: {})
  request(:get, path, params: params)
end

#patch(path, body: nil, params: {}) ⇒ Object



60
61
62
# File 'lib/infisical/http_client.rb', line 60

def patch(path, body: nil, params: {})
  request(:patch, path, body: body, params: params)
end

#post(path, body: nil, params: {}, auth: true) ⇒ Object

auth: false sends the request without the stored access token, for endpoints like login where a stale bearer token must not be attached.



56
57
58
# File 'lib/infisical/http_client.rb', line 56

def post(path, body: nil, params: {}, auth: true)
  request(:post, path, body: body, params: params, auth: auth)
end