oauth2_api_client 2.1.0 → 3.0.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: '09a8c7819bfff19e85236301e161cf9481d4f53357bc9952084e5582d509337c'
4
- data.tar.gz: d047883ee8e1fd35ff7ffce55f0983653dc862050e1aea786ee410259fbc44d1
3
+ metadata.gz: 54c4982a38d552df8c467fcbd0064eba0d1c35e1dd58fffb07a1d8fbe338c8b1
4
+ data.tar.gz: 446b18222a7f5bd1259d2482304180ad0a0e73fd811004464a46b265ac946537
5
5
  SHA512:
6
- metadata.gz: e52fed4a4c43fa3cc4db0ddc9d0cd6fc0e628b65a0aeaa1b50b9761c7774e8f72d8b08e0eba8ccce6cb69032da9ef92eedb5edb3fb5aa988b6ab5f9ce5527584
7
- data.tar.gz: 1ebf22539000c9fb75ca536bc71f59e0476d4ce99c5236c1752d793e6a25fd428f996aae6d9b8cc544f1852f213f7481020d9ff6a8c582369ae26b75aa2dfa7b
6
+ metadata.gz: 42adc9e66943d44b12b9c277042dadaa0cc6e17713962cf6feb2ecc623806e1e7ff19e0bc6265191bd33d552175d3490af9855b152ddba5e70e870f27a6ed391
7
+ data.tar.gz: 4f6beef17b91cac08e5a2c89d4e5e4cc26a91b90217e82c026e84dec99a99f0211902fdff761dbe801b837c9ae019bc01d959f3883537a7440370fabc2f44d3a
@@ -1,6 +1,14 @@
1
1
 
2
2
  # CHANGELOG
3
3
 
4
+ # v3.0.0
5
+
6
+ * [BREAKING] Renamed `Oauth2ApiClient::HttpError` to
7
+ `Oauth2ApiClient::ResponseError`
8
+ * Added http error exception classes like e.g.
9
+ `Oauth2ApiClient::ResponseError::NotFound`,
10
+ `Oauth2ApiClient::ResponseError::InternalServerError`, etc.
11
+
4
12
  # v2.1.0
5
13
 
6
14
  * Include the response code and body in `HttpError#to_s`
data/README.md CHANGED
@@ -33,8 +33,11 @@ client = Oauth2ApiClient.new(
33
33
  )
34
34
  ```
35
35
 
36
- Please note, `get`, `post`, `put`, etc. will raise `Oauth2ApiClient::HttpError`
37
- unless the response code is 2xx.
36
+ Please note, `get`, `post`, `put`, etc. will raise
37
+ `Oauth2ApiClient::ResponseError` unless the response code is 2xx. More
38
+ specifically, it will e.g. raise `Oauth2ApiClient::ResponseError::NotFound` for
39
+ a 404 status code, `Oauth2ApiClient::ResponseError::InternalServerError` for a
40
+ 500 status code, etc.
38
41
 
39
42
  ## Install
40
43
 
@@ -5,7 +5,7 @@ require "ruby2_keywords"
5
5
  require "active_support"
6
6
 
7
7
  require "oauth2_api_client/version"
8
- require "oauth2_api_client/http_error"
8
+ require "oauth2_api_client/response_error"
9
9
  require "oauth2_api_client/token_provider"
10
10
 
11
11
  # The Oauth2ApiClient class is a client wrapped around the oauth2 and http-rb
@@ -79,7 +79,7 @@ class Oauth2ApiClient
79
79
 
80
80
  return response if response.status.success?
81
81
 
82
- raise HttpError, response
82
+ raise ResponseError.for(response)
83
83
  end
84
84
  end
85
85
 
@@ -88,7 +88,7 @@ class Oauth2ApiClient
88
88
 
89
89
  begin
90
90
  yield
91
- rescue HttpError => e
91
+ rescue ResponseError => e
92
92
  if !retried && e.response.status.unauthorized? && @token.respond_to?(:invalidate_token)
93
93
  @token.invalidate_token
94
94
 
@@ -0,0 +1,98 @@
1
+ class Oauth2ApiClient
2
+ # The ResponseError class is the main exception class of Oauth2ApiClient and is
3
+ # raised when a request fails with some status code other than 2xx. Using
4
+ # the exception object, you still have access to the response. Moreover,
5
+ # there are exception classes for all 4xx and 5xx errors.
6
+ #
7
+ # @example
8
+ # begin
9
+ # client.post("/orders", json: { address: "..." })
10
+ # rescue Oauth2ApiClient::ResponseError::NotFound => e
11
+ # e.response # => HTTP::Response
12
+ # rescue Oauth2ApiClient::ResponseError::BadRequest => e
13
+ # # ...
14
+ # rescue Oauth2ApiClient::ResponseError => e
15
+ # # ...
16
+ # end
17
+
18
+ class ResponseError < StandardError
19
+ STATUSES = {
20
+ 400 => "Bad Request",
21
+ 401 => "Unauthorized",
22
+ 402 => "Payment Required",
23
+ 403 => "Forbidden",
24
+ 404 => "Not Found",
25
+ 405 => "Method Not Allowed",
26
+ 406 => "Not Acceptable",
27
+ 407 => "Proxy Authentication Required",
28
+ 408 => "Request Timeout",
29
+ 409 => "Conflict",
30
+ 410 => "Gone",
31
+ 411 => "Length Required",
32
+ 412 => "Precondition Failed",
33
+ 413 => "Payload Too Large",
34
+ 414 => "URI Too Long",
35
+ 415 => "Unsupported Media Type",
36
+ 416 => "Range Not Satisfiable",
37
+ 417 => "Expectation Failed",
38
+ 418 => "I'm A Teapot",
39
+ 421 => "Too Many Connections From This IP",
40
+ 422 => "Unprocessable Entity",
41
+ 423 => "Locked",
42
+ 424 => "Failed Dependency",
43
+ 425 => "Unordered Collection",
44
+ 426 => "Upgrade Required",
45
+ 428 => "Precondition Required",
46
+ 429 => "Too Many Requests",
47
+ 431 => "Request Header Fields Too Large",
48
+ 449 => "Retry With",
49
+ 450 => "Blocked By Windows Parental Controls",
50
+
51
+ 500 => "Internal Server Error",
52
+ 501 => "Not Implemented",
53
+ 502 => "Bad Gateway",
54
+ 503 => "Service Unavailable",
55
+ 504 => "Gateway Timeout",
56
+ 505 => "HTTP Version Not Supported",
57
+ 506 => "Variant Also Negotiates",
58
+ 507 => "Insufficient Storage",
59
+ 508 => "Loop Detected",
60
+ 509 => "Bandwidth Limit Exceeded",
61
+ 510 => "Not Extended",
62
+ 511 => "Network Authentication Required"
63
+ }
64
+
65
+ attr_reader :response
66
+
67
+ def initialize(response)
68
+ @response = response
69
+ end
70
+
71
+ def to_s
72
+ "#{self.class.name} (#{response.code}): #{response.body}"
73
+ end
74
+
75
+ # @api private
76
+ #
77
+ # Returns the exception class for a status code of the given response.
78
+
79
+ def self.for(response)
80
+ return const_get(const_name(STATUSES[response.code])).new(response) if STATUSES.key?(response.code)
81
+
82
+ new(response)
83
+ end
84
+
85
+ # @api private
86
+ #
87
+ # Returns a sanitized version to be used as a constant name for a given
88
+ # http error message.
89
+
90
+ def self.const_name(message)
91
+ message.gsub(/[^a-zA-Z0-9]/, "")
92
+ end
93
+
94
+ STATUSES.each do |_code, message|
95
+ const_set(const_name(message), Class.new(self))
96
+ end
97
+ end
98
+ end
@@ -1,3 +1,3 @@
1
1
  class Oauth2ApiClient
2
- VERSION = "2.1.0"
2
+ VERSION = "3.0.0"
3
3
  end
@@ -0,0 +1,20 @@
1
+ require File.expand_path("../spec_helper", __dir__)
2
+
3
+ RSpec.describe Oauth2ApiClient::ResponseError do
4
+ describe "#to_s" do
5
+ it "returns the message" do
6
+ response = double(code: 401, body: "unauthorized")
7
+
8
+ expect(described_class.new(response).to_s).to eq("Oauth2ApiClient::ResponseError (401): unauthorized")
9
+ end
10
+ end
11
+
12
+ describe ".for" do
13
+ it "returns the exception class for the status code of the given response" do
14
+ expect(described_class.for(double(code: 400, body: "body"))).to be_instance_of(Oauth2ApiClient::ResponseError::BadRequest)
15
+ expect(described_class.for(double(code: 404, body: "body"))).to be_instance_of(Oauth2ApiClient::ResponseError::NotFound)
16
+ expect(described_class.for(double(code: 500, body: "body"))).to be_instance_of(Oauth2ApiClient::ResponseError::InternalServerError)
17
+ expect(described_class.for(double(code: 503, body: "body"))).to be_instance_of(Oauth2ApiClient::ResponseError::ServiceUnavailable)
18
+ end
19
+ end
20
+ end
@@ -123,7 +123,7 @@ RSpec.describe Oauth2ApiClient do
123
123
  )
124
124
  )
125
125
 
126
- expect { client.get("/path") }.to raise_error("Oauth2ApiClient::HttpError (401): unauthorized")
126
+ expect { client.get("/path") }.to raise_error("Oauth2ApiClient::ResponseError::Unauthorized (401): unauthorized")
127
127
  end
128
128
  end
129
129
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oauth2_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Vetter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-17 00:00:00.000000000 Z
11
+ date: 2020-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -153,11 +153,11 @@ files:
153
153
  - README.md
154
154
  - Rakefile
155
155
  - lib/oauth2_api_client.rb
156
- - lib/oauth2_api_client/http_error.rb
156
+ - lib/oauth2_api_client/response_error.rb
157
157
  - lib/oauth2_api_client/token_provider.rb
158
158
  - lib/oauth2_api_client/version.rb
159
159
  - oauth2_api_client.gemspec
160
- - spec/oauth2_api_client/http_error_spec.rb
160
+ - spec/oauth2_api_client/response_error_spec.rb
161
161
  - spec/oauth2_api_client/token_provider_spec.rb
162
162
  - spec/oauth2_api_client_spec.rb
163
163
  - spec/spec_helper.rb
@@ -185,7 +185,7 @@ signing_key:
185
185
  specification_version: 4
186
186
  summary: Small but powerful client around oauth2 and http-rb to interact with APIs
187
187
  test_files:
188
- - spec/oauth2_api_client/http_error_spec.rb
188
+ - spec/oauth2_api_client/response_error_spec.rb
189
189
  - spec/oauth2_api_client/token_provider_spec.rb
190
190
  - spec/oauth2_api_client_spec.rb
191
191
  - spec/spec_helper.rb
@@ -1,24 +0,0 @@
1
- class Oauth2ApiClient
2
- # The HttpError class is the main exception class of Oauth2ApiClient and is
3
- # raised when a request fails with some status code other than 2xx. Using
4
- # the exception object, you still have access to the response.
5
- #
6
- # @example
7
- # begin
8
- # client.post("/orders", json: { address: "..." })
9
- # rescue Oauth2ApiClient::HttpError => e
10
- # e.response # => HTTP::Response
11
- # end
12
-
13
- class HttpError < StandardError
14
- attr_reader :response
15
-
16
- def initialize(response)
17
- @response = response
18
- end
19
-
20
- def to_s
21
- "#{self.class.name} (#{response.code}): #{response.body}"
22
- end
23
- end
24
- end
@@ -1,11 +0,0 @@
1
- require File.expand_path("../spec_helper", __dir__)
2
-
3
- RSpec.describe Oauth2ApiClient::HttpError do
4
- describe "#to_s" do
5
- it "returns the message" do
6
- response = double(code: 401, body: "unauthorized")
7
-
8
- expect(described_class.new(response).to_s).to eq("Oauth2ApiClient::HttpError (401): unauthorized")
9
- end
10
- end
11
- end