fulfil-io 0.6.1 → 0.7.1

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: d85caade2af0ca4d9ede6a970d0c9b802960e5bc192a41d70eb9c70f153640b1
4
- data.tar.gz: 13b43002ee089a9781221b7c42cef81a12474a11ccdb9f6e9df248eaabff8f84
3
+ metadata.gz: 379e34db2187ee0e41cbc4a530deec467a6f7874c8474c6a5057981ed6ff7ffc
4
+ data.tar.gz: da1e1fcf9945729d29554c803fcd954611575ba54f74e7f1c1931fe4e4554731
5
5
  SHA512:
6
- metadata.gz: 415418f4c4a54490b95d3823272096e481b064bf0be0f45cd44dc2c093af7616d93e56b46e0286447febf154f1187fbcbf31a0d3f3d8e456846a5590b636bc04
7
- data.tar.gz: 854d989c8b343dabd2317850f578a7a2c37c9a19d04873b19eb278c278a78a51c94a928d5cf24f5a7265f05a63ff6d1dfcb5853971448843957827af9554bb9e
6
+ metadata.gz: 12f8136e71bbe678bac2e3091209b5c56f03f3a70ceb3df50ebca4d88ac32f527e3f29c308affa2180f6e0e6c23964b48fed87801f18173c369f8cc161eb794f
7
+ data.tar.gz: 415ab129cabfe1bba36c7fd9bd94b083eeeb64fe0ff71ad508a6f29e74111ab863c281d4da93020b44ec54b1d9ef3fc1ac19ef1809e74af08091deb8824bac1f
data/README.md CHANGED
@@ -145,6 +145,34 @@ $ Fulfil.rate_limit.limit
145
145
  $ Fulfil.rate_limit.resets_at
146
146
  => #<DateTime: 2022-01-21T16:36:01-04:00 />
147
147
  ```
148
+
149
+ ### Automatic retry API call after rate limit hit
150
+
151
+ Automatic retries are supported whenever the rate limit is reached. However, it's not enabled by default. To enable it, set the `retry_on_rate_limit` to `true`. By default, the request will be retried in 1 second.
152
+
153
+ ```ruby
154
+ # config/initializers/fulfil.rb
155
+
156
+ Fulfil.configure do |config|
157
+ config.retry_on_rate_limit = true # Defaults to false
158
+ config.retry_on_rate_limit_wait = 0.25 # Defaults to 1 (second)
159
+ end
160
+ ```
161
+
162
+ ### Monitor rate limit hits
163
+
164
+ Through the configurable `rate_limit_notification_handler` one can monitor the rate limit hits to the APM tool of choice.
165
+
166
+ ```ruby
167
+ # config/initializers/fulfil.rb
168
+
169
+ Fulfil.configure do |config|
170
+ config.rate_limit_notification_handler = proc {
171
+ FakeAPM.increment_counter('fulfil.rate_limit_exceeded')
172
+ }
173
+ end
174
+ ```
175
+
148
176
  ## Development
149
177
 
150
178
  After checking out the repo, run `bin/setup` to install dependencies. Then, run
data/lib/fulfil/client.rb CHANGED
@@ -164,6 +164,13 @@ module Fulfil
164
164
  rescue HTTP::ResponseError => e
165
165
  raise ResponseError, "Can't process response: #{e}"
166
166
  []
167
+ # If configured, the client will wait whenever the `RateLimitExceeded` exception
168
+ # is raised. Check `Fulfil::Configuration` for more details.
169
+ rescue RateLimitExceeded => e
170
+ raise e unless config.retry_on_rate_limit?
171
+
172
+ sleep config.retry_on_rate_limit_wait
173
+ retry
167
174
  end
168
175
 
169
176
  def client
@@ -172,5 +179,9 @@ module Fulfil
172
179
  client = client.headers(@headers)
173
180
  client
174
181
  end
182
+
183
+ def config
184
+ Fulfil.config
185
+ end
175
186
  end
176
187
  end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fulfil
4
+ # The `Fulfil::Configuration` contains the available configuration options
5
+ # for the `Fulfil` gem.
6
+ class Configuration
7
+ # Allow the `Fulfil::Client` to automatically retry when the rate limit is hit.
8
+ # By default, the `Fulfil::Client` will wait 1 second before retrying again.
9
+ attr_accessor :retry_on_rate_limit
10
+ attr_accessor :retry_on_rate_limit_wait
11
+
12
+ # Allows the client to configure a notification handler. Can be used by APM
13
+ # tools to monitor the number of rate limit hits.
14
+ #
15
+ # @example Use APM to monitor the API rate limit hits
16
+ # Fulfil.configure do |config|
17
+ # config.rate_limit_notification_handler = proc {
18
+ # FakeAPM.increment_counter('fulfil.rate_limit_exceeded')
19
+ # }
20
+ # end
21
+ #
22
+ # @return [Proc, nil]
23
+ attr_accessor :rate_limit_notification_handler
24
+
25
+ def initialize
26
+ @retry_on_rate_limit = false
27
+ @retry_on_rate_limit_wait = 1
28
+ end
29
+
30
+ def retry_on_rate_limit?
31
+ @retry_on_rate_limit
32
+ end
33
+ end
34
+
35
+ # Returns Fulfil's configuration.
36
+ # @return [Fulfil::Configuration] Fulfil's configuration
37
+ def self.config
38
+ @config ||= Configuration.new
39
+ end
40
+
41
+ # Allows setting a new configuration for Fulfil.
42
+ # @return [Fulfil::Configuration] Fulfil's new configuration
43
+ def self.config=(configuration)
44
+ @config = configuration
45
+ end
46
+
47
+ # Allows modifying Fulfil's configuration.
48
+ #
49
+ # Example usage:
50
+ #
51
+ # Fulfil.configure do |config|
52
+ # config.api_key = "..."
53
+ # end
54
+ #
55
+ def self.configure
56
+ yield(config)
57
+ end
58
+ end
@@ -8,7 +8,8 @@ module Fulfil
8
8
 
9
9
  # Analyses the rate limit based on the response headers from Fulfil.
10
10
  # @param headers [HTTP::Headers] The HTTP response headers from Fulfil.
11
- # @return [Fulfil::RateLimit]
11
+ # @raise [Fulfil::RateLimitExceeded] When the rate limit is hit.
12
+ # @return [true]
12
13
  def analyse!(headers)
13
14
  rate_limit_headers = RateLimitHeaders.new(headers)
14
15
 
@@ -16,7 +17,9 @@ module Fulfil
16
17
  self.requests_left = rate_limit_headers.requests_left
17
18
  self.resets_at = rate_limit_headers.resets_at
18
19
 
19
- raise Fulfil::RateLimitExceeded unless requests_left?
20
+ return true if requests_left?
21
+
22
+ report_rate_limit_hit_and_raise
20
23
  end
21
24
 
22
25
  # Returns whether there are any requests left in the current rate limit window.
@@ -24,5 +27,13 @@ module Fulfil
24
27
  def requests_left?
25
28
  requests_left&.positive?
26
29
  end
30
+
31
+ private
32
+
33
+ # @raise [Fulfil::RateLimitExceeded]
34
+ def report_rate_limit_hit_and_raise
35
+ Fulfil.config.rate_limit_notification_handler&.call
36
+ raise Fulfil::RateLimitExceeded
37
+ end
27
38
  end
28
39
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fulfil
4
- VERSION = "0.6.1"
4
+ VERSION = "0.7.1"
5
5
  end
data/lib/fulfil.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'fulfil/version'
4
4
  require 'fulfil/error'
5
+ require 'fulfil/configuration'
5
6
  require 'fulfil/client'
6
7
  require 'fulfil/model'
7
8
  require 'fulfil/interactive_report'
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fulfil-io
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Moore
8
8
  - Kat Fairbanks
9
+ - Stefan Vermaas
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2022-04-22 00:00:00.000000000 Z
13
+ date: 2022-09-23 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: http
@@ -20,7 +21,7 @@ dependencies:
20
21
  version: 4.4.1
21
22
  - - "<"
22
23
  - !ruby/object:Gem::Version
23
- version: 5.1.0
24
+ version: 5.2.0
24
25
  type: :runtime
25
26
  prerelease: false
26
27
  version_requirements: !ruby/object:Gem::Requirement
@@ -30,7 +31,7 @@ dependencies:
30
31
  version: 4.4.1
31
32
  - - "<"
32
33
  - !ruby/object:Gem::Version
33
- version: 5.1.0
34
+ version: 5.2.0
34
35
  - !ruby/object:Gem::Dependency
35
36
  name: bundler
36
37
  requirement: !ruby/object:Gem::Requirement
@@ -152,6 +153,7 @@ files:
152
153
  - lib/fulfil-io.rb
153
154
  - lib/fulfil.rb
154
155
  - lib/fulfil/client.rb
156
+ - lib/fulfil/configuration.rb
155
157
  - lib/fulfil/error.rb
156
158
  - lib/fulfil/interactive_report.rb
157
159
  - lib/fulfil/model.rb
@@ -180,7 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
180
182
  - !ruby/object:Gem::Version
181
183
  version: '0'
182
184
  requirements: []
183
- rubygems_version: 3.3.7
185
+ rubygems_version: 3.3.12
184
186
  signing_key:
185
187
  specification_version: 4
186
188
  summary: Interact with the Fulfil.io API