-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Retry-After support to Requester
- Loading branch information
Showing
5 changed files
with
423 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class RetryDelayTooLarge < RestClient::Exception | ||
def initialize(response, delay:, max_delay:) | ||
super(response, response.code) | ||
|
||
self.message = 'Retry delay of %0.2gs exceeds limit of %0.2gs' % [delay, max_delay] | ||
end | ||
end | ||
|
||
class RetryLimitExceeded < RestClient::Exception | ||
def initialize(response, max_retries:) | ||
super(response, response.code) | ||
|
||
self.message = "Retry limit (#{max_retries}) exceeded" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
module BerkeleyLibrary | ||
module Util | ||
module URIs | ||
class Requester | ||
# rubocop:disable Metrics/ParameterLists | ||
module ClassMethods | ||
# Performs a GET request and returns the response body as a string. | ||
# | ||
# @param uri [URI, String] the URI to GET | ||
# @param params [Hash] the query parameters to add to the URI. (Note that the URI may already include query parameters.) | ||
# @param headers [Hash] the request headers. | ||
# @return [String] the body as a string. | ||
# @param log [Boolean] whether to log each request URL and response code | ||
# @param max_retries [Integer] the maximum number of times to retry after a 429 or 503 with Retry-After | ||
# @param max_retry_delay [Integer] the maximum retry delay (in seconds) to accept in a Retry-After header | ||
# @raise [RestClient::Exception] in the event of an unsuccessful request. | ||
def get(uri, params: {}, headers: {}, log: true, max_retries: MAX_RETRIES, max_retry_delay: MAX_RETRY_DELAY_SECONDS) | ||
resp = make_request(:get, uri, params, headers, log, max_retries, max_retry_delay) | ||
resp.body | ||
end | ||
|
||
# Performs a HEAD request and returns the response status as an integer. | ||
# Note that unlike {Requester#get}, this does not raise an error in the | ||
# event of an unsuccessful request. | ||
# | ||
# @param uri [URI, String] the URI to HEAD | ||
# @param params [Hash] the query parameters to add to the URI. (Note that the URI may already include query parameters.) | ||
# @param headers [Hash] the request headers. | ||
# @param log [Boolean] whether to log each request URL and response code | ||
# @return [Integer] the response code as an integer. | ||
def head(uri, params: {}, headers: {}, log: true, max_retries: MAX_RETRIES, max_retry_delay: MAX_RETRY_DELAY_SECONDS) | ||
head_response(uri, params: params, headers: headers, log: log, max_retries: max_retries, max_retry_delay: max_retry_delay).code | ||
end | ||
|
||
# Performs a GET request and returns the response, even in the event of | ||
# a failed request. | ||
# | ||
# @param uri [URI, String] the URI to GET | ||
# @param params [Hash] the query parameters to add to the URI. (Note that the URI may already include query parameters.) | ||
# @param headers [Hash] the request headers. | ||
# @param log [Boolean] whether to log each request URL and response code | ||
# @return [RestClient::Response] the response | ||
def get_response(uri, params: {}, headers: {}, log: true, max_retries: MAX_RETRIES, max_retry_delay: MAX_RETRY_DELAY_SECONDS) | ||
make_request(:get, uri, params, headers, log, max_retries, max_retry_delay) | ||
rescue RestClient::Exception => e | ||
e.response | ||
end | ||
|
||
# Performs a HEAD request and returns the response, even in the event of | ||
# a failed request. | ||
# | ||
# @param uri [URI, String] the URI to HEAD | ||
# @param params [Hash] the query parameters to add to the URI. (Note that the URI may already include query parameters.) | ||
# @param headers [Hash] the request headers. | ||
# @param log [Boolean] whether to log each request URL and response code | ||
# @return [RestClient::Response] the response | ||
def head_response(uri, params: {}, headers: {}, log: true, max_retries: MAX_RETRIES, max_retry_delay: MAX_RETRY_DELAY_SECONDS) | ||
make_request(:head, uri, params, headers, log, max_retries, max_retry_delay) | ||
rescue RestClient::Exception => e | ||
e.response | ||
end | ||
|
||
private | ||
|
||
def make_request(method, url, params, headers, log, max_retries, max_retry_delay) | ||
Requester.new( | ||
method, | ||
url, | ||
params: params, | ||
headers: headers, | ||
log: log, | ||
max_retries: max_retries, | ||
max_retry_delay: max_retry_delay | ||
).make_request | ||
end | ||
|
||
end | ||
# rubocop:enable Metrics/ParameterLists | ||
|
||
# ------------------------------------------------------------ | ||
# Class methods | ||
|
||
class << self | ||
include ClassMethods | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.