Class: SabyVok::Http
- Inherits:
-
Object
- Object
- SabyVok::Http
- Defined in:
- lib/saby_vok/http.rb
Constant Summary collapse
- DEFAULT_TIMEOUT =
15- DEFAULT_RETRIES =
2- RETRYABLE_CODES =
[ 429, 502, 503, 504 ].freeze
Instance Method Summary collapse
-
#initialize(logger: nil, timeout: DEFAULT_TIMEOUT, retries: DEFAULT_RETRIES) ⇒ Http
constructor
A new instance of Http.
- #request(method:, uri:, headers:, params:, expect_binary: false) ⇒ Hash, ...
Constructor Details
#initialize(logger: nil, timeout: DEFAULT_TIMEOUT, retries: DEFAULT_RETRIES) ⇒ Http
Returns a new instance of Http.
16 17 18 19 20 |
# File 'lib/saby_vok/http.rb', line 16 def initialize(logger: nil, timeout: DEFAULT_TIMEOUT, retries: DEFAULT_RETRIES) @logger = logger @timeout = timeout @retries = retries end |
Instance Method Details
#request(method:, uri:, headers:, params:, expect_binary: false) ⇒ Hash, ...
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/saby_vok/http.rb', line 29 def request(method:, uri:, headers:, params:, expect_binary: false) raise ArgumentError, "uri must be URI" unless uri.is_a?(URI) attempts = 0 loop do attempts += 1 response = perform_request(method, uri, headers, params) if retryable_response?(response) && attempts <= @retries + 1 backoff(attempts) next end return parse_response(response, expect_binary: expect_binary) rescue Timeout::Error => e if attempts <= @retries + 1 backoff(attempts) next end raise HttpError.new("request timed out: #{e.class}", status: 504, body: nil) rescue SocketError, Errno::ECONNREFUSED => e raise HttpError.new( "connection failed: #{e.class} #{e.}", status: 503, body: nil ) end end |