td-client 0.8.54 → 0.8.55

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.
data/ChangeLog CHANGED
@@ -1,75 +1,82 @@
1
- == 2012-08-23 version 0.8.54
1
+ == 2013-09-13 version 0.8.55
2
+
3
+ * Use httpclient gem for import and bulk_import upload
4
+ * connect_timeout / read_timeout / send_timeout options are available.
5
+ * these options affect only import and bulk_import upload.
6
+
7
+
8
+ == 2013-08-23 version 0.8.54
2
9
 
3
10
  * Support table's expire_days API
4
11
 
5
12
 
6
- == 2012-07-22 version 0.8.53
13
+ == 2013-07-22 version 0.8.53
7
14
 
8
15
  * Add normalized_msgpack method to serialize Bignum type
9
16
 
10
17
 
11
- == 2012-06-24 version 0.8.52
18
+ == 2013-06-24 version 0.8.52
12
19
 
13
20
  * Add last_log_timestamp to Table model
14
21
 
15
22
 
16
- == 2012-06-17 version 0.8.51
23
+ == 2013-06-17 version 0.8.51
17
24
 
18
25
  * Relax dependent gem versions
19
26
 
20
27
 
21
- == 2012-05-27 version 0.8.50
28
+ == 2013-05-27 version 0.8.50
22
29
 
23
30
  * add_user now requires email and passowrd
24
31
 
25
32
 
26
- == 2012-05-06 version 0.8.49
33
+ == 2013-05-06 version 0.8.49
27
34
 
28
35
  * Add User-Agent header
29
36
  * VERSION constant moved to under TreasureData::Client
30
37
 
31
38
 
32
- == 2012-04-22 version 0.8.48
39
+ == 2013-04-22 version 0.8.48
33
40
 
34
41
  * create_schedule now takes :type option
35
42
  * Fix wrong error messages
36
43
  * Ues 'api-import' instead of 'api' on data import
37
44
 
38
45
 
39
- == 2012-04-09 version 0.8.47
46
+ == 2013-04-09 version 0.8.47
40
47
 
41
48
  * Fix HTTP proxy handlig issue which is overwritten with ENV['HTTP_PROXY']
42
49
 
43
50
 
44
- == 2012-03-29 version 0.8.46
51
+ == 2013-03-29 version 0.8.46
45
52
 
46
53
  * Add IP limitation API
47
54
 
48
55
 
49
- == 2012-01-25 version 0.8.45
56
+ == 2013-01-25 version 0.8.45
50
57
 
51
58
  * Re-implement Client#job_status using /v3/job/status/job_id
52
59
  instead of /v3/job/show/job_id to poll the progress of a job
53
60
 
54
61
 
55
- == 2012-01-23 version 0.8.44
62
+ == 2013-01-23 version 0.8.44
56
63
 
57
64
  * Re-add json gem dependency
58
65
 
59
66
 
60
- == 2012-01-23 version 0.8.43
67
+ == 2013-01-23 version 0.8.43
61
68
 
62
69
  * Add organization parameter support to create_database, query,
63
70
  partial_delete, create_bulk_import, create_result
64
71
 
65
72
 
66
- == 2012-01-16 version 0.8.42
73
+ == 2013-01-16 version 0.8.42
67
74
 
68
75
  * Added retry_limit to job and schedule APIs
69
76
  * Increased table/database name limit from 32 to 256
70
77
 
71
78
 
72
- == 2012-01-10 version 0.8.41
79
+ == 2013-01-10 version 0.8.41
73
80
 
74
81
  * Fix API#job_result_format to handle Content-Encoding properly
75
82
 
data/lib/td/client/api.rb CHANGED
@@ -27,6 +27,8 @@ class API
27
27
  require 'net/http'
28
28
  require 'net/https'
29
29
  require 'time'
30
+ #require 'faraday' # faraday doesn't support streaming upload with httpclient yet so now disabled
31
+ require 'httpclient'
30
32
 
31
33
  @apikey = apikey
32
34
  @user_agent = "TD-Client-Ruby: #{TreasureData::Client::VERSION}"
@@ -35,6 +37,10 @@ class API
35
37
  endpoint = opts[:endpoint] || ENV['TD_API_SERVER'] || DEFAULT_ENDPOINT
36
38
  uri = URI.parse(endpoint)
37
39
 
40
+ @connect_timeout = opts[:connect_timeout] || 60
41
+ @read_timeout = opts[:read_timeout] || 600
42
+ @send_timeout = opts[:send_timeout] || 600
43
+
38
44
  case uri.scheme
39
45
  when 'http', 'https'
40
46
  @host = uri.host
@@ -63,10 +69,12 @@ class API
63
69
 
64
70
  @http_proxy = opts[:http_proxy] || ENV['HTTP_PROXY']
65
71
  if @http_proxy
66
- if @http_proxy =~ /\Ahttp:\/\/(.*)\z/
67
- @http_proxy = $~[1]
68
- end
69
- proxy_host, proxy_port = @http_proxy.split(':',2)
72
+ http_proxy = if @http_proxy =~ /\Ahttp:\/\/(.*)\z/
73
+ $~[1]
74
+ else
75
+ @http_proxy
76
+ end
77
+ proxy_host, proxy_port = http_proxy.split(':',2)
70
78
  proxy_port = (proxy_port ? proxy_port.to_i : 80)
