oauth2_api_client 3.1.0 → 3.2.1
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 +4 -4
- data/.github/workflows/test.yml +21 -0
- data/.rubocop.yml +6 -0
- data/CHANGELOG.md +12 -0
- data/README.md +9 -2
- data/lib/oauth2_api_client/response_error.rb +2 -0
- data/lib/oauth2_api_client/version.rb +1 -1
- data/lib/oauth2_api_client.rb +9 -4
- data/oauth2_api_client.gemspec +1 -2
- data/spec/oauth2_api_client_spec.rb +11 -1
- metadata +10 -14
- data/.travis.yml +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 538d67c5360a1a6a2e2f9ff40c1b87acdaf6fdfe64ba292fbee47bc52c48047f
|
4
|
+
data.tar.gz: b50a7904af84f243bc73d0aebf89a8b00d50dd6ef5e579805b959b9de8161319
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b801d4daec062a26567ba686b923a1051a3d024e5a9ed0ddb954edb093d5c26891b4f071657df2c34284be7d3f8cb38f60ceb277d145e3c7628cd45daa58c227
|
7
|
+
data.tar.gz: 4dd5218f1d770194836800702190dba87b943c80c92f65c0a518bc4ca2c9783f24f7d2bc23968705ec52ec067f6d4127439aa9d367f14a80f95cbe828861f722
|
@@ -0,0 +1,21 @@
|
|
1
|
+
on: push
|
2
|
+
name: test
|
3
|
+
jobs:
|
4
|
+
test:
|
5
|
+
runs-on: ubuntu-latest
|
6
|
+
strategy:
|
7
|
+
fail-fast: false
|
8
|
+
matrix:
|
9
|
+
ruby:
|
10
|
+
- 2.6
|
11
|
+
- 2.7
|
12
|
+
- 3.0
|
13
|
+
steps:
|
14
|
+
- uses: actions/checkout@v1
|
15
|
+
- uses: actions/setup-ruby@v1
|
16
|
+
with:
|
17
|
+
ruby-version: ${{ matrix.ruby }}
|
18
|
+
- run: gem install bundler
|
19
|
+
- run: bundle
|
20
|
+
- run: bundle exec rspec
|
21
|
+
- run: bundle exec rubocop
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,18 @@
|
|
1
1
|
|
2
2
|
# CHANGELOG
|
3
3
|
|
4
|
+
# v3.2.1
|
5
|
+
|
6
|
+
* Fix thread safety issue of http-rb
|
7
|
+
|
8
|
+
# v3.2.0
|
9
|
+
|
10
|
+
* Allow passing `nil` as token for unprotected APIs
|
11
|
+
|
12
|
+
# v3.1.1
|
13
|
+
|
14
|
+
* Added oauth2 version constraint
|
15
|
+
|
4
16
|
# v3.1.0
|
5
17
|
|
6
18
|
* Added uri to `Oauth2ApiClient::ReponseError` exception message
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# oauth2_api_client
|
2
2
|
|
3
|
-
[](https://github.com/mrkamel/oauth2_api_client/actions?query=workflow%3Atest+branch%3Amaster)
|
4
4
|
[](http://badge.fury.io/rb/oauth2_api_client)
|
5
5
|
|
6
6
|
Oauth2ApiClient is a small, but powerful client around
|
@@ -12,10 +12,17 @@ oauth2 for authentication.
|
|
12
12
|
client = Oauth2ApiClient.new(base_url: "https://api.example.com", token "oauth2 token")
|
13
13
|
|
14
14
|
client.post("/orders", json: { address: "..." }).status.success?
|
15
|
-
client.headers("User-Agent" => "API Client").timeout(read: 5, write: 5).get("/orders").parse
|
15
|
+
client.headers("User-Agent" => "API Client").timeout(read: 5, write: 5).get("/orders").parse(:json)
|
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.
|
data/lib/oauth2_api_client.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require "ruby2_keywords"
|
2
2
|
require "oauth2"
|
3
3
|
require "http"
|
4
|
-
require "ruby2_keywords"
|
5
4
|
require "active_support"
|
6
5
|
|
7
6
|
require "oauth2_api_client/version"
|
@@ -28,7 +27,7 @@ class Oauth2ApiClient
|
|
28
27
|
# )
|
29
28
|
#
|
30
29
|
# client.post("/orders", json: { address: "..." }).status.success?
|
31
|
-
# client.headers("User-Agent" => "API Client").timeout(read: 5, write: 5).get("/orders").parse
|
30
|
+
# client.headers("User-Agent" => "API Client").timeout(read: 5, write: 5).get("/orders").parse(:json)
|
32
31
|
#
|
33
32
|
# @example
|
34
33
|
# client = Oauth2ApiClient.new(
|
@@ -41,7 +40,7 @@ class Oauth2ApiClient
|
|
41
40
|
# )
|
42
41
|
# )
|
43
42
|
|
44
|
-
def initialize(base_url:,
|
43
|
+
def initialize(base_url:, token: nil, base_request: HTTP)
|
45
44
|
@base_url = base_url
|
46
45
|
@token = token
|
47
46
|
@request = base_request
|
@@ -52,6 +51,8 @@ class Oauth2ApiClient
|
|
52
51
|
# @return [String] The token
|
53
52
|
|
54
53
|
def token
|
54
|
+
return if @token.nil?
|
55
|
+
|
55
56
|
@token.respond_to?(:to_str) ? @token.to_str : @token.token
|
56
57
|
end
|
57
58
|
|
@@ -75,7 +76,11 @@ class Oauth2ApiClient
|
|
75
76
|
|
76
77
|
def execute(verb, path, options = {})
|
77
78
|
with_retry do
|
78
|
-
|
79
|
+
request = @request
|
80
|
+
request = request.headers({}) # Prevent thread-safety issue of http-rb: https://github.com/httprb/http/issues/558
|
81
|
+
request = request.auth("Bearer #{token}") if token
|
82
|
+
|
83
|
+
response = request.send(verb, "#{@base_url}#{path}", options)
|
79
84
|
|
80
85
|
return response if response.status.success?
|
81
86
|
|
data/oauth2_api_client.gemspec
CHANGED
@@ -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"
|
@@ -26,6 +25,6 @@ Gem::Specification.new do |spec|
|
|
26
25
|
|
27
26
|
spec.add_dependency "activesupport"
|
28
27
|
spec.add_dependency "http"
|
29
|
-
spec.add_dependency "oauth2"
|
28
|
+
spec.add_dependency "oauth2", ">= 1.4.2"
|
30
29
|
spec.add_dependency "ruby2_keywords"
|
31
30
|
end
|
@@ -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
|
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
|
4
|
+
version: 3.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Vetter
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -114,14 +114,14 @@ dependencies:
|
|
114
114
|
requirements:
|
115
115
|
- - ">="
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version:
|
117
|
+
version: 1.4.2
|
118
118
|
type: :runtime
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
122
|
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version:
|
124
|
+
version: 1.4.2
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: ruby2_keywords
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -144,9 +144,9 @@ executables: []
|
|
144
144
|
extensions: []
|
145
145
|
extra_rdoc_files: []
|
146
146
|
files:
|
147
|
+
- ".github/workflows/test.yml"
|
147
148
|
- ".gitignore"
|
148
149
|
- ".rubocop.yml"
|
149
|
-
- ".travis.yml"
|
150
150
|
- CHANGELOG.md
|
151
151
|
- Gemfile
|
152
152
|
- LICENSE.txt
|
@@ -165,7 +165,7 @@ homepage: https://github.com/mrkamel/oauth2_api_client
|
|
165
165
|
licenses:
|
166
166
|
- MIT
|
167
167
|
metadata: {}
|
168
|
-
post_install_message:
|
168
|
+
post_install_message:
|
169
169
|
rdoc_options: []
|
170
170
|
require_paths:
|
171
171
|
- lib
|
@@ -180,12 +180,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
180
180
|
- !ruby/object:Gem::Version
|
181
181
|
version: '0'
|
182
182
|
requirements: []
|
183
|
-
rubygems_version: 3.
|
184
|
-
signing_key:
|
183
|
+
rubygems_version: 3.3.3
|
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: []
|