Class: GeneratorLabs::RequestHandler
- Inherits:
-
Object
- Object
- GeneratorLabs::RequestHandler
- 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
-
#delete(path) ⇒ Response
Make a DELETE request to the API.
-
#get(path, params = nil) ⇒ Response
Make a GET request to the API.
-
#initialize(account_sid, auth_token, config) ⇒ RequestHandler
constructor
Initialize request handler with authentication and retry logic.
-
#post(path, params = nil) ⇒ Response
Make a POST request to the API.
-
#put(path, params = nil) ⇒ Response
Make a PUT request to the API.
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
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(account_sid, auth_token, config) @account_sid = 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, account_sid, 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..timeout = config.timeout faraday_config..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.
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.
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.
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.
103 104 105 |
# File 'lib/generatorlabs/request_handler.rb', line 103 def put(path, params = nil) make_request(:put, path, params) end |