faraday-retry 2.0.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b07a16fd11fb4fcc1041260ad3e7341f40762eef86984f23ee5785c1c15b4334
4
- data.tar.gz: 3551328efd8212c9f2079ce2e4b2b192271bfccc3c4df337f921f9403fefd742
3
+ metadata.gz: f4c985b60e619c7a2161b8643fda86adcd4927847a66d903bbc22a32ed851d13
4
+ data.tar.gz: 010d8a002a7310f82c44395344cc81cb3f8acfe9e53ca8aae8ae84cd3b66d90a
5
5
  SHA512:
6
- metadata.gz: c0a0b3718c1085d8c43f350ab4d82a21b508d215fc1caa59751d2552ee2598dc9281a5a21589d9cf08bad4a4b68925d6d6e27c592fe38fc3a40d6d2a606e0ce1
7
- data.tar.gz: 891a2994edb8f81ea829710faa26b87056206be42d12092ce6c7250bb7cebaaaf1cd4363ce0f4d36307d12f1ecaa9a15385418bec80a2cab390b3f245f96ae54
6
+ metadata.gz: b6dc78fe8c3b3ccc189c086271eb646e06f6e598a0a2b311ce3f0831321ba8f00b01d24be1c29521f7bbef5367eb17c43be7f6dfaa6402f0ec9cf206483f8794
7
+ data.tar.gz: 6338884c1806a7d06f33457db3939d71aaf761c9cc8c427a14c68257600d0601ff6765dde9b061254fd5af8f4ff77315d018a8d42d5326842fd0d26d0e52219f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## v2.2.0 (2023-06-01)
4
+
5
+ * Support new `header_parser_block` option. [PR #28](https://github.com/lostisland/faraday-retry/pull/28). Thanks, [@zavan]!
6
+
7
+ ## v2.1.0 (2023-03-03)
8
+
9
+ * Support for custom RateLimit headers. [PR #13](https://github.com/lostisland/faraday-retry/pull/13). Thanks, [@brookemckim]!
10
+
11
+ v2.1.1 (2023-02-17) is a spurious not-released version that you may have seen mentioned in this CHANGELOG.
12
+
3
13
  ## v2.0.0 (2022-06-08)
4
14
 
5
15
  ### Changed
@@ -22,3 +32,5 @@ This release consists of the same middleware that was previously bundled with Fa
22
32
  * Retry middleware `retry_block` is not called if retry will not happen due to `max_interval`, https://github.com/lostisland/faraday/pull/1350
23
33
 
24
34
  [@maxprokopiev]: https://github.com/maxprokopiev
35
+ [@brookemckim]: https://github.com/brookemckim
36
+ [@zavan]: https://github.com/zavan
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Faraday Retry
2
2
 
3
- [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/lostisland/faraday-retry/CI)](https://github.com/lostisland/faraday-retry/actions?query=branch%3Amain)
3
+ [![CI](https://github.com/lostisland/faraday-retry/actions/workflows/ci.yaml/badge.svg)](https://github.com/lostisland/faraday-retry/actions/workflows/ci.yaml)
4
4
  [![Gem](https://img.shields.io/gem/v/faraday-retry.svg?style=flat-square)](https://rubygems.org/gems/faraday-retry)
5
5
  [![License](https://img.shields.io/github/license/lostisland/faraday-retry.svg?style=flat-square)](LICENSE.md)
6
6
 
@@ -100,7 +100,7 @@ retry_options = {
100
100
 
101
101
  Some APIs, like the [Slack API](https://api.slack.com/docs/rate-limits), will inform you when you reach their API limits by replying with a response status code of `429`
102
102
  and a response header of `Retry-After` containing a time in seconds. You should then only retry querying after the amount of time provided by the `Retry-After` header,
103
- otherwise you won't get a response. Other APIs communicate their rate limits via the [RateLimit-xxx](https://tools.ietf.org/id/draft-polli-ratelimit-headers-00.html#rfc.section.3.3) headers
103
+ otherwise you won't get a response. Other APIs communicate their rate limits via the [RateLimit-xxx](https://www.ietf.org/archive/id/draft-ietf-httpapi-ratelimit-headers-05.html#name-providing-ratelimit-fields) headers
104
104
  where `RateLimit-Reset` behaves similarly to the `Retry-After`.
105
105
 
106
106
  You can automatically handle both headers and have Faraday pause and retry for the right amount of time by including the `429` status code in the retry statuses list:
@@ -111,6 +111,17 @@ retry_options = {
111
111
  }
112
112
  ```
113
113
 
114
+ If you are working with an API which does not comply with the Rate Limit RFC you can specify custom headers to be used for retry and reset, as well as a block to parse the headers:
115
+
116
+ ```ruby
117
+ retry_options = {
118
+ retry_statuses: [429],
119
+ rate_limit_retry_header: 'x-rate-limit-retry-after',
120
+ rate_limit_reset_header: 'x-rate-limit-reset',
121
+ header_parser_block: ->(value) { Time.at(value.to_i).utc - Time.now.utc }
122
+ }
123
+ ```
124
+
114
125
  #### Specify a custom retry logic
115
126
 
116
127
  You can also specify a custom retry logic with the `retry_if` option.
@@ -142,7 +153,7 @@ For example, you might want to keep track of the response statuses:
142
153
  ```ruby
143
154
  response_statuses = []
144
155
  retry_options = {
145
- retry_block: -> (env:, options:, retries_remaining:, exception:, will_retry_in:) { response_statuses << env.status }
156
+ retry_block: -> (env:, options:, retry_count:, exception:, will_retry_in:) { response_statuses << env.status }
146
157
  }
147
158
  ```
148
159
 
@@ -26,7 +26,8 @@ module Faraday
26
26
  :interval_randomness,
27
27
  :backoff_factor, :exceptions,
28
28
  :methods, :retry_if, :retry_block,
29
- :retry_statuses)
29
+ :retry_statuses, :rate_limit_retry_header,
30
+ :rate_limit_reset_header, :header_parser_block)
30
31
 
31
32
  DEFAULT_CHECK = ->(_env, _exception) { false }
32
33
 
@@ -119,6 +120,10 @@ module Faraday
119
120
  # codes or a single Integer value that determines whether to raise
120
121
  # a Faraday::RetriableResponse exception based on the HTTP status code
121
122
  # of an HTTP response.
123
+ # @option options [Block] :header_parser_block block that will receive
124
+ # the the value of the retry header and should return the number of
125
+ # seconds to wait before retrying the request. This is useful if the
126
+ # value of the header is not a number of seconds or a RFC 2822 formatted date.
122
127
  def initialize(app, options = nil)
123
128
  super(app)
124
129
  @options = Options.from(options)
@@ -213,15 +218,17 @@ module Faraday
213
218
  end
214
219
 
215
220
  # RFC for RateLimit Header Fields for HTTP:
216
- # https://tools.ietf.org/id/draft-polli-ratelimit-headers-00.html#rfc.section.3.3
221
+ # https://www.ietf.org/archive/id/draft-ietf-httpapi-ratelimit-headers-05.html#name-fields-definition
217
222
  def calculate_rate_limit_reset(env)
218
- parse_retry_header(env, 'RateLimit-Reset')
223
+ reset_header = @options.rate_limit_reset_header || 'RateLimit-Reset'
224
+ parse_retry_header(env, reset_header)
219
225
  end
220
226
 
221
227
  # MDN spec for Retry-After header:
222
228
  # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
223
229
  def calculate_retry_after(env)
224
- parse_retry_header(env, 'Retry-After')
230
+ retry_header = @options.rate_limit_retry_header || 'Retry-After'
231
+ parse_retry_header(env, retry_header)
225
232
  end
226
233
 
227
234
  def calculate_retry_interval(retries)
@@ -241,12 +248,16 @@ module Faraday
241
248
 
242
249
  retry_after_value = env[:response_headers][header]
243
250
 
244
- # Try to parse date from the header value
245
- begin
246
- datetime = DateTime.rfc2822(retry_after_value)
247
- datetime.to_time - Time.now.utc
248
- rescue ArgumentError
249
- retry_after_value.to_f
251
+ if @options.header_parser_block
252
+ @options.header_parser_block.call(retry_after_value)
253
+ else
254
+ # Try to parse date from the header value
255
+ begin
256
+ datetime = DateTime.rfc2822(retry_after_value)
257
+ datetime.to_time - Time.now.utc
258
+ rescue ArgumentError
259
+ retry_after_value.to_f
260
+ end
250
261
  end
251
262
  end
252
263
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Faraday
4
4
  module Retry
5
- VERSION = '2.0.0'
5
+ VERSION = '2.2.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-retry
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mattia Giuffrida
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-15 00:00:00.000000000 Z
11
+ date: 2023-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -157,8 +157,8 @@ licenses:
157
157
  - MIT
158
158
  metadata:
159
159
  bug_tracker_uri: https://github.com/lostisland/faraday-retry/issues
160
- changelog_uri: https://github.com/lostisland/faraday-retry/blob/v2.0.0/CHANGELOG.md
161
- documentation_uri: http://www.rubydoc.info/gems/faraday-retry/2.0.0
160
+ changelog_uri: https://github.com/lostisland/faraday-retry/blob/v2.2.0/CHANGELOG.md
161
+ documentation_uri: http://www.rubydoc.info/gems/faraday-retry/2.2.0
162
162
  homepage_uri: https://github.com/lostisland/faraday-retry
163
163
  source_code_uri: https://github.com/lostisland/faraday-retry
164
164
  post_install_message: