Module: SimpleAuthSdk::Mixins::HTTPProxy
- Included in:
- API::AuthenticationEndpoints, SimpleAuthSdk::Mixins
- Defined in:
- lib/simple_auth_sdk/mixins/httpproxy.rb
Overview
HTTPProxy module provides methods for making HTTP requests using the rest-client gem. It includes functionality for retrying requests, encoding URIs, and handling various HTTP methods. You can integrate this module into your classes to easily make HTTP requests with retry logic.
Constant Summary collapse
- DEFAULT_RETRIES =
3
- MAX_ALLOWED_RETRIES =
10
- MAX_REQUEST_RETRY_JITTER =
250
- MAX_REQUEST_RETRY_DELAY =
1000
- MIN_REQUEST_RETRY_DELAY =
250
- BASE_DELAY =
100
Instance Attribute Summary collapse
- #base_uri ⇒ Object
-
#headers ⇒ Hash
The headers to be sent with each request.
-
#retry_count ⇒ Integer
The number of retries for failed requests.
-
#timeout ⇒ Integer
The timeout in seconds for HTTP requests.
Instance Method Summary collapse
-
#add_headers(h = {}) ⇒ void
Merges additional headers into the existing headers hash.
-
#call(method, url, timeout, headers, body = nil) ⇒ RestClient::Response
Executes an HTTP request using rest-client.
-
#encode_uri(uri) ⇒ String
Encodes the URI based on whether a base_uri is set.
-
#method ⇒ Hash, String
Defines HTTP methods that will be proxied to HTTP class methods.
-
#request(method, uri, body = {}, extra_headers = {}) ⇒ Hash, String
Makes an HTTP request.
-
#request_with_retry(method, uri, body = {}, extra_headers = {}) ⇒ Hash, String
Makes an HTTP request with retry logic.
-
#retry_options ⇒ Hash
Returns the options for retrying requests, including the number of retries and the sleep timer.
-
#safe_parse_json(body) ⇒ Hash, String
Safely parses the JSON response body.
-
#url(path) ⇒ String
Constructs the full URL by appending the path to the base URI.
Instance Attribute Details
#base_uri ⇒ Object
200 201 202 |
# File 'lib/simple_auth_sdk/mixins/httpproxy.rb', line 200 def base_uri SimpleAuthSdk.configuration.base_uri end |
#headers ⇒ Hash
Returns the headers to be sent with each request.
17 |
# File 'lib/simple_auth_sdk/mixins/httpproxy.rb', line 17 attr_writer :headers |
#retry_count ⇒ Integer
Returns the number of retries for failed requests.
25 26 27 |
# File 'lib/simple_auth_sdk/mixins/httpproxy.rb', line 25 def retry_count @retry_count end |
#timeout ⇒ Integer
Returns the timeout in seconds for HTTP requests.
21 22 23 |
# File 'lib/simple_auth_sdk/mixins/httpproxy.rb', line 21 def timeout @timeout end |
Instance Method Details
#add_headers(h = {}) ⇒ void
This method returns an undefined value.
Merges additional headers into the existing headers hash.
100 101 102 103 104 105 |
# File 'lib/simple_auth_sdk/mixins/httpproxy.rb', line 100 def add_headers(h = {}) raise ArgumentError, 'Headers must be an object which responds to #to_hash' unless h.respond_to?(:to_hash) @headers ||= {} @headers.merge!(h.to_hash) end |
#call(method, url, timeout, headers, body = nil) ⇒ RestClient::Response
Executes an HTTP request using rest-client.
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/simple_auth_sdk/mixins/httpproxy.rb', line 181 def call(method, url, timeout, headers, body = nil) RestClient::Request.execute( method: method, url: url, timeout: timeout, headers: headers, payload: body ) rescue RestClient::Exception => e case e when RestClient::RequestTimeout raise SimpleAuthSdk::RequestTimeout, e. else e.response end end |
#encode_uri(uri) ⇒ String
Encodes the URI based on whether a base_uri is set.
82 83 84 85 |
# File 'lib/simple_auth_sdk/mixins/httpproxy.rb', line 82 def encode_uri(uri) path = base_uri ? Addressable::URI.new(path: uri).normalized_path : Addressable::URI.escape(uri) url(path) end |
#method ⇒ Hash, String
Defines HTTP methods that will be proxied to HTTP class methods. These methods will make an HTTP request with the corresponding HTTP method.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/simple_auth_sdk/mixins/httpproxy.rb', line 42 %i[get post put patch delete delete_with_body].each do |method| define_method(method) do |uri, auth_token = nil, body = {}, extra_headers = {}| body = body.delete_if { |_, v| v.nil? } # Set the Authorization header if auth_token is provided headers_with_auth = extra_headers.dup headers_with_auth['Authorization'] = "Bearer #{auth_token}" if auth_token headers_with_auth['Content-Type'] ||= 'application/json' # Proceed with the request, passing the modified headers request_with_retry(method, uri, body, headers_with_auth) end end |
#request(method, uri, body = {}, extra_headers = {}) ⇒ Hash, String
Makes an HTTP request.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/simple_auth_sdk/mixins/httpproxy.rb', line 144 def request(method, uri, body = {}, extra_headers = {}) result = case method when :get @headers ||= {} get_headers = @headers.merge({ params: body }).merge(extra_headers) call(:get, encode_uri(uri), timeout, get_headers) when :delete @headers ||= {} delete_headers = @headers.merge({ params: body }) call(:delete, encode_uri(uri), timeout, delete_headers) when :delete_with_body call(:delete, encode_uri(uri), timeout, headers, body.to_json) else get_headers = headers.merge(extra_headers) call(method, encode_uri(uri), timeout, get_headers, body.to_json) end case result.code when 200...226 then safe_parse_json(result.body) when 400 then raise SimpleAuthSdk::BadRequest.new(result.body, code: result.code, headers: result.headers) when 401 then raise SimpleAuthSdk::Unauthorized.new(result.body, code: result.code, headers: result.headers) when 403 then raise SimpleAuthSdk::AccessDenied.new(result.body, code: result.code, headers: result.headers) when 404 then raise SimpleAuthSdk::NotFound.new(result.body, code: result.code, headers: result.headers) when 429 then raise SimpleAuthSdk::RateLimitEncountered.new(result.body, code: result.code, headers: result.headers) when 500 then raise SimpleAuthSdk::ServerError.new(result.body, code: result.code, headers: result.headers) else raise SimpleAuthSdk::Unsupported.new(result.body, code: result.code, headers: result.headers) end end |
#request_with_retry(method, uri, body = {}, extra_headers = {}) ⇒ Hash, String
Makes an HTTP request with retry logic.
124 125 126 127 128 |
# File 'lib/simple_auth_sdk/mixins/httpproxy.rb', line 124 def request_with_retry(method, uri, body = {}, extra_headers = {}) Retryable.retryable() do request(method, uri, body, extra_headers) end end |
#retry_options ⇒ Hash
Returns the options for retrying requests, including the number of retries and the sleep timer.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/simple_auth_sdk/mixins/httpproxy.rb', line 60 def sleep_timer = lambda do |attempt| wait = BASE_DELAY * (2**attempt - 1) # Exponential delay with each subsequent request attempt. wait += rand(wait + 1..wait + MAX_REQUEST_RETRY_JITTER) # Add jitter to the delay window. wait = [MAX_REQUEST_RETRY_DELAY, wait].min # Cap delay at MAX_REQUEST_RETRY_DELAY. wait = [MIN_REQUEST_RETRY_DELAY, wait].max # Ensure delay is no less than MIN_REQUEST_RETRY_DELAY. wait / 1000.to_f.round(2) # Convert ms to seconds end tries = 1 + [Integer(retry_count || DEFAULT_RETRIES), MAX_ALLOWED_RETRIES].min # Cap retries at MAX_ALLOWED_RETRIES { tries: tries, sleep: sleep_timer, on: SimpleAuthSdk::RateLimitEncountered } end |
#safe_parse_json(body) ⇒ Hash, String
Safely parses the JSON response body.
111 112 113 114 115 |
# File 'lib/simple_auth_sdk/mixins/httpproxy.rb', line 111 def safe_parse_json(body) JSON.parse(body.to_s) rescue JSON::ParserError body end |
#url(path) ⇒ String
Constructs the full URL by appending the path to the base URI.
91 92 93 |
# File 'lib/simple_auth_sdk/mixins/httpproxy.rb', line 91 def url(path) "#{base_uri}#{path}" end |