td-client 1.0.7 → 2.0.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: bfa8466ceb0e2dc014d9222e5bb295d7f9102951828531d993f9d92eb6592457
4
- data.tar.gz: 8521a0ec4855d3d266fceabe8bbb7a42fcf52c38bdf4f6d8e8af96cae97add7f
3
+ metadata.gz: 54f6df5e1e0ea43f4a92e837b1793cf9f217cd643c7cecb831c9191fdb0396ae
4
+ data.tar.gz: f28643eb1908f7c6e251b75321a50dd87a0419f5f0982c218829e947a15ac04b
5
5
  SHA512:
6
- metadata.gz: 477518baf02db8ec6f126f847d54b2b9fe5885a4bb3c5d6d07dfb4a8110d997ca4288541260932abd0e69dc56c50dd0ed1d62b5fbd9884cc6be10b31329b8440
7
- data.tar.gz: f13066abdba694a7dd28f3fe67efac2c598f25b67f1fa7948dc85b4cdd6d9c476ea12eddd63b3229cb275ba193cb397f3a803a2c6ccc56cf7c08e56498d93e34
6
+ metadata.gz: 29eed4ae7f9957391958faaaa9a86ddb64040fbb640deb5922614f95806de48455f94c20602ee39240df16adeae0257a7ff2db4036459e3285ca84b486e54370
7
+ data.tar.gz: c0de46e029df837aa5decfb1cead811d24bd783444cabd7fe97da114818641f0cd7476756d94f273dbef72f1673856e4b16fda51ae150e5ef840a144aaf67948
@@ -22,24 +22,5 @@ module Account
22
22
  return [account_id, plan, storage_size, guaranteed_cores, maximum_cores, created_at]
23
23
  end
24
24
 
25
- # @param [Fixnum] from
26
- # @param [Fixnum] to
27
- # @return [Array]
28
- def account_core_utilization(from, to)
29
- params = { }
30
- params['from'] = from.to_s if from
31
- params['to'] = to.to_s if to
32
- code, body, res = get("/v3/account/core_utilization", params)
33
- if code != "200"
34
- raise_error("Show account failed", res)
35
- end
36
- js = checked_json(body, %w[from to interval history])
37
- from = Time.parse(js['from']).utc
38
- to = Time.parse(js['to']).utc
39
- interval = js['interval'].to_i
40
- history = js['history']
41
- return [from, to, interval, history]
42
- end
43
-
44
25
  end
45
26
  end
@@ -29,9 +29,7 @@ module Import
29
29
  if code[0] != ?2
30
30
  raise_error("Import failed", res)
31
31
  end
32
- js = checked_json(body, %w[])
33
- time = js['elapsed_time'].to_f
34
- return time
32
+ return true
35
33
  end
36
34
 
37
35
  end
@@ -126,7 +126,7 @@ module Job
126
126
  job_result_download(job_id) do |chunk|
127
127
  unpacker.feed_each(chunk) do |row|
128
128
  result << row
129
- end
129
+ end unless chunk.empty?
130
130
  end
131
131
  return result
132
132
  end
@@ -163,7 +163,7 @@ module Job
163
163
  upkr = MessagePack::Unpacker.new
164
164
  # default to decompressing the response since format is fixed to 'msgpack'
165
165
  job_result_download(job_id) do |chunk|
166
- upkr.feed_each(chunk, &block)
166
+ upkr.feed_each(chunk, &block) unless chunk.empty?
167
167
  end
168
168
  nil
169
169
  end
@@ -177,9 +177,9 @@ module Job
177
177
  upkr = MessagePack::Unpacker.new
178
178
  # default to decompressing the response since format is fixed to 'msgpack'
179
179
  job_result_download(job_id) do |chunk, total|
180
- upkr.feed_each(chunk) {|unpacked|
180
+ upkr.feed_each(chunk) do |unpacked|
181
181
  yield unpacked, total if block_given?
182
- }
182
+ end unless chunk.empty?
183
183
  end