71
79
  @http_class = Net::HTTP::Proxy(proxy_host, proxy_port)
72
80
  else
@@ -1404,28 +1412,26 @@ class API
1404
1412
  end
1405
1413
 
1406
1414
  def put(url, stream, size, opts = {})
1407
- http, header = new_http(opts)
1408
-
1409
- http.read_timeout = 600
1410
-
1411
- path = @base_path + url
1415
+ client, header = new_client(opts)
1416
+ client.send_timeout = @send_timeout
1417
+ client.receive_timeout = @read_timeout
1412
1418
 
1413
1419
  header['Content-Type'] = 'application/octet-stream'
1414
1420
  header['Content-Length'] = size.to_s
1415
1421
 
1416
- request = Net::HTTP::Put.new(url, header)
1417
- if stream.class.name == 'StringIO'
1418
- request.body = stream.string
1419
- else
1420
- if request.respond_to?(:body_stream=)
1421
- request.body_stream = stream
1422
- else # Ruby 1.8
1423
- request.body = stream.read
1424
- end
1425
- end
1422
+ body = if stream.class.name == 'StringIO'
1423
+ stream.string
1424
+ else
1425
+ stream
1426
+ end
1427
+ target = build_endpoint(url, opts[:host] || @host)
1428
+ response = client.put(target, body, header)
1429
+ return [response.code.to_s, response.body, response]
1430
+ end
1426
1431
 
1427
- response = http.request(request)
1428
- return [response.code, response.body, response]
1432
+ def build_endpoint(url, host)
1433
+ schema = @ssl ? 'https' : 'http'
1434
+ "#{schema}://#{host}:#{@port}/#{@base_path + url}"
1429
1435
  end
1430
1436
 
1431
1437
  def new_http(opts = {})
@@ -1450,17 +1456,36 @@ class API
1450
1456
  return http, header
1451
1457
  end
1452
1458
 
1459
+ def new_client(opts = {})
1460
+ client = HTTPClient.new(@http_proxy, @user_agent)
1461
+ client.connect_timeout = @connect_timeout
1462
+
1463
+ if @ssl
1464
+ client.ssl_config.add_trust_ca(File.join(File.dirname(__FILE__), '..', '..', '..', 'data', 'ca-bundle.crt'))
1465
+ client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_PEER
1466
+ end
1467
+
1468
+ header = {}
1469
+ if @apikey
1470
+ header['Authorization'] = "TD1 #{apikey}"
1471
+ end
1472
+ header['Date'] = Time.now.rfc2822
1473
+
1474
+ return client, header
1475
+ end
1476
+
1453
1477
  def raise_error(msg, res, klass=nil)
1454
1478
  begin
1455
1479
  js = JSON.load(res.body)
1456
1480
  msg = js['message']
1457
1481
  error_code = js['error_code']
1482
+ status_code = res.code.to_s
1458
1483
 
1459
1484
  if klass
1460
1485
  raise klass, "#{error_code}: #{msg}"
1461
- elsif res.code == "404"
1486
+ elsif status_code == "404"
1462
1487
  raise NotFoundError, "#{error_code}: #{msg}"
1463
- elsif res.code == "409"
1488
+ elsif status_code == "409"
1464
1489
  raise AlreadyExistsError, "#{error_code}: #{msg}"
1465
1490
  else
1466
1491
  raise APIError, "#{error_code}: #{msg}"
@@ -1469,9 +1494,9 @@ class API
1469
1494
  rescue
1470
1495
  if klass
1471
1496
  raise klass, "#{error_code}: #{msg}"
1472
- elsif res.code == "404"
1497
+ elsif status_code == "404"
1473
1498
  raise NotFoundError, "#{msg}: #{res.body}"
1474
- elsif res.code == "409"
1499
+ elsif status_code == "409"
1475
1500
  raise AlreadyExistsError, "#{msg}: #{res.body}"
1476
1501
  else
1477
1502
  raise APIError, "#{msg}: #{res.body}"
@@ -1,5 +1,5 @@
1
1
  module TreasureData
2
2
  class Client
3
- VERSION = '0.8.54'
3
+ VERSION = '0.8.55'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: td-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.54
4
+ version: 0.8.55
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-23 00:00:00.000000000 Z
12
+ date: 2013-09-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: msgpack
@@ -73,6 +73,22 @@ dependencies:
73
73
  - - ! '>='
74
74
  - !ruby/object:Gem::Version
75
75
  version: 1.7.6
76
+ - !ruby/object:Gem::Dependency
77
+ name: httpclient
78
+ requirement: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ~>
82
+ - !ruby/object:Gem::Version
83
+ version: 2.3.4
84
+ type: :runtime
85
+ prerelease: false
86
+ version_requirements: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ version: 2.3.4
76
92
  - !ruby/object:Gem::Dependency
77
93
  name: rspec
78
94
  requirement: !ruby/object:Gem::Requirement