oauth2_api_client 3.2.1 → 3.3.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: 538d67c5360a1a6a2e2f9ff40c1b87acdaf6fdfe64ba292fbee47bc52c48047f
4
- data.tar.gz: b50a7904af84f243bc73d0aebf89a8b00d50dd6ef5e579805b959b9de8161319
3
+ metadata.gz: b846fd82d5e91d799c60adb483691d92a8ee508e3d602c624f9dea0f7d924af5
4
+ data.tar.gz: 983f980480afb33623d5733ab5cafc019d6e9e2b93048cd5f6a3e9536acd7dc8
5
5
  SHA512:
6
- metadata.gz: b801d4daec062a26567ba686b923a1051a3d024e5a9ed0ddb954edb093d5c26891b4f071657df2c34284be7d3f8cb38f60ceb277d145e3c7628cd45daa58c227
7
- data.tar.gz: 4dd5218f1d770194836800702190dba87b943c80c92f65c0a518bc4ca2c9783f24f7d2bc23968705ec52ec067f6d4127439aa9d367f14a80f95cbe828861f722
6
+ metadata.gz: 457d37f335df145f62464d7d380fbf2506bd8003ef38e2f471073513a38851dbe8ef5ad78d9763065e4bb0fa48f83fff832686705c50005b49f26a92c0c41e41
7
+ data.tar.gz: 4b8f9a70d1afc7de2890196d34bef86e1006dae031ea60b7cef075442281bad8f3a3156bbb33fece19de67fcff404fbfdd834518388ac37f315daba4e7ea4b27
data/CHANGELOG.md CHANGED
@@ -1,6 +1,10 @@
1
1
 
2
2
  # CHANGELOG
3
3
 
4
+ # v3.3.0
5
+
6
+ * Add Oauth2ApiClient#params to set default query params
7
+
4
8
  # v3.2.1
5
9
 
6
10
  * Fix thread safety issue of http-rb
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.3.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|
@@ -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
 
@@ -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
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.2.1
4
+ version: 3.3.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: 2022-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler