esi 0.4.14 → 0.4.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5318ada47a8456e3ec80feb871a3998c8e760dc3ec12c2b8d975252292a04f45
4
- data.tar.gz: b0adbff86424651b6c12f19f3dc1e5d0564ea20e2b46e7c3a76170a0140c5cc6
3
+ metadata.gz: 38abebf1504982b7b86029e5aea6b8b3fe9160f6fcde54a67f14f1c361e3da3e
4
+ data.tar.gz: a41d6bcc3e17481df8986e2b767a806c446f20e6236c3524160b49e72951043b
5
5
  SHA512:
6
- metadata.gz: 20c417ed1f54121112a39cb8a7c1e4bc8087f88e167a75a8f06314a7ed67a418040e01f6c1583dc26f6da5cc7abb664839ab3fc74a386f0b85c68fbb47b562b5
7
- data.tar.gz: e15bcb5f258699f1d3721d5f5936b0ae30c4540275369f4dd95fba537f7eddc4cd605d1881b0b35afe1b5dadc1cd294320d7f9ae446e8720e03110cdf271294c
6
+ metadata.gz: 287f5456f7fbefaaea7ebf854c29fa33d7022aabda913e3e06c9cc59f9c85f06bf92e9f0c92609aff681eb355fe3343a3c5b0c303fc13dc0c950ef33f871525c
7
+ data.tar.gz: b3a34b04669ce5b1d43ee8ab7f76ce0ec62a92c9bcf566df232ae58ae82eb0859de5767d84b424122b67fe6a3b9af6b5f7a722e07d5e891ee2b2bdca963e1459
data/lib/esi/api_error.rb CHANGED
@@ -50,6 +50,58 @@ module Esi
50
50
  end
51
51
  end
52
52
 
53
+ # TimeoutError Class
54
+ # @!attribute [r] response
55
+ # @return [Esi::Response] the ApiError Response
56
+ # @!attribute [r] key
57
+ # @return [String] the response.data[:key]
58
+ # @!attribute [r] message
59
+ # @return [String] the response error message
60
+ # @!attribute [r] type
61
+ # @return [String] the response.data[:exceptionType]
62
+ # @!attribute [r] original_exception
63
+ # @return [ExceptionClass|nil] the orginal raised exception
64
+ class TimeoutError < ApiError; end
65
+
66
+ # TemporaryServerError Class
67
+ # @!attribute [r] response
68
+ # @return [Esi::Response] the ApiError Response
69
+ # @!attribute [r] key
70
+ # @return [String] the response.data[:key]
71
+ # @!attribute [r] message
72
+ # @return [String] the response error message
73
+ # @!attribute [r] type
74
+ # @return [String] the response.data[:exceptionType]
75
+ # @!attribute [r] original_exception
76
+ # @return [ExceptionClass|nil] the orginal raised exception
77
+ class TemporaryServerError < ApiError; end
78
+
79
+ # RateLimitError Class
80
+ # @!attribute [r] response
81
+ # @return [Esi::Response] the ApiError Response
82
+ # @!attribute [r] key
83
+ # @return [String] the response.data[:key]
84
+ # @!attribute [r] message
85
+ # @return [String] the response error message
86
+ # @!attribute [r] type
87
+ # @return [String] the response.data[:exceptionType]
88
+ # @!attribute [r] original_exception
89
+ # @return [ExceptionClass|nil] the orginal raised exception
90
+ class RateLimitError < ApiError; end
91
+
92
+ # UnauthorizedError Class
93
+ # @!attribute [r] response
94
+ # @return [Esi::Response] the ApiError Response
95
+ # @!attribute [r] key
96
+ # @return [String] the response.data[:key]
97
+ # @!attribute [r] message
98
+ # @return [String] the response error message
99
+ # @!attribute [r] type
100
+ # @return [String] the response.data[:exceptionType]
101
+ # @!attribute [r] original_exception
102
+ # @return [ExceptionClass|nil] the orginal raised exception
103
+ class UnauthorizedError < ApiError; end
104
+
53
105
  # ApiUnknowntError Class
54
106
  # @!attribute [r] response
55
107
  # @return [Esi::Response] the ApiError Response
data/lib/esi/client.rb CHANGED
@@ -64,7 +64,7 @@ module Esi
64
64
  # previous client or default client
65
65
  #
66
66
  # @example Call an Esi::Client method using an instance of client
67
- # new_client = Esi::Client.new(token: 'foo', refresh_token: 'foo', expires_at: 30.minutes.from_now)
67
+ # new_client = Esi::Client.new(token: 'foo', refresh_token: 'foo', exceptionxpires_at: 30.minutes.from_now)
68
68
  # new_client.with_client do |client|
69
69
  # client.character(1234)
70
70
  # end
@@ -143,7 +143,7 @@ module Esi
143
143
 
144
144
  def cached_response(klass, *args, &block)
145
145
  call = klass.new(*args)
146
- Esi.cache.fetch(call.cache_key, expires_in: klass.cache_duration) do
146
+ Esi.cache.fetch(call.cache_key, exceptionxpires_in: klass.cache_duration) do
147
147
  make_call(call, &block)
148
148
  end
149
149
  end
@@ -157,21 +157,17 @@ module Esi
157
157
  access_token: @access_token,
158
158
  refresh_token: @refresh_token,
159
159
  expires_at: @expires_at,
160
- callback: lambda { |token, expires_at|
160
+ callback: lambda { |token, exceptionxpires_at|
161
161
  @access_token = token
162
162
  @expires_at = expires_at
163
- refresh_callback.call(token, expires_at) if refresh_callback.respond_to?(:call)
163
+ refresh_callback.call(token, exceptionxpires_at) if refresh_callback.respond_to?(:call)
164
164
  }
165
165
  )
166
166
  end
167
167
 
168
168
  def request_paginated(call, &block)
169
169
  call.page = 1
170
- response = nil
171
- ActiveSupport::Notifications.instrument('esi.client.request.paginated') do
172
- response = paginated_response(response, call, &block)
173
- end
174
- response
170
+ paginated_response(response, call, &block)
175
171
  end
176
172
 
177
173
  def paginated_response(response, call, &block)
@@ -183,114 +179,41 @@ module Esi
183
179
  end
184
180
  end
185
181
 
186
- # @todo esi should not retry
187
182
  # @todo make rubocop compliant
188
- # rubocop:disable Lint/ShadowedException
189
183
  # rubocop:disable Metrics/AbcSize
190
- # rubocop:disable Metrics/BlockLength
191
- # rubocop:disable Metrics/CyclomaticComplexity
192
- # rubocop:disable Metrics/MethodLength
193
- # rubocop:disable Metrics/PerceivedComplexity
194
184
  def request(call, &block)
195
- response = nil
196
- last_ex = nil
197
- options = { timeout: Esi.config.timeout }
198
- url ||= call.url
199
-
200
- debug "Starting request: #{url}"
201
-
202
- ActiveSupport::Notifications.instrument('esi.client.request') do
203
- 1.upto(MAX_ATTEMPTS) do |_try|
204
- last_ex = nil
205
- response = nil
206
-
207
- begin
208
- response = Timeout.timeout(Esi.config.timeout) do
209
- oauth.request(call.method, url, options)
210
- end
211
- rescue Faraday::SSLError, Faraday::ConnectionFailed, Timeout::Error, Net::ReadTimeout => e
212
- last_ex = e
213
- logger.error e.to_s
214
- sleep 3
215
- next
216
- rescue OAuth2::Error => e
217
- last_ex = e
218
- response = Response.new(e.response, call)
219
-
220
- case e.response.status
221
- when 502 # Temporary server error
222
- logger.error "TemporaryServerError: #{response.error}"
223
- sleep 5
224
- next
225
- when 503 # Rate Limit
226
- logger.error 'RateLimit error, sleeping for 5 seconds'
227
- sleep 5
228
- next
229
- when 404 # Not Found
230
- raise Esi::ApiNotFoundError.new(Response.new(e.response, call), e)
231
- when 403 # Forbidden
232
- debug_error('ApiForbiddenError', url, response)
233
- raise Esi::ApiForbiddenError.new(response, e)
234
- when 400 # Bad Request
235
- exception = Esi::ApiBadRequestError.new(response, e)
236
- case exception.message
237
- when 'invalid_token'
238
- debug_error('ApiRefreshTokenExpiredError ', url, response)
239
- raise Esi::ApiRefreshTokenExpiredError.new(response, e)
240
- when 'invalid_client'
241
- debug_error('ApiInvalidAppClientKeysError', url, response)
242
- raise Esi::ApiInvalidAppClientKeysError.new(response, e)
243
- else
244
- debug_error('ApiBadRequestError', url, response)
245
- raise exception
246
- end
247
- else
248
- debug_error('ApiUnknownError', url, response)
249
- raise Esi::ApiUnknownError.new(response, e)
250
- end
251
- end
252
-
253
- break if response
254
- end
185
+ response = Timeout.timeout(Esi.config.timeout) do
186
+ oauth.request(call.method, call.url, timeout: Esi.config.timeout)
255
187
  end
