Class: ChimeraHttpClient::Request
- Inherits:
-
Object
- Object
- ChimeraHttpClient::Request
- Defined in:
- lib/chimera_http_client/request.rb
Constant Summary collapse
- TIMEOUT_SECONDS =
3- RETRYABLE_METHODS =
Only idempotent methods are retried automatically, regardless of the configured retries count
%i(get put delete head).freeze
- RETRYABLE_ERRORS =
Only network failures and 5xx are retried - 4xx client errors can't be fixed by retrying
[ConnectionError, TimeoutError, ServerError].freeze
- RETRY_BACKOFF_MULTIPLIER =
2
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
- #create(url:, method:, body: nil, options: {}, headers: {}) ⇒ Object
-
#initialize(options = {}) ⇒ Request
constructor
A new instance of Request.
- #run(url:, method:, body: nil, options: {}, headers: {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Request
Returns a new instance of Request.
15 16 17 18 19 |
# File 'lib/chimera_http_client/request.rb', line 15 def initialize( = {}) @options = @hydra = [:hydra] @attempt = 0 end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
13 14 15 |
# File 'lib/chimera_http_client/request.rb', line 13 def request @request end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
13 14 15 |
# File 'lib/chimera_http_client/request.rb', line 13 def result @result end |
Instance Method Details
#create(url:, method:, body: nil, options: {}, headers: {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/chimera_http_client/request.rb', line 38 def create(url:, method:, body: nil, options: {}, headers: {}) request_params = { method: method, body: body, params: [:params] || {}, headers: headers, timeout: [:timeout] || TIMEOUT_SECONDS, accept_encoding: "gzip", cache: [:cache], verbose: [:verbose], } # Basic-auth support: username = .fetch(:username, nil) password = .fetch(:password, nil) request_params[:userpwd] = "#{username}:#{password}" if username && password @request = Typhoeus::Request.new(url, request_params) @result = nil @request.on_complete do |response| runtime = response.total_time&.round(3) @options[:monitor]&.call( { url: url, method: method, status: response.code, runtime: runtime, completed_at: Time.now.utc.iso8601(3), context: [:monitoring_context] } ) @options[:logger]&.info( { message: "Completed Chimera HTTP Request", method: method.upcase, url: url, code: response.code, runtime: runtime, user_agent: headers["User-Agent"], } ) result = on_complete_handler(response) # Used for queued requests (Queue): retries are re-queued onto the same Hydra from # inside this callback, per Typhoeus::Hydra::Queueable#queue ("can even be done while # the hydra is running"). Connection-built requests never have a @hydra, so this branch # never triggers for them - their retries are handled by the loop in #run instead. if @hydra && retryable?(method, result, , @attempt) @attempt += 1 sleep(delay_for(@attempt, )) create(url: url, method: method, body: body, options: , headers: headers) @hydra.queue(@request) else @result = result end end @options[:logger]&.info( { message: "Starting Chimera HTTP Request", method: method.upcase, url: url, code: nil, runtime: 0, user_agent: headers["User-Agent"], } ) self end |
#run(url:, method:, body: nil, options: {}, headers: {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/chimera_http_client/request.rb', line 21 def run(url:, method:, body: nil, options: {}, headers: {}) create(url: url, method: method, body: body, options: , headers: headers) @request.run attempt = 0 while retryable?(method, @result, , attempt) attempt += 1 sleep(delay_for(attempt, )) create(url: url, method: method, body: body, options: , headers: headers) @request.run end @result end |