quiz_api_client 4.13.4 → 4.14.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/quiz_api_client/http_client.rb +8 -5
- data/lib/quiz_api_client/http_request/metrics.rb +3 -5
- data/lib/quiz_api_client/services/quiz_clone_jobs_service.rb +11 -0
- data/lib/quiz_api_client/version.rb +1 -1
- data/spec/http_client_spec.rb +15 -15
- data/spec/quiz_api_client/http_request/metrics_spec.rb +3 -6
- data/spec/services/quiz_clone_jobs_service_spec.rb +37 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c7df5e01555ce19ad84445f0287c640e0c2eb9873af3c88a6ad1432162ce228a
|
4
|
+
data.tar.gz: 04d758bf5007880147d0e4316b6d151821b904b0a84db0401b7b53b426d6791f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4e95a71d9e8853ae5a3ef251b6df743ee36ce1838be1bcdf7ab6675c6e85a800a5cb4a48a7f394b6094d6b9747ea90c5a1ff2942059991bd6a5f69306185043
|
7
|
+
data.tar.gz: 72c177b35fc78f784c0b26fa49d90c92bfc305539528b357e104864cf339cd870202c97710df4ca672e8d9505b96217be73039b313a4bec7bd244bffbd6c3c15
|
@@ -101,7 +101,7 @@ module QuizApiClient
|
|
101
101
|
rescue HTTParty::Error, Errno::ECONNREFUSED, Net::ReadTimeout => e
|
102
102
|
raise_error(method, url, current_error: e)
|
103
103
|
ensure
|
104
|
-
record_metrics(method,
|
104
|
+
record_metrics(method, resp, start_time, Time.now)
|
105
105
|
end
|
106
106
|
|
107
107
|
def make_paginated_request(method, url, options)
|
@@ -138,7 +138,8 @@ module QuizApiClient
|
|
138
138
|
end
|
139
139
|
|
140
140
|
def next_page_link(response, options)
|
141
|
-
return if response.headers['link'].
|
141
|
+
return if response.headers['link'].nil?
|
142
|
+
return if response.headers['link'].empty?
|
142
143
|
|
143
144
|
links = LinkHeader.parse(response.headers['link']).links
|
144
145
|
next_link = links.find { |link| link['rel'] == 'next' }
|
@@ -148,7 +149,9 @@ module QuizApiClient
|
|
148
149
|
def next_page_dynamo(response, url, options)
|
149
150
|
hash_key = response.headers['x-last-evaluated-hash-key']
|
150
151
|
range_key = response.headers['x-last-evaluated-range-key']
|
151
|
-
|
152
|
+
|
153
|
+
return if hash_key.nil? || hash_key.empty?
|
154
|
+
return if range_key.nil? || range_key.empty?
|
152
155
|
|
153
156
|
query = (options[:query] || {}).merge(
|
154
157
|
last_evaluated_hash_key: hash_key,
|
@@ -163,9 +166,9 @@ module QuizApiClient
|
|
163
166
|
failure.raise_error(method, url, response: response, current_error: current_error)
|
164
167
|
end
|
165
168
|
|
166
|
-
def record_metrics(method,
|
169
|
+
def record_metrics(method, resp, start_time, end_time)
|
167
170
|
code = resp&.code || 0
|
168
|
-
metrics = QuizApiClient::HttpRequest::Metrics.new(config, method,
|
171
|
+
metrics = QuizApiClient::HttpRequest::Metrics.new(config, method, code)
|
169
172
|
|
170
173
|
metrics.increment
|
171
174
|
metrics.duration(start_time, end_time)
|
@@ -3,13 +3,12 @@ module QuizApiClient
|
|
3
3
|
class Metrics
|
4
4
|
extend Forwardable
|
5
5
|
|
6
|
-
attr_reader :config, :method, :
|
6
|
+
attr_reader :config, :method, :code
|
7
7
|
def_delegators :config, :metrics_handler, :metrics_namespace
|
8
8
|
|
9
|
-
def initialize(config, method,
|
9
|
+
def initialize(config, method, code)
|
10
10
|
@config = config
|
11
11
|
@method = method
|
12
|
-
@url = url
|
13
12
|
@code = code
|
14
13
|
end
|
15
14
|
|
@@ -58,8 +57,7 @@ module QuizApiClient
|
|
58
57
|
def tags
|
59
58
|
{
|
60
59
|
method: method,
|
61
|
-
status: code
|
62
|
-
url: url
|
60
|
+
status: code
|
63
61
|
}
|
64
62
|
end
|
65
63
|
end
|
@@ -4,6 +4,10 @@ module QuizApiClient::Services
|
|
4
4
|
post_to_quiz_api(body: body, params: params, token: token)
|
5
5
|
end
|
6
6
|
|
7
|
+
def update(body: {}, params:, token: nil)
|
8
|
+
put_to_quiz_api(body: body, params: params, token: token)
|
9
|
+
end
|
10
|
+
|
7
11
|
def create_batch(body:, token: nil)
|
8
12
|
batch_post_to_quiz_api(body: body, token: token)
|
9
13
|
end
|
@@ -17,6 +21,13 @@ module QuizApiClient::Services
|
|
17
21
|
)
|
18
22
|
end
|
19
23
|
|
24
|
+
def put_to_quiz_api(body:, params:, token:)
|
25
|
+
client(token: token).put(
|
26
|
+
"/api/quiz_clone_jobs/#{params.fetch(:id)}",
|
27
|
+
body
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
20
31
|
def batch_post_to_quiz_api(body:, token:)
|
21
32
|
client(token: token).post(
|
22
33
|
'/api/quiz_clone_jobs/create_batch',
|
data/spec/http_client_spec.rb
CHANGED
@@ -41,9 +41,9 @@ describe QuizApiClient::HttpClient do
|
|
41
41
|
mock
|
42
42
|
end
|
43
43
|
|
44
|
-
def expect_metrics_calls(config, method,
|
44
|
+
def expect_metrics_calls(config, method, code)
|
45
45
|
expect(QuizApiClient::HttpRequest::Metrics).to receive(:new)
|
46
|
-
.with(config, method,
|
46
|
+
.with(config, method, code)
|
47
47
|
.and_return(mock_metrics(mock_time(10)))
|
48
48
|
end
|
49
49
|
|
@@ -65,7 +65,7 @@ describe QuizApiClient::HttpClient do
|
|
65
65
|
it 'makes a get request' do
|
66
66
|
path = '/api/quizzes'
|
67
67
|
stub_quiz_api path, query: { sort: 'alpha' }
|
68
|
-
expect_metrics_calls(client.config, :get,
|
68
|
+
expect_metrics_calls(client.config, :get, 200)
|
69
69
|
response = client.get(path, all: false, query: { sort: 'alpha' })
|
70
70
|
expect(response.parsed_response).to eq [1]
|
71
71
|
end
|
@@ -138,7 +138,7 @@ describe QuizApiClient::HttpClient do
|
|
138
138
|
body: { title: 'ohai' }.to_json,
|
139
139
|
**default_request_data
|
140
140
|
).and_return(instance_double('HTTParty::Response', success?: true, code: 200))
|
141
|
-
expect_metrics_calls(client.config, :post,
|
141
|
+
expect_metrics_calls(client.config, :post, 200)
|
142
142
|
client.post('/api/quizzes', title: 'ohai')
|
143
143
|
end
|
144
144
|
end
|
@@ -151,7 +151,7 @@ describe QuizApiClient::HttpClient do
|
|
151
151
|
body: { title: 'new title' }.to_json,
|
152
152
|
**default_request_data
|
153
153
|
).and_return(instance_double('HTTParty::Response', success?: true, code: 200))
|
154
|
-
expect_metrics_calls(client.config, :patch,
|
154
|
+
expect_metrics_calls(client.config, :patch, 200)
|
155
155
|
client.patch('/api/quizzes/1', title: 'new title')
|
156
156
|
end
|
157
157
|
end
|
@@ -164,7 +164,7 @@ describe QuizApiClient::HttpClient do
|
|
164
164
|
body: { title: 'new title' }.to_json,
|
165
165
|
**default_request_data
|
166
166
|
).and_return(instance_double('HTTParty::Response', success?: true, code: 200))
|
167
|
-
expect_metrics_calls(client.config, :put,
|
167
|
+
expect_metrics_calls(client.config, :put, 200)
|
168
168
|
client.put('/api/quizzes/1', title: 'new title')
|
169
169
|
end
|
170
170
|
end
|
@@ -176,7 +176,7 @@ describe QuizApiClient::HttpClient do
|
|
176
176
|
url,
|
177
177
|
**default_request_data
|
178
178
|
).and_return(instance_double('HTTParty::Response', success?: true, code: 200))
|
179
|
-
expect_metrics_calls(client.config, :delete,
|
179
|
+
expect_metrics_calls(client.config, :delete, 200)
|
180
180
|
client.delete('/api/quizzes/1')
|
181
181
|
end
|
182
182
|
end
|
@@ -277,7 +277,7 @@ describe QuizApiClient::HttpClient do
|
|
277
277
|
it 'raises error' do
|
278
278
|
expect(client).to receive(:successful_response?).with(mock_response).and_return(false)
|
279
279
|
expect(client.class).to receive(:get).and_return(mock_response)
|
280
|
-
expect_metrics_calls(client.config, method,
|
280
|
+
expect_metrics_calls(client.config, method, code)
|
281
281
|
expect_raise_error_call(client.config, method, url_for_path(path), mock_response, nil)
|
282
282
|
expect { client.get(path, all: false, query: { sort: 'alpha' }) }
|
283
283
|
.to raise_error(QuizApiClient::HttpClient::RequestFailed)
|
@@ -290,7 +290,7 @@ describe QuizApiClient::HttpClient do
|
|
290
290
|
it 'raises error' do
|
291
291
|
expect(client).to receive(:successful_response?).with(mock_response).and_return(false)
|
292
292
|
expect(client.class).to receive(:post).and_return(mock_response)
|
293
|
-
expect_metrics_calls(client.config, method,
|
293
|
+
expect_metrics_calls(client.config, method, code)
|
294
294
|
expect_raise_error_call(client.config, method, url_for_path(path), mock_response, nil)
|
295
295
|
expect { client.post(path) }.to raise_error(QuizApiClient::HttpClient::RequestFailed)
|
296
296
|
end
|
@@ -302,7 +302,7 @@ describe QuizApiClient::HttpClient do
|
|
302
302
|
it 'raises error' do
|
303
303
|
expect(client).to receive(:successful_response?).with(mock_response).and_return(false)
|
304
304
|
expect(client.class).to receive(:put).and_return(mock_response)
|
305
|
-
expect_metrics_calls(client.config, method,
|
305
|
+
expect_metrics_calls(client.config, method, code)
|
306
306
|
expect_raise_error_call(client.config, method, url_for_path(path), mock_response, nil)
|
307
307
|
expect { client.put(path) }.to raise_error(QuizApiClient::HttpClient::RequestFailed)
|
308
308
|
end
|
@@ -314,7 +314,7 @@ describe QuizApiClient::HttpClient do
|
|
314
314
|
it 'raises error' do
|
315
315
|
expect(client).to receive(:successful_response?).with(mock_response).and_return(false)
|
316
316
|
expect(client.class).to receive(:patch).and_return(mock_response)
|
317
|
-
expect_metrics_calls(client.config, method,
|
317
|
+
expect_metrics_calls(client.config, method, code)
|
318
318
|
expect_raise_error_call(client.config, method, url_for_path(path), mock_response, nil)
|
319
319
|
expect { client.patch(path) }.to raise_error(QuizApiClient::HttpClient::RequestFailed)
|
320
320
|
end
|
@@ -326,7 +326,7 @@ describe QuizApiClient::HttpClient do
|
|
326
326
|
it 'raises error' do
|
327
327
|
expect(client).to receive(:successful_response?).with(mock_response).and_return(false)
|
328
328
|
expect(client.class).to receive(:delete).and_return(mock_response)
|
329
|
-
expect_metrics_calls(client.config, method,
|
329
|
+
expect_metrics_calls(client.config, method, code)
|
330
330
|
expect_raise_error_call(client.config, method, url_for_path(path), mock_response, nil)
|
331
331
|
expect { client.delete(path) }.to raise_error(QuizApiClient::HttpClient::RequestFailed)
|
332
332
|
end
|
@@ -348,21 +348,21 @@ describe QuizApiClient::HttpClient do
|
|
348
348
|
|
349
349
|
it 'handles an HTTParty::Error error' do
|
350
350
|
stub_request(:get, url).to_raise(HTTParty::Error)
|
351
|
-
expect_metrics_calls(client.config, :get,
|
351
|
+
expect_metrics_calls(client.config, :get, 0)
|
352
352
|
expect_raise_error_call(client.config, :get, url_for_path(path), nil, kind_of(HTTParty::Error))
|
353
353
|
expect { client.get('/') }.to raise_error(QuizApiClient::HttpClient::RequestFailed)
|
354
354
|
end
|
355
355
|
|
356
356
|
it 'handles an Errno::ECONNREFUSED error' do
|
357
357
|
stub_request(:get, url).to_raise(Errno::ECONNREFUSED)
|
358
|
-
expect_metrics_calls(client.config, :get,
|
358
|
+
expect_metrics_calls(client.config, :get, 0)
|
359
359
|
expect_raise_error_call(client.config, :get, url_for_path(path), nil, kind_of(Errno::ECONNREFUSED))
|
360
360
|
expect { client.get('/') }.to raise_error(QuizApiClient::HttpClient::RequestFailed)
|
361
361
|
end
|
362
362
|
|
363
363
|
it 'handles an Net::ReadTimeout error' do
|
364
364
|
stub_request(:get, url).to_raise(Net::ReadTimeout)
|
365
|
-
expect_metrics_calls(client.config, :get,
|
365
|
+
expect_metrics_calls(client.config, :get, 0)
|
366
366
|
expect_raise_error_call(client.config, :get, url_for_path(path), nil, kind_of(Net::ReadTimeout))
|
367
367
|
expect { client.get('/') }.to raise_error(QuizApiClient::HttpClient::RequestFailed)
|
368
368
|
end
|
@@ -3,10 +3,9 @@ require 'inst_statsd'
|
|
3
3
|
describe QuizApiClient::HttpRequest::Metrics do
|
4
4
|
let(:config) { QuizApiClient::Config.new }
|
5
5
|
let(:method) { 'GET' }
|
6
|
-
let(:url) { 'https://something.com' }
|
7
6
|
let(:code) { 200 }
|
8
7
|
|
9
|
-
subject { described_class.new(config, method,
|
8
|
+
subject { described_class.new(config, method, code) }
|
10
9
|
|
11
10
|
describe '#increment' do
|
12
11
|
it 'does nothing when the metrics handler is nil' do
|
@@ -29,8 +28,7 @@ describe QuizApiClient::HttpRequest::Metrics do
|
|
29
28
|
'fake-namespace.quiz_api_client.request.count',
|
30
29
|
tags: {
|
31
30
|
method: method,
|
32
|
-
status: code
|
33
|
-
url: url
|
31
|
+
status: code
|
34
32
|
}
|
35
33
|
)
|
36
34
|
|
@@ -64,8 +62,7 @@ describe QuizApiClient::HttpRequest::Metrics do
|
|
64
62
|
duration_in_secs * 1_000,
|
65
63
|
tags: {
|
66
64
|
method: method,
|
67
|
-
status: code
|
68
|
-
url: url
|
65
|
+
status: code
|
69
66
|
}
|
70
67
|
)
|
71
68
|
|
@@ -40,6 +40,43 @@ describe QuizApiClient::Services::QuizCloneJobsService do
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
+
describe '#update' do
|
44
|
+
let(:body) { { resource_map_url: 'http://foo.bar/123' } }
|
45
|
+
let(:params) { { id: 1 } }
|
46
|
+
let(:expected_url) { "https://#{host}/api/quiz_clone_jobs/#{params.fetch(:id)}" }
|
47
|
+
let(:stubbed_response) { { 'id' => '1' } }
|
48
|
+
|
49
|
+
context 'on success' do
|
50
|
+
before do
|
51
|
+
stub_request(:put, expected_url)
|
52
|
+
.to_return(
|
53
|
+
status: 200,
|
54
|
+
body: stubbed_response.to_json,
|
55
|
+
headers: { 'Content-Type' => 'application/json' }
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'puts to /api/quiz_clone_jobs/:id' do
|
60
|
+
result = subject.update(body: body, params: params, token: 'token')
|
61
|
+
expect(result.parsed_response).to eql(stubbed_response)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'on failure' do
|
66
|
+
let(:status_code) { 401 }
|
67
|
+
|
68
|
+
before do
|
69
|
+
stub_request(:put, expected_url)
|
70
|
+
.to_return(status: status_code)
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'returns a response with the correct code' do
|
74
|
+
response = subject.update(body: body, params: params, token: 'token')
|
75
|
+
expect(response.code).to eq(status_code)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
43
80
|
describe '#create_batch' do
|
44
81
|
let(:expected_url) { "https://#{host}/api/quiz_clone_jobs/create_batch" }
|
45
82
|
let(:body) { { quiz_ids: [1, 2] } }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: quiz_api_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.14.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Slaughter
|
@@ -15,7 +15,7 @@ authors:
|
|
15
15
|
autorequire:
|
16
16
|
bindir: exe
|
17
17
|
cert_chain: []
|
18
|
-
date:
|
18
|
+
date: 2024-02-22 00:00:00.000000000 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: httparty
|