oauth2_api_client 3.1.1 → 3.2.0

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: 33e0e15f43fe027760355c1a403da0d02b30412b064984102234744fc8da65b7
4
- data.tar.gz: bf10a1428e0a797daa31f32b7712d2525eddab710c2ba12d244e95b968489314
3
+ metadata.gz: 61d5d7602f4fd19918ac64b34d46dbf636d149da28605a296a284d48b9f46506
4
+ data.tar.gz: 90febed5c60202e3406988c77fdb1ecb103adbf4b0216b4452c10639f0128ffe
5
5
  SHA512:
6
- metadata.gz: 46dbbbd595379cc1217f272247149d0ce8084651704c2264301e28dcc8ece22b2c8cc2b9b108d38140eaf1f46145cfde1fb600e24a5434f00a013948e79b22c3
7
- data.tar.gz: d3b38d88ac90e9550971f5320b5cca814be87ade495a8bd999047faebd310f41beecbe45ac16e35fd2ba1e54bbea7b443f13f670e1c119e3a08931b6ea7476ef
6
+ metadata.gz: a5385487519625a1c65afa4c6c208a779f1373ba76d5695b07338cdcb5bc160ef4141fed85d9ba9364f6c0d5a7f605cc351db4eb7a57bb3d41cbad8cd1343dbd
7
+ data.tar.gz: b63e215e39f98570b2604dc4352505a359e307355755aa11f8ea823953c6f083060072958895c0d847ff082d7ec460338219236b3f4abcc3829ce918ece1de37
data/CHANGELOG.md CHANGED
@@ -1,6 +1,10 @@
1
1
 
2
2
  # CHANGELOG
3
3
 
4
+ # v3.2.0
5
+
6
+ * Allow passing `nil` as token for unprotected APIs
7
+
4
8
  # v3.1.1
5
9
 
6
10
  * Added oauth2 version constraint
data/README.md CHANGED
@@ -16,6 +16,13 @@ client.headers("User-Agent" => "API Client").timeout(read: 5, write: 5).get("/or
16
16
  # ...
17
17
  ```
18
18
 
19
+ In case an API is unprotected and you still want to use Oauth2ApiClient, you
20
+ can simply not pass any token:
21
+
22
+ ```ruby
23
+ client = Oauth2ApiClient.new(base_url: "...")
24
+ ```
25
+
19
26
  Oauth2ApiClient is capable of generating oauth2 tokens, when a client id,
20
27
  client secret and oauth token url is given with automatic token caching and
21
28
  renewal on expiry, including retry of the current request.
@@ -1,3 +1,3 @@
1
1
  class Oauth2ApiClient
2
- VERSION = "3.1.1"
2
+ VERSION = "3.2.0"
3
3
  end
@@ -40,7 +40,7 @@ class Oauth2ApiClient
40
40
  # )
41
41
  # )
42
42
 
43
- def initialize(base_url:, token:, base_request: HTTP)
43
+ def initialize(base_url:, token: nil, base_request: HTTP)
44
44
  @base_url = base_url
45
45
  @token = token
46
46
  @request = base_request
@@ -51,6 +51,8 @@ class Oauth2ApiClient
51
51
  # @return [String] The token
52
52
 
53
53
  def token
54
+ return if @token.nil?
55
+
54
56
  @token.respond_to?(:to_str) ? @token.to_str : @token.token
55
57
  end
56
58
 
@@ -74,7 +76,10 @@ class Oauth2ApiClient
74
76
 
75
77
  def execute(verb, path, options = {})
76
78
  with_retry do
77
- response = @request.auth("Bearer #{token}").send(verb, "#{@base_url}#{path}", options)
79
+ request = @request
80
+ request = request.auth("Bearer #{token}") if token
81
+
82
+ response = request.send(verb, "#{@base_url}#{path}", options)
78
83
 
79
84
  return response if response.status.success?
80
85
 
@@ -15,7 +15,6 @@ Gem::Specification.new do |spec|
15
15
 
16
16
  spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
18
  spec.require_paths = ["lib"]
20
19
 
21
20
  spec.add_development_dependency "bundler"
@@ -84,7 +84,7 @@ RSpec.describe Oauth2ApiClient do
84
84
  expect(client.get("/path", params: { key: "value" }).to_s).to eq("ok")
85
85
  end
86
86
 
87
- it "passes the token in the authentication header" do
87
+ it "passes the token in the authorization header" do
88
88
  stub_request(:get, "http://localhost/api/path")
89
89
  .with(headers: { "Authorization" => "Bearer access_token" })
90
90
  .to_return(status: 200, body: "ok", headers: {})
@@ -94,6 +94,16 @@ RSpec.describe Oauth2ApiClient do
94
94
  expect(client.get("/path").to_s).to eq("ok")
95
95
  end
96
96
 
97
+ it "does not pass any authorization header when no token is provided" do
98
+ stub_request(:get, "http://localhost/api/path")
99
+ .with { |request| !request.headers.keys.map(&:to_s).map(&:downcase).include?("authorization") }
100
+ .to_return(status: 200, body: "ok", headers: {})
101
+
102
+ client = described_class.new(base_url: "http://localhost/api")
103
+
104
+ expect(client.get("/path").to_s).to eq("ok")
105
+ end
106
+
97
107
  it "retries the request when an http unauthorized status is returned" do
98
108
  stub_request(:get, "http://localhost/api/path")
99
109
  .to_return({ status: 401, body: "unauthorized" }, { status: 200, body: "ok" })
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: 3.1.1
4
+ version: 3.2.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: 2022-01-17 00:00:00.000000000 Z
11
+ date: 2022-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -184,8 +184,4 @@ rubygems_version: 3.3.3
184
184
  signing_key:
185
185
  specification_version: 4
186
186
  summary: Small but powerful client around oauth2 and http-rb to interact with APIs
187
- test_files:
188
- - spec/oauth2_api_client/response_error_spec.rb
189
- - spec/oauth2_api_client/token_provider_spec.rb
190
- - spec/oauth2_api_client_spec.rb
191
- - spec/spec_helper.rb
187
+ test_files: []