artirix_data_models 0.16.0 → 0.17.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
  SHA1:
3
- metadata.gz: ad3a2bfa1f8645d66fa869c8f181c0967c04e6e6
4
- data.tar.gz: 8d35e9ea6f56f73461d0a3ccb77682a663507302
3
+ metadata.gz: 40b6f9f87c468b62e4110b789710aa52c26733a2
4
+ data.tar.gz: 2a2099654a9c5b6c93d78c3ea5b1e614353ad07e
5
5
  SHA512:
6
- metadata.gz: 9b326d4c9a5461c59db3b7d1dd76e9d1ddbdbd85bf93e024dfa1181e0ef1f8024c7688a6d114a6b6754586b62c525e5c1c1b2ffc516d2f3b15013e6a71bd2580
7
- data.tar.gz: dee1cffa2dfc3d207cb62c3501d43d3b46b43900ab0f1cecc341842e6ac92041fe29e8766aaca97a381fe1af56571821a0294322dce69da7e8fe33b935452562
6
+ metadata.gz: 287d064bc3dd3579a378de1b229ae910033c171877031a3407490b93d747cb6a75006897444bd96413accc451be836defade734d6849958fe614a5d57d255cd9
7
+ data.tar.gz: 33fdb76a7ae986cceeccd6d03a21558109b57c815060d208c899a72f94cc10f5c66df701570d2e16a9641f83a3a80f2c5a02a4d3ad927ccc2728ef4681bf880b
data/README.md CHANGED
@@ -270,6 +270,32 @@ end
270
270
 
271
271
  ## Changes
272
272
 
273
+ ### 0.17.0
274
+
275
+ `DataGateway` now has `authorization_bearer` and `authorization_token_hash` options:
276
+ - they can be passed on the gateway creation and they will be used on all elements
277
+ - they can be overridden on a given gateway call:
278
+ -- if passed `nil` it will use the value on object creation, if present.
279
+ -- if passed `false` it will not use it (can override a value on object creation).
280
+
281
+ The values can also be added on config to the connection (but then the `false` override won't work). The authorization will be set on the connection level instead on the request level.
282
+
283
+ ```ruby
284
+ SimpleConfig.for(:site) do
285
+ group :data_gateway do
286
+ set :token_hash, { email: 'something', token: 'whatever }
287
+ end
288
+ end
289
+ ```
290
+
291
+ ```ruby
292
+ SimpleConfig.for(:site) do
293
+ group :data_gateway do
294
+ set :bearer_token, 'SomeBearerToken'
295
+ end
296
+ end
297
+ ```
298
+
273
299
  ### 0.16.0
274
300
  `ArtirixDataModels::Model::CacheKey` now does not assume that you are in a complete model. It tries to use `model_dao_name`, `primary_key`, `id`, `_timestamp` and `updated_at`, but it has default for each section. Change to be able to make a model with `OnlyData` compatible with `AMS` using [`artirix_data_models-ams`](https://github.com/artirix/artirix_data_models-ams/) gem
275
301
 
@@ -1,9 +1,16 @@
1
1
  class ArtirixDataModels::DataGateway
2
2
  attr_reader :connection, :post_as_json
3
3
 
4
- def initialize(connection: nil, post_as_json: true)
5
- @connection = connection || DefaultConnectionLoader.default_connection
6
- @post_as_json = !!post_as_json
4
+ def initialize(connection: nil,
5
+ post_as_json: true,
6
+ timeout: nil,
7
+ authorization_bearer: nil,
8
+ authorization_token_hash: nil)
9
+ @connection = connection || DefaultConnectionLoader.default_connection
10
+ @post_as_json = !!post_as_json
11
+ @authorization_bearer = authorization_bearer
12
+ @authorization_token_hash = authorization_token_hash
13
+ @timeout = timeout
7
14
  end
8
15
 
9
16
  def get(path, **opts)
@@ -22,13 +29,40 @@ class ArtirixDataModels::DataGateway
22
29
  call :delete, path, **opts
23
30
  end
24
31
 
25
- def call(method, path, json_body: true, response_adaptor: nil, body: nil, fake: false, fake_response: nil, cache_adaptor: nil, timeout: nil, **_ignored_options)
32
+ def call(method,
33
+ path,
34
+ json_body: true,
35
+ response_adaptor: nil,
36
+ body: nil,
37
+ fake: false,
38
+ fake_response: nil,
39
+ cache_adaptor: nil,
40
+ timeout: nil,
41
+ authorization_bearer: nil,
42
+ authorization_token_hash: nil,
43
+ **_ignored_options)
26
44
  if fake
27
45
  result = fake_response.respond_to?(:call) ? fake_response.call : fake_response
46
+
28
47
  elsif cache_adaptor.present?
29
- result = cache_adaptor.call { perform(method, path: path, body: body, json_body: json_body, timeout: timeout) }
48
+ result = cache_adaptor.call do
49
+ perform method,
50
+ path: path,
51
+ body: body,
52
+ json_body: json_body,
53
+ timeout: timeout,
54
+ authorization_bearer: authorization_bearer,
55
+ authorization_token_hash: authorization_token_hash
56
+ end
57
+
30
58
  else
31
- result = perform(method, path: path, body: body, json_body: json_body, timeout: timeout)
59
+ result = perform method,
60
+ path: path,
61
+ body: body,
62
+ json_body: json_body,
63
+ timeout: timeout,
64
+ authorization_bearer: authorization_bearer,
65
+ authorization_token_hash: authorization_token_hash
32
66
  end
33
67
 
34
68
  parse_response result: result,
@@ -55,15 +89,47 @@ class ArtirixDataModels::DataGateway
55
89
  perform :delete, path: path, **opts
56
90
  end
57
91
 
58
- def perform(method, path:, body: nil, json_body: true, timeout: nil)
59
- response = connect(method, path: path, body: body, json_body: json_body, timeout: timeout)
92
+ def perform(method,
93
+ path:,
94
+ body: nil,
95
+ json_body: true,
96
+ timeout: nil,
97
+ authorization_bearer: nil,
98
+ authorization_token_hash: nil)
99
+
100
+ pars = {
101
+ path: path,
102
+ body: body,
103
+ json_body: json_body,
104
+ timeout: timeout,
105
+ authorization_bearer: authorization_bearer,
106
+ authorization_token_hash: authorization_token_hash
107
+ }
108
+
109
+ response = connect(method, pars)
60
110
  treat_response(response, method, path)
61
111
  end
62
112
 
63
- def connect(method, path:, body: nil, json_body: true, timeout: nil)
113
+ # for options `timeout`, `authorization_bearer` and `authorization_token_hash`:
114
+ # if `nil` is passed (or param is omitted) it will try to use the default passed on the gateway creation
115
+ # but if `false` is passed, it will stay as false (can be used to override a default option passed on gateway creation)
116
+ def connect(method,
117
+ path:,
118
+ body: nil,
119
+ json_body: true,
120
+ timeout: nil,
121
+ authorization_bearer: nil,
122
+ authorization_token_hash: nil)
123
+
124
+ timeout = timeout.nil? ? @timeout : timeout
125
+ authorization_bearer = authorization_bearer.nil? ? @authorization_bearer : authorization_bearer
126
+ authorization_token_hash = authorization_token_hash.nil? ? @authorization_token_hash : authorization_token_hash
127
+
64
128
  connection.send(method, path) do |req|
65
129
 
66
- req.options.timeout = timeout if timeout.present?
130
+ req.options.timeout = timeout if timeout.present?
131
+ req.headers['Authorization'] = Faraday::Request::Authorization.header(:Bearer, authorization_bearer) if authorization_bearer.present?
132
+ req.headers['Authorization'] = Faraday::Request::Authorization.header(:Token, authorization_token_hash) if authorization_token_hash.present?
67
133
 
68
134
  unless body.nil?
69
135
  if json_body
@@ -172,7 +238,15 @@ class ArtirixDataModels::DataGateway
172
238
  Faraday.new(url: url, request: { params_encoder: Faraday::FlatParamsEncoder }) do |faraday|
173
239
  faraday.request :url_encoded # form-encode POST params
174
240
  faraday.response :logger # log requests to STDOUT
175
- faraday.basic_auth(config.login, config.password) if basic_auth?
241
+
242
+ if basic_auth?
243
+ faraday.basic_auth(config.login, config.password)
244
+ elsif bearer_auth?
245
+ faraday.authorization :Bearer, config.bearer_token
246
+ elsif token_auth?
247
+ faraday.authorization :Token, config.token_hash
248
+ end
249
+
176
250
  faraday.adapter Faraday.default_adapter
177
251
  end
178
252
  end
@@ -190,6 +264,14 @@ class ArtirixDataModels::DataGateway
190
264
  def basic_auth?
191
265
  config.respond_to?(:login) && config.respond_to?(:password)
192
266
  end
267
+
268
+ def bearer_auth?
269
+ config.respond_to?(:bearer_token) && config.bearer_token.present?
270
+ end
271
+
272
+ def token_auth?
273
+ config.respond_to?(:token_hash) && config.token_hash.present?
274
+ end
193
275
  end
194
276
 
195
277
  end
@@ -12,25 +12,61 @@ def given_gateway_config(connection_url = nil)
12
12
  end
13
13
 
14
14
 
15
- def mock_gateway_response(response:, method:, path:, body: nil, json_body: true, timeout: nil, gateway: nil)
15
+ def mock_gateway_response(response:,
16
+ method:,
17
+ path:,
18
+ body: nil,
19
+ json_body: true,
20
+ timeout: nil,
21
+ authorization_bearer: nil,
22
+ authorization_token_hash: nil,
23
+ gateway: nil)
16
24
  gateway ||= ArtirixDataModels::DAORegistry.gateway
17
- allow(gateway).to receive(:perform).with(method, path: path, body: body, json_body: json_body, timeout: timeout).and_return response
25
+
26
+ params_hash = {
27
+ path: path,
28
+ body: body,
29
+ json_body: json_body,
30
+ timeout: timeout,
31
+ authorization_bearer: authorization_bearer,
32
+ authorization_token_hash: authorization_token_hash
33
+ }
34
+
35
+ allow(gateway).to receive(:perform).with(method, params_hash).and_return response
18
36
 
19
37
  # check with body already parsed
20
38
  unless body.nil?
21
39
  body = body.kind_of?(String) ? body : body.to_json
22
- allow(gateway).to receive(:perform).with(method, path: path, body: body, json_body: json_body, timeout: timeout).and_return response
40
+ allow(gateway).to receive(:perform).with(method, params_hash.merge(body: body)).and_return response
23
41
  end
24
42
  end
25
43
 
26
- def mock_gateway_not_found_response(method:, path:, body: nil, json_body: true, timeout: nil, gateway: nil)
44
+ def mock_gateway_not_found_response(method:,
45
+ path:,
46
+ body: nil,
47
+ json_body: true,
48
+ timeout: nil,
49
+ authorization_bearer: nil,
50
+ authorization_token_hash: nil,
51
+ gateway: nil)
52
+
27
53
  gateway ||= ArtirixDataModels::DAORegistry.gateway
28
- allow(gateway).to receive(:perform).with(method, path: path, body: body, json_body: json_body, timeout: timeout).and_raise ArtirixDataModels::DataGateway::NotFound
54
+
55
+ params_hash = {
56
+ path: path,
57
+ body: body,
58
+ json_body: json_body,
59
+ timeout: timeout,
60
+ authorization_bearer: authorization_bearer,
61
+ authorization_token_hash: authorization_token_hash
62
+ }
63
+
64
+ allow(gateway).to receive(:perform).with(method, params_hash).and_raise ArtirixDataModels::DataGateway::NotFound
29
65
 
30
66
  # check with body already parsed
31
67
  unless body.nil?
32
68
  body = body.kind_of?(String) ? body : body.to_json
33
- allow(gateway).to receive(:perform).with(method, path: path, body: body, json_body: json_body, timeout: timeout).and_raise ArtirixDataModels::DataGateway::NotFound
69
+ allow(gateway).to receive(:perform).with(method, params_hash.merge(body: body)).and_raise ArtirixDataModels::DataGateway::NotFound
34
70
  end
35
71
  end
36
72
 
@@ -1,3 +1,3 @@
1
1
  module ArtirixDataModels
2
- VERSION = '0.16.0'
2
+ VERSION = '0.17.0'
3
3
  end
@@ -1,6 +1,15 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe ArtirixDataModels::DataGateway, type: :model do
4
+ Given(:bearer) { 'MyBearerToken' }
5
+ Given(:bearer2) { 'OtherBearerToken' }
6
+
7
+ Given(:token_hash) { { a: '123', b: 'abc' } }
8
+ Given(:token_str) { Faraday::Request::Authorization.build_hash('Token', token_hash) }
9
+
10
+ Given(:token_hash2) { { x: 'some', y: 'whaat' } }
11
+ Given(:token_str2) { Faraday::Request::Authorization.build_hash('Token', token_hash2) }
12
+
4
13
  describe '.new' do
5
14
  context 'without connection' do
6
15
  Given(:connection_url) { 'http://example.com/other' }
@@ -30,6 +39,9 @@ RSpec.describe ArtirixDataModels::DataGateway, type: :model do
30
39
 
31
40
  SimpleConfig.for(:site) do
32
41
  group :data_gateway do
42
+ unset :bearer_token if set? :bearer_token
43
+ unset :token_hash if set? :token_hash
44
+
33
45
  set :url, url
34
46
  set :login, login
35
47
  set :password, password
@@ -41,6 +53,54 @@ RSpec.describe ArtirixDataModels::DataGateway, type: :model do
41
53
  Then { expect(gateway.connection.url_prefix.to_s).to eq(connection_url) }
42
54
  Then { expect(gateway.connection.headers).to have_key('Authorization') }
43
55
  end
56
+
57
+ context 'Bearer auth connection' do
58
+ Given(:connection_url) { 'http://example.com/other' }
59
+
60
+ Given do
61
+ u = connection_url
62
+ b = bearer
63
+
64
+ SimpleConfig.for(:site) do
65
+ group :data_gateway do
66
+ unset :login if set? :login
67
+ unset :password if set? :password
68
+ unset :token_hash if set? :token_hash
69
+
70
+ set :url, u
71
+ set :bearer_token, b
72
+ end
73
+ end
74
+ end
75
+
76
+ When(:gateway) { described_class.new }
77
+ Then { expect(gateway.connection.url_prefix.to_s).to eq(connection_url) }
78
+ Then { expect(gateway.connection.headers['Authorization']).to eq("Bearer #{bearer}") }
79
+ end
80
+
81
+ context 'Token auth connection' do
82
+ Given(:connection_url) { 'http://example.com/other' }
83
+
84
+ Given do
85
+ u = connection_url
86
+ t = token_hash
87
+
88
+ SimpleConfig.for(:site) do
89
+ group :data_gateway do
90
+ unset :login if set? :login
91
+ unset :password if set? :password
92
+ unset :bearer_token if set? :bearer_token
93
+
94
+ set :url, u
95
+ set :token_hash, t
96
+ end
97
+ end
98
+ end
99
+
100
+ When(:gateway) { described_class.new }
101
+ Then { expect(gateway.connection.url_prefix.to_s).to eq(connection_url) }
102
+ Then { expect(gateway.connection.headers['Authorization']).to eq(token_str) }
103
+ end
44
104
  end
45
105
 
46
106
  context 'requests with timeout' do
@@ -66,6 +126,177 @@ RSpec.describe ArtirixDataModels::DataGateway, type: :model do
66
126
  end
67
127
  end
68
128
 
129
+ context 'requests with token based auth' do
130
+ Given(:path) { '/somepath' }
131
+
132
+ Given(:connection_url) { 'http://example.com/other' }
133
+ Given do
134
+ c = connection_url
135
+ SimpleConfig.for(:site) do
136
+ group :data_gateway do
137
+ set :url, c
138
+ end
139
+ end
140
+ end
141
+
142
+ Given(:connection_stubs) { Faraday::Adapter::Test::Stubs.new }
143
+
144
+ Given(:connection) do
145
+ Faraday.new(url: connection_url, request: { params_encoder: Faraday::FlatParamsEncoder }) do |faraday|
146
+ faraday.request :url_encoded # form-encode without body only path params
147
+ faraday.response :logger # log requests to STDOUT
148
+ faraday.adapter :test, connection_stubs
149
+
150
+ faraday.authorization :Bearer, bearer
151
+ end
152
+ end
153
+
154
+ Given(:gateway) do
155
+ described_class.new connection: connection
156
+ end
157
+
158
+ Given do
159
+ # stub gets
160
+ connection_stubs.get(path, headers_bearer) do |env|
161
+ [200, {}, response_body_bearer.to_json]
162
+ end
163
+
164
+ connection_stubs.get(path, headers_bearer2) do |env|
165
+ [200, {}, response_body_bearer2.to_json]
166
+ end
167
+
168
+ connection_stubs.get(path, headers_token) do |env|
169
+ [200, {}, response_body_token.to_json]
170
+ end
171
+
172
+ connection_stubs.get(path, headers_token2) do |env|
173
+ [200, {}, response_body_token2.to_json]
174
+ end
175
+
176
+ connection_stubs.get(path, headers_empty) do |env|
177
+ [200, {}, response_body_empty.to_json]
178
+ end
179
+ end
180
+
181
+ Given(:headers_empty) { {} }
182
+ Given(:headers_bearer) { { 'Authorization' => "Bearer #{bearer}" } }
183
+ Given(:headers_bearer2) { { 'Authorization' => "Bearer #{bearer2}" } }
184
+ Given(:headers_token) { { 'Authorization' => token_str } }
185
+ Given(:headers_token2) { { 'Authorization' => token_str2 } }
186
+
187
+ Given(:response_body_empty) { { status: 'is empty' } }
188
+ Given(:response_body_bearer) { { status: 'with bearer' } }
189
+ Given(:response_body_bearer2) { { status: 'with bearer2' } }
190
+ Given(:response_body_token) { { status: 'with token' } }
191
+ Given(:response_body_token2) { { status: 'with token2' } }
192
+
193
+ context 'Bearer' do
194
+ context 'in config of connection' do
195
+ Given(:connection) do
196
+ Faraday.new(url: connection_url, request: { params_encoder: Faraday::FlatParamsEncoder }) do |faraday|
197
+ faraday.authorization :Bearer, bearer
198
+ faraday.request :url_encoded # form-encode without body only path params
199
+ faraday.response :logger # log requests to STDOUT
200
+ faraday.adapter :test, connection_stubs
201
+ end
202
+ end
203
+
204
+ describe 'normal use (uses the bearer)' do
205
+ Given(:gateway) do
206
+ described_class.new connection: connection
207
+ end
208
+
209
+ When(:result) { gateway.get path }
210
+ Then { result == response_body_bearer }
211
+ end
212
+ end
213
+
214
+ context 'not in config of connection' do
215
+ Given(:connection) do
216
+ Faraday.new(url: connection_url, request: { params_encoder: Faraday::FlatParamsEncoder }) do |faraday|
217
+ faraday.request :url_encoded # form-encode without body only path params
218
+ faraday.response :logger # log requests to STDOUT
219
+ faraday.adapter :test, connection_stubs
220
+ end
221
+ end
222
+
223
+ context 'passing on init' do
224
+ Given(:gateway) do
225
+ described_class.new connection: connection, authorization_bearer: bearer
226
+ end
227
+
228
+ describe 'normal use (uses the bearer)' do
229
+ When(:result) { gateway.get path }
230
+ Then { result == response_body_bearer }
231
+ end
232
+
233
+ describe 'overrides with other bearer (uses the other bearer)' do
234
+ When(:result) { gateway.get path, authorization_bearer: bearer2 }
235
+ Then { result == response_body_bearer2 }
236
+ end
237
+
238
+ describe 'overrides with false (uses no bearer)' do
239
+ When(:result) { gateway.get path, authorization_bearer: false }
240
+ Then { result == response_body_empty }
241
+ end
242
+ end
243
+ end
244
+ end
245
+
246
+ context 'Token' do
247
+ context 'in config of connection' do
248
+ Given(:connection) do
249
+ Faraday.new(url: connection_url, request: { params_encoder: Faraday::FlatParamsEncoder }) do |faraday|
250
+ faraday.authorization :Token, token_hash
251
+ faraday.request :url_encoded # form-encode without body only path params
252
+ faraday.response :logger # log requests to STDOUT
253
+ faraday.adapter :test, connection_stubs
254
+ end
255
+ end
256
+
257
+ describe 'normal use (uses the token)' do
258
+ Given(:gateway) do
259
+ described_class.new connection: connection
260
+ end
261
+
262
+ When(:result) { gateway.get path }
263
+ Then { result == response_body_token }
264
+ end
265
+ end
266
+
267
+ context 'not in config of connection' do
268
+ Given(:connection) do
269
+ Faraday.new(url: connection_url, request: { params_encoder: Faraday::FlatParamsEncoder }) do |faraday|
270
+ faraday.request :url_encoded # form-encode without body only path params
271
+ faraday.response :logger # log requests to STDOUT
272
+ faraday.adapter :test, connection_stubs
273
+ end
274
+ end
275
+
276
+ context 'passing on init' do
277
+ Given(:gateway) do
278
+ described_class.new connection: connection, authorization_token_hash: token_hash
279
+ end
280
+
281
+ describe 'normal use (uses the token)' do
282
+ When(:result) { gateway.get path }
283
+ Then { result == response_body_token }
284
+ end
285
+
286
+ describe 'overrides with other token (uses the other token)' do
287
+ When(:result) { gateway.get path, authorization_token_hash: token_hash2 }
288
+ Then { result == response_body_token2 }
289
+ end
290
+
291
+ describe 'overrides with false (uses no token)' do
292
+ When(:result) { gateway.get path, authorization_token_hash: false }
293
+ Then { result == response_body_empty }
294
+ end
295
+ end
296
+ end
297
+ end
298
+ end
299
+
69
300
  context 'requests' do
70
301
  Given(:connection_url) { 'http://example.com' }
71
302
  Given(:connection_stubs) { Faraday::Adapter::Test::Stubs.new }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: artirix_data_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.0
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Turiño
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-11 00:00:00.000000000 Z
11
+ date: 2016-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport