Class: Webpipe::HttpClient Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Thin net/http wrapper: auth headers, retries with backoff, error mapping.

Constant Summary collapse

RETRYABLE_STATUSES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[429, 500, 502, 503, 504].freeze
RETRYABLE_ERRORS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[
  Net::OpenTimeout, Net::ReadTimeout, SocketError, SystemCallError
].freeze
STATUS_TO_ERROR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  400 => BadRequestError,
  401 => AuthenticationError,
  403 => ForbiddenError,
  404 => NotFoundError,
  429 => RateLimitError
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, api_key:, timeout:, max_retries:, user_agent:) ⇒ HttpClient

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of HttpClient.



24
25
26
27
28
29
30
# File 'lib/webpipe/http_client.rb', line 24

def initialize(base_url:, api_key:, timeout:, max_retries:, user_agent:)
  @base_uri = URI(base_url)
  @api_key = api_key
  @timeout = timeout
  @max_retries = max_retries
  @user_agent = user_agent
end

Instance Method Details

#delete(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



34
# File 'lib/webpipe/http_client.rb', line 34

def delete(path) = request("DELETE", path)

#get(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
# File 'lib/webpipe/http_client.rb', line 32

def get(path) = request("GET", path)

#post(path, body = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



33
# File 'lib/webpipe/http_client.rb', line 33

def post(path, body = nil) = request("POST", path, body)

#request(method, path, body = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/webpipe/http_client.rb', line 36

def request(method, path, body = nil)
  attempt = 0
  loop do
    response = perform(method, path, body)
    if RETRYABLE_STATUSES.include?(response.code.to_i) && attempt < @max_retries
      attempt += 1
      sleep backoff(response, attempt)
      next
    end
    return handle(response)
  rescue *RETRYABLE_ERRORS
    attempt += 1
    raise if attempt > @max_retries

    sleep(0.5 * (2**attempt))
    retry
  end
end