184
184
  nil
185
185
  end
data/lib/td/client/api.rb CHANGED
@@ -74,6 +74,7 @@ class API
74
74
  @retry_post_requests = opts[:retry_post_requests] || false
75
75
  @retry_delay = opts[:retry_delay] || 5
76
76
  @max_cumul_retry_delay = opts[:max_cumul_retry_delay] || 600
77
+ @verify = opts[:verify]
77
78
 
78
79
  case uri.scheme
79
80
  when 'http', 'https'
@@ -526,6 +527,14 @@ private
526
527
  client.ssl_config.options |= OpenSSL::SSL::OP_NO_SSLv3
527
528
  end
528
529
 
530
+ # allow users to use their own custom ca
531
+ # or disable verification
532
+ if @verify == false
533
+ client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
534
+ elsif @verify.is_a? String
535
+ client.ssl_config.add_trust_ca(@verify)
536
+ end
537
+
529
538
  header = {}
530
539
  if @apikey
531
540
  header['Authorization'] = "TD1 #{apikey}"
@@ -636,15 +645,27 @@ private
636
645
  klass
637
646
  else
638
647
  case status_code
648
+ when "400"
649
+ BadRequestError
650
+ when "401"
651
+ AuthError
652
+ when "403"
653
+ ForbiddenError
639
654
  when "404"
640
655
  NotFoundError
656
+ when "405"
657
+ MethodNotAllowedError
641
658
  when "409"
642
659
  message = "#{message}: conflicts_with job:#{error["details"]["conflicts_with"]}" if error["details"] && error["details"]["conflicts_with"]
643
660
  AlreadyExistsError
644
- when "401"
645
- AuthError
646
- when "403"
647
- ForbiddenError
661
+ when "415"
662
+ UnsupportedMediaTypeError
663
+ when "422"
664
+ UnprocessableEntityError
665
+ when "429"
666
+ TooManyRequestsError
667
+ when /\A4\d\d\z/
668
+ ClientError
648
669
  else
649
670
  message = "#{status_code}: #{message}"
650
671
  APIError
@@ -13,16 +13,32 @@ class APIError < StandardError
13
13
  end
14
14
  end
15
15
 
16
- # 401 API errors
17
- class AuthError < APIError
16
+ # 4xx Client Errors
17
+ class ClientError < APIError
18
18
  end
19
19
 
20
- # 403 API errors, used for database permissions
21
- class ForbiddenError < APIError
20
+ # 400 Bad Request
21
+ class BadRequestError < ClientError
22
22
  end
23
23
 
24
- # 409 API errors
25
- class AlreadyExistsError < APIError
24
+ # 401 Unauthorized
25
+ class AuthError < ClientError
26
+ end
27
+
28
+ # 403 Forbidden, used for database permissions
29
+ class ForbiddenError < ClientError
30
+ end
31
+
32
+ # 404 Not Found
33
+ class NotFoundError < ClientError
34
+ end
35
+
36
+ # 405 Method Not Allowed
37
+ class MethodNotAllowedError < ClientError
38
+ end
39
+
40
+ # 409 Conflict
41
+ class AlreadyExistsError < ClientError
26
42
  attr_reader :conflicts_with
27
43
  def initialize(error_message = nil, api_backtrace = nil, conflicts_with=nil)
28
44
  super(error_message, api_backtrace)
@@ -30,8 +46,16 @@ class AlreadyExistsError < APIError
30
46
  end
31
47
  end
32
48
 
33
- # 404 API errors
34
- class NotFoundError < APIError
49
+ # 415 Unsupported Media Type
50
+ class UnsupportedMediaTypeError < ClientError
51
+ end
52
+
53
+ # 422 Unprocessable Entity
54
+ class UnprocessableEntityError < ClientError
55
+ end
56
+
57
+ # 429 Too Many Requests
58
+ class TooManyRequestsError < ClientError
35
59
  end
