oauth2_api_client 3.2.1 → 3.4.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: 538d67c5360a1a6a2e2f9ff40c1b87acdaf6fdfe64ba292fbee47bc52c48047f
4
- data.tar.gz: b50a7904af84f243bc73d0aebf89a8b00d50dd6ef5e579805b959b9de8161319
3
+ metadata.gz: bf92b6840d7440d512d8d0b7586acfe9751c38e6c99e4ab9f83cd9893cf9f13e
4
+ data.tar.gz: 2e79b922b01af914adef3bacc2f415af62d26e3c8ad95b699daa63147842410a
5
5
  SHA512:
6
- metadata.gz: b801d4daec062a26567ba686b923a1051a3d024e5a9ed0ddb954edb093d5c26891b4f071657df2c34284be7d3f8cb38f60ceb277d145e3c7628cd45daa58c227
7
- data.tar.gz: 4dd5218f1d770194836800702190dba87b943c80c92f65c0a518bc4ca2c9783f24f7d2bc23968705ec52ec067f6d4127439aa9d367f14a80f95cbe828861f722
6
+ metadata.gz: 0b782f564cde4d121868f274946df14af27ab13d7e1d501367451b571ada534696027c8ae51e8705315ad35a5a43b04d800d3209a6c70a6eaedf0a1a5f077ca1
7
+ data.tar.gz: f2c3a469a6a189c6a0c986182ab2a53eb268a851882406d1d7de00cc32556f5a7ebb9e9dcc6732fd370ff9ce905a90a714ddea0649e33ada8eeea2dcb7a74be7
@@ -7,12 +7,12 @@ jobs:
7
7
  fail-fast: false
8
8
  matrix:
9
9
  ruby:
10
- - 2.6
11
- - 2.7
12
- - 3.0
10
+ - "2.7"
11
+ - "3.0"
12
+ - "3.1"
13
13
  steps:
14
14
  - uses: actions/checkout@v1
15
- - uses: actions/setup-ruby@v1
15
+ - uses: ruby/setup-ruby@v1
16
16
  with:
17
17
  ruby-version: ${{ matrix.ruby }}
18
18
  - run: gem install bundler
data/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
1
 
2
2
  # CHANGELOG
3
3
 
4
+ # v3.4.0
5
+
6
+ * Add PATCH method
7
+
8
+ # v3.3.0
9
+
10
+ * Add Oauth2ApiClient#params to set default query params
11
+
4
12
  # v3.2.1
5
13
 
6
14
  * Fix thread safety issue of http-rb
data/Gemfile CHANGED
@@ -1,3 +1,9 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
+
5
+ gem "bundler"
6
+ gem "rspec"
7
+ gem "rubocop"
8
+ gem "timecop"
9
+ gem "webmock"
data/README.md CHANGED
@@ -46,6 +46,17 @@ specifically, it will e.g. raise `Oauth2ApiClient::ResponseError::NotFound` for
46
46
  a 404 status code, `Oauth2ApiClient::ResponseError::InternalServerError` for a
47
47
  500 status code, etc.
48
48
 
49
+ ## Default query params
50
+
51
+ In addition to the DSL of http-rb Oauth2ApiClient allows to set default query
52
+ params, which can be useful for some APIs:
53
+
54
+ ```ruby
55
+ client = Oauth2ApiClient.new(base_url: "https://api.example.com").params(key1: "value1")
56
+ client.get("/path", params: { key2: "value" })
57
+ #=> GET https://api.example.com/path?key1=value1&key2=value2
58
+ ```
59
+
49
60
  ## Install
50
61
 
51
62
  Add this line to your application's Gemfile:
@@ -1,3 +1,3 @@
1
1
  class Oauth2ApiClient
2
- VERSION = "3.2.1"
2
+ VERSION = "3.4.0"
3
3
  end
@@ -56,6 +56,12 @@ class Oauth2ApiClient
56
56
  @token.respond_to?(:to_str) ? @token.to_str : @token.token
57
57
  end
58
58
 
59
+ def params(parms = {})
60
+ dup.tap do |client|
61
+ client.instance_variable_set(:@params, (@params || {}).merge(parms))
62
+ end
63
+ end
64
+
59
65
  [:timeout, :headers, :cookies, :via, :encoding, :accept, :auth, :basic_auth].each do |method|
60
66
  define_method method do |*args|
61
67
  dup.tap do |client|
@@ -66,7 +72,7 @@ class Oauth2ApiClient
66
72
  ruby2_keywords method
67
73
  end
68
74
 
69
- [:get, :post, :put, :delete, :head, :options].each do |method|
75
+ [:get, :post, :put, :patch, :delete, :head, :options].each do |method|
70
76
  define_method method do |path, options = {}|
71
77
  execute(method, path, options)
72
78
  end
@@ -80,7 +86,10 @@ class Oauth2ApiClient
80
86
  request = request.headers({}) # Prevent thread-safety issue of http-rb: https://github.com/httprb/http/issues/558
81
87
  request = request.auth("Bearer #{token}") if token
82
88
 
83
- response = request.send(verb, "#{@base_url}#{path}", options)
89
+ opts = options.dup
90
+ opts[:params] = @params.merge(opts.fetch(:params, {})) if @params
91
+
92
+ response = request.send(verb, "#{@base_url}#{path}", opts)
84
93
 
85
94
  return response if response.status.success?
86
95
 
@@ -17,12 +17,6 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_development_dependency "bundler"
21
- spec.add_development_dependency "rspec"
22
- spec.add_development_dependency "rubocop"
23
- spec.add_development_dependency "timecop"
24
- spec.add_development_dependency "webmock"
25
-
26
20
  spec.add_dependency "activesupport"
27
21
  spec.add_dependency "http"
28
22
  spec.add_dependency "oauth2", ">= 1.4.2"
@@ -51,6 +51,62 @@ RSpec.describe Oauth2ApiClient do
51
51
  end
52
52
  end
53
53
 
54
+ describe "#params" do
55
+ it "creates a dupped instance" do
56
+ client = described_class.new(base_url: "http://localhost")
57
+
58
+ client1 = client.params(key1: "value1")
59
+ client2 = client1.params(key2: "value2")
60
+
61
+ expect(client1.object_id).not_to eq(client2.object_id)
62
+ end
63
+
64
+ it "merges the params" do
65
+ client = described_class.new(base_url: "http://localhost")
66
+
67
+ client1 = client.params(key1: "value1")
68
+ client2 = client1.params(key2: "value2")
69
+
70
+ expect(client2.instance_variable_get(:@params)).to eq(key1: "value1", key2: "value2")
71
+ end
72
+
73
+ it "merges the params with passed params in requests" do
74
+ stub_request(:get, "http://localhost/api/path?key1=value1&key2=value2")
75
+ .to_return(status: 200, body: "ok")
76
+
77
+ client = described_class.new(base_url: "http://localhost/api", token: "token").params(key1: "value1")
78
+
79
+ expect(client.get("/path", params: { key2: "value2" }).to_s).to eq("ok")
80
+ end
81
+
82
+ it "overwrites the default params when neccessary" do
83
+ stub_request(:get, "http://localhost/api/path?key=value2")
84
+ .to_return(status: 200, body: "ok")
85
+
86
+ client = described_class.new(base_url: "http://localhost/api", token: "token").params(key: "value1")
87
+
88
+ expect(client.get("/path", params: { key: "value2" }).to_s).to eq("ok")
89
+ end
90
+
91
+ it "passes the default params only when no params are given" do
92
+ stub_request(:get, "http://localhost/api/path?key=value")
93
+ .to_return(status: 200, body: "ok")
94
+
95
+ client = described_class.new(base_url: "http://localhost/api", token: "token").params(key: "value")
96
+
97
+ expect(client.get("/path").to_s).to eq("ok")
98
+ end
99
+
100
+ it "passes the params only when no default params are given" do
101
+ stub_request(:get, "http://localhost/api/path?key=value")
102
+ .to_return(status: 200, body: "ok")
103
+
104
+ client = described_class.new(base_url: "http://localhost/api", token: "token")
105
+
106
+ expect(client.get("/path", params: { key: "value" }).to_s).to eq("ok")
107
+ end
108
+ end
109
+
54
110
  [:timeout, :headers, :cookies, :via, :encoding, :accept, :auth, :basic_auth].each do |method|
55
111
  describe "##{method}" do
56
112
  it "creates a dupped instance" do
@@ -84,6 +140,19 @@ RSpec.describe Oauth2ApiClient do
84
140
  expect(client.get("/path", params: { key: "value" }).to_s).to eq("ok")
85
141
  end
86
142
 
143
+ [:get, :post, :put, :patch, :delete, :head, :options].each do |method|
144
+ describe "##{method}" do
145
+ it "calls with body" do
146
+ stub_request(method, "http://localhost/api/path").with(body: { key: "value" }.to_json)
147
+ .to_return(status: 200, body: "ok")
148
+
149
+ client = described_class.new(base_url: "http://localhost/api", token: "token")
150
+
151
+ expect(client.send(method, "/path", headers: { "Content-Type" => "application/json" }, body: { key: "value" }.to_json).to_s).to eq("ok")
152
+ end
153
+ end
154
+ end
155
+
87
156
  it "passes the token in the authorization header" do
88
157
  stub_request(:get, "http://localhost/api/path")
89
158
  .with(headers: { "Authorization" => "Bearer access_token" })
metadata CHANGED
@@ -1,85 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oauth2_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.1
4
+ version: 3.4.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-08-19 00:00:00.000000000 Z
11
+ date: 2023-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rspec
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: rubocop
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: timecop
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: webmock
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
13
  - !ruby/object:Gem::Dependency
84
14
  name: activesupport
85
15
  requirement: !ruby/object:Gem::Requirement