zendesk_api 1.18.0 → 1.19.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: 6245981abb291dac06ffeb8de8a5b15616ae662af15fa98bbd0602868bee842f
4
- data.tar.gz: 07635336cd0ad32a96a936f35b5190fd61bfb21c8ed7a1e49195f5565d0f5126
3
+ metadata.gz: 6d251a3a7a6b3e5830ef96e225b17cab2955918438ba819e2148fc1e5d233870
4
+ data.tar.gz: 3ae7a4241ab08f7b83833341368050ea52b8877ae0fdabaed97ed4d7d6054032
5
5
  SHA512:
6
- metadata.gz: eb77d9d24bcd8749b128d9f99a58f69bb611538b1e1b6b28580599db900aed3db309a4efda05e2edfeafb57de8fa498f3c938db0531e8efe0aaa34566e8fa683
7
- data.tar.gz: 3f90c014afafe35b5ca52e7374e97590f245114212c305aff4de5e080c0cb14be754f13e9a7109162c75a50397bd6354c9ea26085bd4170d57f5aef7de244ef2
6
+ metadata.gz: 11e3c75f505d736d00a6124867fcdd521a9545b7c3d1a60a72f4a0404db33c08048752f0a251513428e14ea909a37eda5a727cbf99ac514eb20106c497ae0ca9
7
+ data.tar.gz: 41cb490e567057b7b6938670561827401e6a9a8fd244298fd2180dc57af97c3eec129e4c64042f382232165680a998517eab60d0398e249f21c9a38d4d15ab4f
@@ -8,6 +8,7 @@ require 'zendesk_api/lru_cache'
8
8
  require 'zendesk_api/silent_mash'
9
9
  require 'zendesk_api/middleware/request/etag_cache'
10
10
  require 'zendesk_api/middleware/request/retry'
11
+ require 'zendesk_api/middleware/request/raise_rate_limited'
11
12
  require 'zendesk_api/middleware/request/upload'
12
13
  require 'zendesk_api/middleware/request/encode_json'
13
14
  require 'zendesk_api/middleware/request/url_based_access_token'
@@ -93,6 +94,8 @@ module ZendeskAPI
93
94
 
94
95
  config.retry = !!config.retry # nil -> false
95
96
 
97
+ set_raise_error_when_rated_limited
98
+
96
99
  set_token_auth
97
100
 
98
101
  set_default_logger
@@ -171,7 +174,12 @@ module ZendeskAPI
171
174
  builder.use ZendeskAPI::Middleware::Request::Upload
172
175
  builder.request :multipart
173
176
  builder.use ZendeskAPI::Middleware::Request::EncodeJson
174
- builder.use ZendeskAPI::Middleware::Request::Retry, :logger => config.logger if config.retry # Should always be first in the stack
177
+
178
+ # Should always be first in the stack
179
+ builder.use ZendeskAPI::Middleware::Request::Retry, :logger => config.logger if config.retry
180
+ if config.raise_error_when_rate_limited
181
+ builder.use ZendeskAPI::Middleware::Request::RaiseRateLimited, :logger => config.logger
182
+ end
175
183
 
176
184
  builder.adapter(*adapter)
177
185
  end
@@ -190,6 +198,14 @@ module ZendeskAPI
190
198
  end
191
199
  end
192
200
 
201
+ def set_raise_error_when_rated_limited
202
+ config.raise_error_when_rate_limited = if config.retry
203
+ false
204
+ else
205
+ !!config.raise_error_when_rate_limited
206
+ end
207
+ end
208
+
193
209
  def set_token_auth
194
210
  if config.token && !config.password
195
211
  config.password = config.token
@@ -16,6 +16,9 @@ module ZendeskAPI
16
16
  # @return [Boolean] Whether to attempt to retry when rate-limited (http status: 429).
17
17
  attr_accessor :retry
18
18
 
19
+ # @return [Boolean] Whether to raise error when rate-limited (http status: 429).
20
+ attr_accessor :raise_error_when_rate_limited
21
+
19
22
  # @return [Logger] Logger to use when logging requests.
20
23
  attr_accessor :logger
21
24
 
@@ -33,5 +33,6 @@ module ZendeskAPI
33
33
 
34
34
  class NetworkError < ClientError; end
35
35
  class RecordNotFound < ClientError; end
36
+ class RateLimited < ClientError; end
36
37
  end
37
38
  end
@@ -0,0 +1,31 @@
1
+ require 'faraday/middleware'
2
+ require 'zendesk_api/error'
3
+
4
+ module ZendeskAPI
5
+ module Middleware
6
+ # @private
7
+ module Request
8
+ # Faraday middleware to handle HTTP Status 429 (rate limiting) / 503 (maintenance)
9
+ # @private
10
+ class RaiseRateLimited < Faraday::Middleware
11
+ ERROR_CODES = [429, 503].freeze
12
+
13
+ def initialize(app, options = {})
14
+ super(app)
15
+ @logger = options[:logger]
16
+ end
17
+
18
+ def call(env)
19
+ response = @app.call(env)
20
+
21
+ if ERROR_CODES.include?(response.env[:status])
22
+ @logger.warn 'You have been rate limited. Raising error...' if @logger
23
+ raise Error::RateLimited.new(env)
24
+ else
25
+ response
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module ZendeskAPI
2
- VERSION = "1.18.0"
2
+ VERSION = "1.19.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zendesk_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.18.0
4
+ version: 1.19.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Davidovitz
@@ -109,6 +109,7 @@ files:
109
109
  - lib/zendesk_api/lru_cache.rb
110
110
  - lib/zendesk_api/middleware/request/encode_json.rb
111
111
  - lib/zendesk_api/middleware/request/etag_cache.rb
112
+ - lib/zendesk_api/middleware/request/raise_rate_limited.rb
112
113
  - lib/zendesk_api/middleware/request/retry.rb
113
114
  - lib/zendesk_api/middleware/request/upload.rb
114
115
  - lib/zendesk_api/middleware/request/url_based_access_token.rb