Class: GeneratorLabs::RequestHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/generatorlabs/request_handler.rb

Overview

Handles HTTP requests to the Generator Labs API.

This class manages HTTP client configuration, authentication, retry logic, and error handling for all API requests. It automatically retries failed requests using exponential backoff on connection errors, 5xx server errors, and 429 rate limit errors.

Instance Method Summary collapse

Constructor Details

#initialize(account_sid, auth_token, config) ⇒ RequestHandler

Initialize request handler with authentication and retry logic.

This creates a Faraday connection with:

  • Automatic retries on connection errors, 5xx errors, and 429 rate limits

  • Exponential backoff based on config.retry_backoff

  • Configurable timeouts from config.timeout and config.connect_timeout

  • HTTP Basic Authentication using account_sid and auth_token

Parameters:

  • account_sid (String)

    Account SID for authentication

  • auth_token (String)

    Auth token for authentication

  • config (Config)

    Configuration object with timeouts and retry settings



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/generatorlabs/request_handler.rb', line 35

def initialize(, auth_token, config)
  @account_sid = 
  @auth_token = auth_token
  @config = config

  # Create Faraday connection with retry middleware
  @connection = Faraday.new(url: config.base_url) do |faraday_config|
    faraday_config.request :url_encoded
    faraday_config.request :authorization, :basic, , auth_token
    # Retry with exponential backoff; faraday-retry respects Retry-After
    # headers automatically on rate limit (429) responses
    faraday_config.request :retry,
                           max: config.max_retries,
                           interval: 1.0,
                           backoff_factor: config.retry_backoff,
                           retry_statuses: [429, 500, 502, 503, 504],
                           methods: %i[get post put delete]

    faraday_config.adapter Faraday.default_adapter
    faraday_config.options.timeout = config.timeout
    faraday_config.options.open_timeout = config.connect_timeout
  end
end

Instance Method Details

#delete(path) ⇒ Response

Make a DELETE request to the API.

No parameters are sent with DELETE requests. The request includes automatic retry logic for failures.

Examples:

handler.delete('rbl/hosts/HT1a2b3c4d5e6f7890abcdef1234567890')

Parameters:

  • path (String)

    API endpoint path (e.g., ‘rbl/hosts/HTxxxxxxxx’)

Returns:

  • (Response)

    Response wrapper with parsed data and rate limit info

Raises:

  • (Error)

    if request fails after all retries



118
119
120
# File 'lib/generatorlabs/request_handler.rb', line 118

def delete(path)
  make_request(:delete, path, nil)
end

#get(path, params = nil) ⇒ Response

Make a GET request to the API.

Parameters are sent as query string parameters. The request includes automatic retry logic for failures.

Examples:

handler.get('rbl/hosts', { status: 'active' })

Parameters:

  • path (String)

    API endpoint path (e.g., ‘rbl/hosts’)

  • params (Hash, nil) (defaults to: nil)

    Query parameters (e.g., ‘active’)

Returns:

  • (Response)

    Response wrapper with parsed data and rate limit info

Raises:

  • (Error)

    if request fails after all retries



71
72
73
# File 'lib/generatorlabs/request_handler.rb', line 71

def get(path, params = nil)
  make_request(:get, path, params)
end

#post(path, params = nil) ⇒ Response

Make a POST request to the API.

Parameters are sent as application/x-www-form-urlencoded data. The request includes automatic retry logic for failures.

Examples:

handler.post('rbl/hosts', { name: 'My Host', host: '1.2.3.4', type: 'rbl' })

Parameters:

  • path (String)

    API endpoint path (e.g., ‘rbl/hosts’)

  • params (Hash, nil) (defaults to: nil)

    Request parameters (e.g., ‘My Host’, host: ‘1.2.3.4’)

Returns:

  • (Response)

    Response wrapper with parsed data and rate limit info

Raises:

  • (Error)

    if request fails after all retries



87
88
89
# File 'lib/generatorlabs/request_handler.rb', line 87

def post(path, params = nil)
  make_request(:post, path, params)
end

#put(path, params = nil) ⇒ Response

Make a PUT request to the API.

Parameters are sent as application/x-www-form-urlencoded data. The request includes automatic retry logic for failures.

Examples:

handler.put('rbl/hosts/HT1a2b3c4d5e6f7890abcdef1234567890', { name: 'Updated Name' })

Parameters:

  • path (String)

    API endpoint path (e.g., ‘rbl/hosts/HTxxxxxxxx’)

  • params (Hash, nil) (defaults to: nil)

    Request parameters (e.g., ‘Updated Name’)

Returns:

  • (Response)

    Response wrapper with parsed data and rate limit info

Raises:

  • (Error)

    if request fails after all retries



103
104
105
# File 'lib/generatorlabs/request_handler.rb', line 103

def put(path, params = nil)
  make_request(:put, path, params)
end