256
- # rubocop:enable Lint/ShadowedException
257
- # rubocop:enable Metrics/AbcSize
258
- # rubocop:enable Metrics/BlockLength
259
- # rubocop:enable Metrics/CyclomaticComplexity
260
- # rubocop:enable Metrics/MethodLength
261
- # rubocop:enable Metrics/PerceivedComplexity
262
-
263
- if last_ex
264
- logger.error "Request failed with #{last_ex.class}"
265
- debug_error(last_ex.class, url, response)
266
- raise Esi::ApiRequestError, last_ex
188
+ response = Response.new(response, call)
189
+ response.data.each { |item| yield(item) } if block
190
+ response.save
191
+ rescue OAuth2::Error => e
192
+ exception = error_class_for(e.response.status).new(Response.new(e.response, call), e)
193
+ raise exception.is_a?(Esi::ApiBadRequestError) ? process_bad_request_error(exception) : exception
194
+ rescue Faraday::SSLError, Faraday::ConnectionFailed, Timeout::Error => e
195
+ raise Esi::TimeoutError.new(Response.new(e.response, call), exception)
196
+ end
197
+ # rubocop:enable Metrics/AbcSize
198
+
199
+ def error_class_for(status)
200
+ case status
201
+ when 400 then Esi::ApiBadRequestError
202
+ when 401 then Esi::UnauthorizedError
203
+ when 403 then Esi::ApiForbiddenError
204
+ when 404 then Esi::ApiNotFoundError
205
+ when 502 then Esi::TemporaryServerError
206
+ when 503 then Esi::RateLimitError
207
+ else Esi::ApiUnknownError
267
208
  end
268
-
269
- debug 'Request successful'
270
-
271
- ActiveSupport::Notifications.instrument('esi.client.response.render') do
272
- response = Response.new(response, call)
273
- response.save
274
- end
275
- ActiveSupport::Notifications.instrument('esi.client.response.callback') do
276
- response.data.each { |item| yield(item) } if block
277
- end
278
- response
279
209
  end
280
210
 
281
- def debug_error(klass, url, response)
282
- [
283
- '-' * 60,
284
- "#{klass}(#{response.error})",
285
- "STATUS: #{response.status}",
286
- "MESSAGE: #{debug_message_for_response(response)}",
287
- "URL: #{url}",
288
- '-' * 60
289
- ].each { |msg| logger.error(msg) }
290
- end
291
-
292
- def debug_message_for_response(response)
293
- response.respond_to?(:data) ? (response.data[:message].presence || response.data[:error]) : response.try(:body)
211
+ def process_bad_request_error(exception)
212
+ case exception.message
213
+ when 'invalid_token' then Esi::ApiRefreshTokenExpiredError.new(response, exception)
214
+ when 'invalid_client' then Esi::ApiInvalidAppClientKeysError.new(response, exception)
215
+ else exception
216
+ end
294
217
  end
295
218
  end
296
219
  end
data/lib/esi/response.rb CHANGED
@@ -32,8 +32,8 @@ module Esi
32
32
  end
33
33
 
34
34
  def save
35
- return unless should_log_response?
36
- File.write(log_directroy.join("#{Time.now.to_i}.json"), to_json)
35
+ File.write(log_directroy.join("#{Time.now.to_i}.json"), to_json) if should_log_response?
36
+ self
37
37
  end
38
38
 
39
39
  def log_directory
@@ -56,7 +56,7 @@ module Esi
56
56
  private
57
57
 
58
58
  def normalize_response_body
59
- MultiJson.load(body || {}, symbolize_keys: true, object_class: OpenStruct)
59
+ MultiJson.load(body.presence || '{}', symbolize_keys: true, object_class: OpenStruct)
60
60
  end
61
61
  end
62
62
  end
data/lib/esi/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Esi
4
- VERSION = '0.4.14'
4
+ VERSION = '0.4.16'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: esi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.14
4
+ version: 0.4.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Hiemstra
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-03-25 00:00:00.000000000 Z
12
+ date: 2018-03-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport