td-client 1.0.2 → 1.0.3
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/lib/td/client/api.rb +11 -10
- data/lib/td/client/api/job.rb +29 -25
- data/lib/td/client/version.rb +1 -1
- data/spec/spec_helper.rb +9 -0
- data/spec/td/client/job_api_spec.rb +21 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8123cb6d1ba413d6e482a5396d2675340256e1b
|
4
|
+
data.tar.gz: 3e8541a2b1070b50dc270c54bb5f76a24e29cd24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7bdbacf95cd257c635dd6d72486b32974408d9f09e4f81e1ec1233c4db0b709bbb0b48991c2820c80d1a94c305295b7809771dbc34ca5e8b0e885a1146053277
|
7
|
+
data.tar.gz: 901e31f20e95e0aa83fabaa4daf18470376aee5396cbe09cad474e5680ab1e56fee43ceae3e466b91f92d1b60b7de1a4b2ad777e86e309807bf3d5a733007fcd
|
data/lib/td/client/api.rb
CHANGED
@@ -289,6 +289,7 @@ private
|
|
289
289
|
if block
|
290
290
|
current_total_chunk_size = 0
|
291
291
|
response = client.get(target, params, header) {|res, chunk|
|
292
|
+
break if res.status != 200
|
292
293
|
current_total_chunk_size += chunk.bytesize
|
293
294
|
block.call(res, chunk, current_total_chunk_size)
|
294
295
|
}
|
@@ -300,17 +301,17 @@ private
|
|
300
301
|
else
|
301
302
|
response = client.get(target, params, header)
|
302
303
|
|
303
|
-
|
304
|
-
|
304
|
+
status = response.code
|
305
|
+
# retry if the HTTP error code is 500 or higher and we did not run out of retrying attempts
|
306
|
+
if status >= 500 && cumul_retry_delay < @max_cumul_retry_delay
|
307
|
+
$stderr.puts "Error #{status}: #{get_error(response)}. Retrying after #{retry_delay} seconds..."
|
308
|
+
sleep retry_delay
|
309
|
+
cumul_retry_delay += retry_delay
|
310
|
+
retry_delay *= 2
|
311
|
+
redo # restart from beginning of do-while loop
|
312
|
+
end
|
305
313
|
|
306
|
-
|
307
|
-
# retry if the HTTP error code is 500 or higher and we did not run out of retrying attempts
|
308
|
-
if !block_given? && status >= 500 && cumul_retry_delay < @max_cumul_retry_delay
|
309
|
-
$stderr.puts "Error #{status}: #{get_error(response)}. Retrying after #{retry_delay} seconds..."
|
310
|
-
sleep retry_delay
|
311
|
-
cumul_retry_delay += retry_delay
|
312
|
-
retry_delay *= 2
|
313
|
-
redo # restart from beginning of do-while loop
|
314
|
+
validate_content_length!(response, response.body.to_s.bytesize) if @ssl
|
314
315
|
end
|
315
316
|
rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Timeout::Error, EOFError,
|
316
317
|
SystemCallError, OpenSSL::SSL::SSLError, SocketError, HTTPClient::TimeoutError,
|
data/lib/td/client/api/job.rb
CHANGED
@@ -272,6 +272,22 @@ module Job
|
|
272
272
|
end
|
273
273
|
end
|
274
274
|
|
275
|
+
def validate_response_status(res, current_total_chunk_size=0)
|
276
|
+
case res.status
|
277
|
+
when 200
|
278
|
+
if current_total_chunk_size != 0
|
279
|
+
# try to resume but the server returns 200
|
280
|
+
raise_error("Get job result failed", res)
|
281
|
+
end
|
282
|
+
when 206 # resuming
|
283
|
+
else
|
284
|
+
if res.status/100 == 5
|
285
|
+
raise HTTPServerException
|
286
|
+
end
|
287
|
+
raise_error("Get job result failed", res)
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
275
291
|
class HTTPServerException < StandardError
|
276
292
|
end
|
277
293
|
|
@@ -297,43 +313,31 @@ module Job
|
|
297
313
|
current_total_chunk_size = 0
|
298
314
|
infl = nil
|
299
315
|
begin # LOOP of Network/Server errors
|
300
|
-
response =
|
301
|
-
|
302
|
-
|
303
|
-
case res.
|
304
|
-
when
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
end
|
309
|
-
when 206 # resuming
|
310
|
-
else
|
311
|
-
if res.status/100 == 5
|
312
|
-
raise HTTPServerException
|
313
|
-
end
|
314
|
-
raise_error("Get job result failed", res)
|
315
|
-
end
|
316
|
-
if infl.nil? && autodecode
|
317
|
-
case res.header['Content-Encoding'][0].to_s.downcase
|
318
|
-
when 'gzip'
|
319
|
-
infl = Zlib::Inflate.new(Zlib::MAX_WBITS + 16)
|
320
|
-
when 'deflate'
|
321
|
-
infl = Zlib::Inflate.new
|
322
|
-
end
|
316
|
+
response = client.get(url, params, header) do |res, chunk|
|
317
|
+
validate_response_status(res, current_total_chunk_size)
|
318
|
+
if infl.nil? && autodecode
|
319
|
+
case res.header['Content-Encoding'][0].to_s.downcase
|
320
|
+
when 'gzip'
|
321
|
+
infl = Zlib::Inflate.new(Zlib::MAX_WBITS + 16)
|
322
|
+
when 'deflate'
|
323
|
+
infl = Zlib::Inflate.new
|
323
324
|
end
|
324
325
|
end
|
325
|
-
response = res
|
326
326
|
current_total_chunk_size += chunk.bytesize
|
327
327
|
chunk = infl.inflate(chunk) if infl
|
328
328
|
yield chunk, current_total_chunk_size
|
329
329
|
end
|
330
330
|
|
331
|
+
# for the case response body is empty
|
332
|
+
# Note that webmock returns response.body as "" instead of nil
|
333
|
+
validate_response_status(response, 0) if response.body.to_s.empty?
|
334
|
+
|
331
335
|
# completed?
|
332
336
|
validate_content_length_with_range(response, current_total_chunk_size)
|
333
337
|
rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Timeout::Error, EOFError,
|
334
338
|
SystemCallError, OpenSSL::SSL::SSLError, SocketError, HTTPClient::TimeoutError,
|
335
339
|
HTTPServerException => e
|
336
|
-
if
|
340
|
+
if current_total_chunk_size > 0
|
337
341
|
if etag = response.header['ETag'][0]
|
338
342
|
header['If-Range'] = etag
|
339
343
|
header['Range'] = "bytes=#{current_total_chunk_size}-"
|
data/lib/td/client/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -34,6 +34,15 @@ require 'json'
|
|
34
34
|
|
35
35
|
include TreasureData
|
36
36
|
|
37
|
+
RSpec.configure do |config|
|
38
|
+
# This allows you to limit a spec run to individual examples or groups
|
39
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
40
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
41
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
42
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
43
|
+
config.filter_run_when_matching :focus
|
44
|
+
end
|
45
|
+
|
37
46
|
shared_context 'common helper' do
|
38
47
|
let :account_id do
|
39
48
|
1
|
@@ -320,6 +320,27 @@ describe 'Job API' do
|
|
320
320
|
expect(api.job_result(12345)).to eq ['hello', 'world']
|
321
321
|
end
|
322
322
|
|
323
|
+
it '200 (blank) -> 200 works fine' do
|
324
|
+
i = 0
|
325
|
+
stub_api_request(:get, '/v3/job/result/12345').
|
326
|
+
with(:query => {'format' => 'msgpack'}).
|
327
|
+
to_return do
|
328
|
+
i += 1
|
329
|
+
puts "count : #{i}"
|
330
|
+
{
|
331
|
+
:headers => {
|
332
|
+
'Content-Length' => packed.bytesize,
|
333
|
+
'Etag' => '"abcdefghijklmn"',
|
334
|
+
},
|
335
|
+
:body => (i == 1 ? nil : packed)
|
336
|
+
}
|
337
|
+
end
|
338
|
+
expect(api).to receive(:sleep).once
|
339
|
+
expect($stderr).to receive(:print)
|
340
|
+
expect($stderr).to receive(:puts)
|
341
|
+
expect(api.job_result(12345)).to eq ['hello', 'world']
|
342
|
+
end
|
343
|
+
|
323
344
|
end
|
324
345
|
|
325
346
|
describe 'job_result_format' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: td-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Treasure Data, Inc.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: msgpack
|
@@ -200,7 +200,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
200
200
|
version: '0'
|
201
201
|
requirements: []
|
202
202
|
rubyforge_project:
|
203
|
-
rubygems_version: 2.6.
|
203
|
+
rubygems_version: 2.6.13
|
204
204
|
signing_key:
|
205
205
|
specification_version: 4
|
206
206
|
summary: Treasure Data API library for Ruby
|