36
60
 
37
61
  end
@@ -1,5 +1,5 @@
1
1
  module TreasureData
2
2
  class Client
3
- VERSION = '1.0.7'
3
+ VERSION = '2.0.0'
4
4
  end
5
5
  end
data/lib/td/client.rb CHANGED
@@ -60,14 +60,6 @@ class Client
60
60
  return Account.new(self, account_id, plan, storage, guaranteed_cores, maximum_cores, created_at)
61
61
  end
62
62
 
63
- # @param [Fixnum] from
64
- # @param [Fixnum] to
65
- # @return [Array] from, to, interval, history
66
- def core_utilization(from, to)
67
- from, to, interval, history = @api.account_core_utilization(from, to)
68
- return from, to, interval, history
69
- end
70
-
71
63
  # @return [Array] databases
72
64
  def databases
73
65
  m = @api.list_databases
@@ -18,17 +18,4 @@ describe 'Account API' do
18
18
  end
19
19
  end
20
20
 
21
- describe 'account_core_utilization' do
22
- it 'returns core utilization' do
23
- from = '2014-12-01T00:00:00+0900'
24
- to = '2015-01-01T00:00:00+0900'
25
- stub_api_request(:get, "/v3/account/core_utilization", :query => {'from' => from, 'to' => to}).
26
- to_return(:body => {'from' => from, 'to' => to, 'interval' => 1, 'history' => ['dummy']}.to_json)
27
- r = api.account_core_utilization(from, to)
28
- expect(r[0]).to eq(Time.parse(from))
29
- expect(r[1]).to eq(Time.parse(to))
30
- expect(r[2]).to eq(1)
31
- expect(r[3]).to eq(['dummy'])
32
- end
33
- end
34
21
  end
@@ -37,34 +37,54 @@ describe 'API SSL connection' do
37
37
  it 'should fail to connect SSLv3 only server' do
38
38
  @server = setup_server(:SSLv3)
39
39
  api = API.new(nil, :endpoint => "https://localhost:#{@serverport}", :retry_post_requests => false)
40
- api.ssl_ca_file = File.join(DIR, 'ca-all.cert')
40
+ api.ssl_ca_file = File.join(DIR, 'testRootCA.crt')
41
41
  expect {
42
- api.delete_database('no_such_database')
42
+ begin
43
+ api.delete_database('no_such_database')
44
+ rescue Errno::ECONNRESET
45
+ raise OpenSSL::SSL::SSLError # When openssl does not support SSLv3, httpclient server will not start. For context: https://github.com/nahi/httpclient/pull/424#issuecomment-731714786
46
+ end
43
47
  }.to raise_error OpenSSL::SSL::SSLError
44
48
  end
45
49
 
46
- it 'should success to connect TLSv1 only server' do
47
- @server = setup_server(:TLSv1)
50
+ it 'should succeed to access to the server with verify false option' do
51
+ @server = setup_server(:TLSv1_2)
52
+ api = API.new(nil, :endpoint => "https://localhost:#{@serverport}", :retry_post_requests => false, :verify => false)
53
+ expect {
54
+ api.delete_database('no_such_database')
55
+ }.to raise_error TreasureData::NotFoundError
56
+ end
57
+
58
+ it 'should succeed to access to the server with self signed certificate' do
59
+ @server = setup_server(:TLSv1_2)
60
+ api = API.new(nil, :endpoint => "https://localhost:#{@serverport}", :retry_post_requests => false, :verify => File.join(DIR, 'testRootCA.crt'))
61
+ expect {
62
+ api.delete_database('no_such_database')
63
+ }.to raise_error TreasureData::NotFoundError
64
+ end
65
+
66
+ it 'should success to connect TLSv1_2 only server' do
67
+ @server = setup_server(:TLSv1_2)
48
68
  api = API.new(nil, :endpoint => "https://localhost:#{@serverport}", :retry_post_requests => false)
49
- api.ssl_ca_file = File.join(DIR, 'ca-all.cert')
69
+ api.ssl_ca_file = File.join(DIR, 'testRootCA.crt')
50
70
  expect {
51
71
  api.delete_database('no_such_database')
52
72
  }.to raise_error TreasureData::NotFoundError
53
73
  end
54
74
 
55
- def setup_server(ssl_version)
75
+ def setup_server(ssl_version, port = 1000 + rand(1000))
56
76
  logger = Logger.new(STDERR)
57
77
  logger.level = Logger::Severity::FATAL # avoid logging SSLError (ERROR level)
58
78
  @server = WEBrick::HTTPServer.new(
59
79
  :BindAddress => "localhost",
60
80
  :Logger => logger,
61
- :Port => 0,
81
+ :Port => port,
62
82
  :AccessLog => [],
63
83
  :DocumentRoot => '.',
64
84
  :SSLEnable => true,
65
- :SSLCACertificateFile => File.join(DIR, 'ca.cert'),
66
- :SSLCertificate => cert('server.cert'),
67
- :SSLPrivateKey => key('server.key')
85
+ :SSLCACertificateFile => File.join(DIR, 'testRootCA.crt'),
86
+ :SSLCertificate => cert('testServer.crt'),
87
+ :SSLPrivateKey => key('testServer.key')
68
88
  )
69
89
  @serverport = @server.config[:Port]
70
90
  @server.mount(
@@ -46,9 +46,9 @@ describe 'Import API' do
46
46
  end
47
47
  stub_request(:put, "https://#{endpoint_import}/v3/table/import_with_id/db/table/unique_id/format").
48
48
  with(:body => '12345').
49
- to_return(:body => '{"elapsed_time":"1.23"}')
49
+ to_return(:status => 200)
50
50
  File.open(t.path) do |f|
51
- expect(api.import('db', 'table', 'format', f, 5, 'unique_id')).to eq(1.23)
51
+ expect(api.import('db', 'table', 'format', f, 5, 'unique_id')).to eq(true)
52
52
  end
53
53
  end
54
54
 
@@ -59,9 +59,9 @@ describe 'Import API' do
59
59
  end
60
60
  stub_request(:put, "https://#{endpoint_import}/v3/table/import/db/table/format").
61
61
  with(:body => '12345').
62
- to_return(:body => '{"elapsed_time":"1.23"}')
62
+ to_return(:status => 200)
63
63
  File.open(t.path) do |f|
64
- expect(api.import('db', 'table', 'format', f, 5)).to eq(1.23)
64
+ expect(api.import('db', 'table', 'format', f, 5)).to eq(true)
65
65
  end
66
66
  end
67
67
 
@@ -72,9 +72,9 @@ describe 'Import API' do
72
72
  end
73
73
  stub_request(:put, "http://#{endpoint_import_old}/v3/table/import/db/table/format").
74
74
  with(:body => '12345').
75
- to_return(:body => '{"elapsed_time":"1.23"}')
75
+ to_return(:status => 200)
76
76
  File.open(t.path) do |f|
77
- expect(api_old.import('db', 'table', 'format', f, 5)).to eq(1.23)
77
+ expect(api_old.import('db', 'table', 'format', f, 5)).to eq(true)
78
78
  end
79
79
  end
80
80
 
@@ -85,9 +85,9 @@ describe 'Import API' do
85
85
  end
86
86
  stub_request(:put, "https://#{endpoint_import}/v3/table/import/db/table/format").
87
87
  with(:body => '12345').
88
- to_return(:body => '{"elapsed_time":"1.23"}')
88
+ to_return(:status => 200)
89
89
  File.open(t.path) do |f|
90
- expect(api_default.import('db', 'table', 'format', f, 5)).to eq 1.23
90
+ expect(api_default.import('db', 'table', 'format', f, 5)).to eq true
91
91
  end
92
92
  end
93
93
 
@@ -98,9 +98,9 @@ describe 'Import API' do
98
98
  end
99
99
  stub_request(:put, "http://#{endpoint_import}/v3/table/import/db/table/format").
100
100
  with(:body => '12345').
101
- to_return(:body => '{"elapsed_time":"1.23"}')
101
+ to_return(:status => 200)
102
102
  File.open(t.path) do |f|
103
- expect(api_default_http.import('db', 'table', 'format', f, 5)).to eq 1.23
103
+ expect(api_default_http.import('db', 'table', 'format', f, 5)).to eq true
104
104
  end
105
105
  end
106
106
 
@@ -111,9 +111,9 @@ describe 'Import API' do
111
111
  end
112
112
  stub_request(:put, "https://#{endpoint_unknown}/v3/table/import/db/table/format").
113
113
  with(:body => '12345').
114
- to_return(:body => '{"elapsed_time":"1.23"}')
114
+ to_return(:status => 200)
115
115
  File.open(t.path) do |f|
116
- expect(api_unknown_host.import('db', 'table', 'format', f, 5)).to eq 1.23
116
+ expect(api_unknown_host.import('db', 'table', 'format', f, 5)).to eq true
117
117
  end
118
118
  end
119
119
 
@@ -124,9 +124,9 @@ describe 'Import API' do
124
124
  end
125
125
  stub_request(:put, "http://#{endpoint_unknown}/v3/table/import/db/table/format").
126
126
  with(:body => '12345').
127
- to_return(:body => '{"elapsed_time":"1.23"}')
127
+ to_return(:status => 200)
128
128
  File.open(t.path) do |f|
129
- expect(api_unknown_host_http.import('db', 'table', 'format', f, 5)).to eq 1.23
129
+ expect(api_unknown_host_http.import('db', 'table', 'format', f, 5)).to eq true
130
130
  end
131
131
  end
132
132
 
@@ -140,15 +140,16 @@ describe 'Job Model' do
140
140
  end
141
141
 
142
142
  it 'calls a given block in every wait_interval second' do
143
- now = 1_400_000_000
144
- allow(self).to receive(:sleep){|arg| now += arg }
145
- allow(Process).to receive(:clock_gettime){ now }
143
+ # Let's try disable stubbing #sleep for now
144
+ # now = 1_400_000_000
145
+ # allow(self).to receive(:sleep){|arg| now += arg }
146
+ # allow(Process).to receive(:clock_gettime){ now }
146
147
  expect { |b|
147
148
  begin
148
149
  thread = Thread.start {
149
- job.wait(nil, 2, &b)
150
+ job.wait(nil, 0.1, &b)
150
151
  }
151
- sleep 6
152
+ sleep 0.3
152
153
  change_job_status(Job::STATUS_SUCCESS)
153
154
  thread.join(1)
154
155
  expect(thread).to be_stop
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.7
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Treasure Data, Inc.
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-04 00:00:00.000000000 Z
11
+ date: 2023-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack
@@ -128,6 +128,20 @@ dependencies:
128
128
  - - ">="
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: webrick
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
131
145
  description: Treasure Data API library for Ruby
132
146
  email: support@treasure-data.com
133
147
  executables: []
@@ -183,7 +197,7 @@ homepage: http://treasuredata.com/
183
197
  licenses:
184
198
  - Apache-2.0
185
199
  metadata: {}
186
- post_install_message:
200
+ post_install_message:
187
201
  rdoc_options: []
188
202
  require_paths:
189
203
  - lib
@@ -198,8 +212,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
198
212
  - !ruby/object:Gem::Version
199
213
  version: '0'
200
214
  requirements: []
201
- rubygems_version: 3.0.0.beta3
202
- signing_key:
215
+ rubygems_version: 3.4.10
216
+ signing_key:
203
217
  specification_version: 4
204
218
  summary: Treasure Data API library for Ruby
205
219
  test_files: