td-client 1.0.0-java

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.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/data/ca-bundle.crt +3448 -0
  3. data/lib/td-client.rb +1 -0
  4. data/lib/td/client.rb +606 -0
  5. data/lib/td/client/api.rb +707 -0
  6. data/lib/td/client/api/access_control.rb +74 -0
  7. data/lib/td/client/api/account.rb +45 -0
  8. data/lib/td/client/api/bulk_import.rb +184 -0
  9. data/lib/td/client/api/bulk_load.rb +172 -0
  10. data/lib/td/client/api/database.rb +50 -0
  11. data/lib/td/client/api/export.rb +38 -0
  12. data/lib/td/client/api/import.rb +38 -0
  13. data/lib/td/client/api/job.rb +390 -0
  14. data/lib/td/client/api/partial_delete.rb +27 -0
  15. data/lib/td/client/api/result.rb +46 -0
  16. data/lib/td/client/api/schedule.rb +120 -0
  17. data/lib/td/client/api/server_status.rb +21 -0
  18. data/lib/td/client/api/table.rb +132 -0
  19. data/lib/td/client/api/user.rb +134 -0
  20. data/lib/td/client/api_error.rb +37 -0
  21. data/lib/td/client/compat_gzip_reader.rb +22 -0
  22. data/lib/td/client/model.rb +816 -0
  23. data/lib/td/client/version.rb +5 -0
  24. data/lib/td/core_ext/openssl/ssl/sslcontext/set_params.rb +18 -0
  25. data/spec/spec_helper.rb +63 -0
  26. data/spec/td/client/access_control_api_spec.rb +37 -0
  27. data/spec/td/client/account_api_spec.rb +34 -0
  28. data/spec/td/client/api_error_spec.rb +77 -0
  29. data/spec/td/client/api_spec.rb +269 -0
  30. data/spec/td/client/api_ssl_connection_spec.rb +109 -0
  31. data/spec/td/client/bulk_import_spec.rb +199 -0
  32. data/spec/td/client/bulk_load_spec.rb +401 -0
  33. data/spec/td/client/db_api_spec.rb +123 -0
  34. data/spec/td/client/export_api_spec.rb +51 -0
  35. data/spec/td/client/import_api_spec.rb +148 -0
  36. data/spec/td/client/job_api_spec.rb +833 -0
  37. data/spec/td/client/model_job_spec.rb +136 -0
  38. data/spec/td/client/model_schedule_spec.rb +26 -0
  39. data/spec/td/client/model_schema_spec.rb +134 -0
  40. data/spec/td/client/partial_delete_api_spec.rb +58 -0
  41. data/spec/td/client/result_api_spec.rb +77 -0
  42. data/spec/td/client/sched_api_spec.rb +109 -0
  43. data/spec/td/client/server_status_api_spec.rb +25 -0
  44. data/spec/td/client/spec_resources.rb +99 -0
  45. data/spec/td/client/table_api_spec.rb +226 -0
  46. data/spec/td/client/user_api_spec.rb +118 -0
  47. data/spec/td/client_sched_spec.rb +79 -0
  48. data/spec/td/client_spec.rb +46 -0
  49. metadata +271 -0
@@ -0,0 +1,27 @@
1
+ class TreasureData::API
2
+ module PartialDelete
3
+
4
+ ####
5
+ ## Partial delete API
6
+ ##
7
+
8
+ # @param [String] db
9
+ # @param [String] table
10
+ # @param [Fixnum] to
11
+ # @param [Fixnum] from
12
+ # @param [Hash] opts
13
+ # @return [String]
14
+ def partial_delete(db, table, to, from, opts={})
15
+ params = opts.dup
16
+ params['to'] = to.to_s
17
+ params['from'] = from.to_s
18
+ code, body, res = post("/v3/table/partialdelete/#{e db}/#{e table}", params)
19
+ if code != "200"
20
+ raise_error("Partial delete failed", res)
21
+ end
22
+ js = checked_json(body, %w[job_id])
23
+ return js['job_id'].to_s
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,46 @@
1
+ class TreasureData::API
2
+ module Result
3
+
4
+ ####
5
+ ## Result API
6
+ ##
7
+
8
+ # @return [Array<String>]
9
+ def list_result
10
+ code, body, res = get("/v3/result/list")
11
+ if code != "200"
12
+ raise_error("List result table failed", res)
13
+ end
14
+ js = checked_json(body, %w[results])
15
+ result = []
16
+ js['results'].map {|m|
17
+ result << [m['name'], m['url'], nil] # same as database
18
+ }
19
+ return result
20
+ end
21
+
22
+ # @param [String] name
23
+ # @param [String] url
24
+ # @param [Hash] opts
25
+ # @return [true]
26
+ def create_result(name, url, opts={})
27
+ params = {'url'=>url}.merge(opts)
28
+ code, body, res = post("/v3/result/create/#{e name}", params)
29
+ if code != "200"
30
+ raise_error("Create result table failed", res)
31
+ end
32
+ return true
33
+ end
34
+
35
+ # @param [String] name
36
+ # @return [true]
37
+ def delete_result(name)
38
+ code, body, res = post("/v3/result/delete/#{e name}")
39
+ if code != "200"
40
+ raise_error("Delete result table failed", res)
41
+ end
42
+ return true
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,120 @@
1
+ class TreasureData::API
2
+ module Schedule
3
+
4
+ ####
5
+ ## Schedule API
6
+ ##
7
+
8
+ # @param [String] name
9
+ # @param [Hash] opts
10
+ # @return [String]
11
+ def create_schedule(name, opts)
12
+ params = opts.update({:type=> opts[:type] || opts['type'] || 'hive'})
13
+ code, body, res = post("/v3/schedule/create/#{e name}", params)
14
+ if code != "200"
15
+ raise_error("Create schedule failed", res)
16
+ end
17
+ js = checked_json(body)
18
+ return js['start']
19
+ end
20
+
21
+ # @param [String] name
22
+ # @return [Array]
23
+ def delete_schedule(name)
24
+ code, body, res = post("/v3/schedule/delete/#{e name}")
25
+ if code != "200"
26
+ raise_error("Delete schedule failed", res)
27
+ end
28
+ js = checked_json(body, %w[])
29
+ return js['cron'], js["query"]
30
+ end
31
+
32
+ # @return [Array]
33
+ def list_schedules
34
+ code, body, res = get("/v3/schedule/list")
35
+ if code != "200"
36
+ raise_error("List schedules failed", res)
37
+ end
38
+ js = checked_json(body, %w[schedules])
39
+ result = []
40
+ js['schedules'].each {|m|
41
+ name = m['name']
42
+ cron = m['cron']
43
+ query = m['query']
44
+ database = m['database']
45
+ result_url = m['result']
46
+ timezone = m['timezone']
47
+ delay = m['delay']
48
+ next_time = m['next_time']
49
+ priority = m['priority']
50
+ retry_limit = m['retry_limit']
51
+ result << [name, cron, query, database, result_url, timezone, delay, next_time, priority, retry_limit, nil] # same as database
52
+ }
53
+ return result
54
+ end
55
+
56
+ # @param [String] name
57
+ # @param [Hash] params
58
+ # @return [nil]
59
+ def update_schedule(name, params)
60
+ code, body, res = post("/v3/schedule/update/#{e name}", params)
61
+ if code != "200"
62
+ raise_error("Update schedule failed", res)
63
+ end
64
+ return nil
65
+ end
66
+
67
+ # @param [String] name
68
+ # @param [Fixnum] from
69
+ # @param [Fixnum] to
70
+ # @return [Array]
71
+ def history(name, from=0, to=nil)
72
+ params = {}
73
+ params['from'] = from.to_s if from
74
+ params['to'] = to.to_s if to
75
+ code, body, res = get("/v3/schedule/history/#{e name}", params)
76
+ if code != "200"
77
+ raise_error("List history failed", res)
78
+ end
79
+ js = checked_json(body, %w[history])
80
+ result = []
81
+ js['history'].each {|m|
82
+ job_id = m['job_id']
83
+ type = (m['type'] || '?').to_sym
84
+ database = m['database']
85
+ status = m['status']
86
+ query = m['query']
87
+ start_at = m['start_at']
88
+ end_at = m['end_at']
89
+ scheduled_at = m['scheduled_at']
90
+ result_url = m['result']
91
+ priority = m['priority']
92
+ result << [scheduled_at, job_id, type, status, query, start_at, end_at, result_url, priority, database]
93
+ }
94
+ return result
95
+ end
96
+
97
+ # @param [String] name
98
+ # @param [String] time
99
+ # @param [Fixnum] num
100
+ # @return [Array]
101
+ def run_schedule(name, time, num)
102
+ params = {}
103
+ params = {'num' => num} if num
104
+ code, body, res = post("/v3/schedule/run/#{e name}/#{e time}", params)
105
+ if code != "200"
106
+ raise_error("Run schedule failed", res)
107
+ end
108
+ js = checked_json(body, %w[jobs])
109
+ result = []
110
+ js['jobs'].each {|m|
111
+ job_id = m['job_id']
112
+ scheduled_at = m['scheduled_at']
113
+ type = (m['type'] || '?').to_sym
114
+ result << [job_id, type, scheduled_at]
115
+ }
116
+ return result
117
+ end
118
+
119
+ end
120
+ end
@@ -0,0 +1,21 @@
1
+ class TreasureData::API
2
+ module ServerStatus
3
+
4
+ ####
5
+ ## Server Status API
6
+ ##
7
+
8
+ # => status:String
9
+ # @return [String] HTTP status code
10
+ def server_status
11
+ code, body, res = get('/v3/system/server_status')
12
+ if code != "200"
13
+ return "Server is down (#{code})"
14
+ end
15
+ js = checked_json(body, %w[status])
16
+ status = js['status']
17
+ return status
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,132 @@
1
+ class TreasureData::API
2
+ module Table
3
+
4
+ ####
5
+ ## Table API
6
+ ##
7
+
8
+ # @param [String] db
9
+ # @return [Array]
10
+ def list_tables(db)
11
+ code, body, res = get("/v3/table/list/#{e db}")
12
+ if code != "200"
13
+ raise_error("List tables failed", res)
14
+ end
15
+ js = checked_json(body, %w[tables])
16
+ result = {}
17
+ js["tables"].map {|m|
18
+ name = m['name']
19
+ type = (m['type'] || '?').to_sym
20
+ count = (m['count'] || 0).to_i # TODO?
21
+ created_at = m['created_at']
22
+ updated_at = m['updated_at']
23
+ last_import = m['counter_updated_at']
24
+ last_log_timestamp = m['last_log_timestamp']
25
+ estimated_storage_size = m['estimated_storage_size'].to_i
26
+ schema = JSON.parse(m['schema'] || '[]')
27
+ expire_days = m['expire_days']
28
+ result[name] = [type, schema, count, created_at, updated_at, estimated_storage_size, last_import, last_log_timestamp, expire_days]
29
+ }
30
+ return result
31
+ end
32
+
33
+ # @param [String] db
34
+ # @param [String] table
35
+ # @return [true]
36
+ def create_log_table(db, table)
37
+ create_table(db, table, :log)
38
+ end
39
+
40
+ # @param [String] db
41
+ # @param [String] table
42
+ # @param [String] type
43
+ # @param [Hash] params
44
+ # @return [true]
45
+ def create_table(db, table, type, params={})
46
+ schema = schema.to_s
47
+ code, body, res = post("/v3/table/create/#{e db}/#{e table}/#{type}", params)
48
+ if code != "200"
49
+ raise_error("Create #{type} table failed", res)
50
+ end
51
+ return true
52
+ end
53
+ private :create_table
54
+
55
+ # @param [String] db
56
+ # @param [String] table1
57
+ # @param [String] table2
58
+ # @return [true]
59
+ def swap_table(db, table1, table2)
60
+ code, body, res = post("/v3/table/swap/#{e db}/#{e table1}/#{e table2}")
61
+ if code != "200"
62
+ raise_error("Swap tables failed", res)
63
+ end
64
+ return true
65
+ end
66
+
67
+ # @param [String] db
68
+ # @param [String] table
69
+ # @param [String] schema_json
70
+ # @return [true]
71
+ def update_schema(db, table, schema_json)
72
+ code, body, res = post("/v3/table/update-schema/#{e db}/#{e table}", {'schema'=>schema_json})
73
+ if code != "200"
74
+ raise_error("Create schema table failed", res)
75
+ end
76
+ return true
77
+ end
78
+
79
+ # @param [String] db
80
+ # @param [String] table
81
+ # @param [Fixnum] expire_days
82
+ # @return [true]
83
+ def update_expire(db, table, expire_days)
84
+ code, body, res = post("/v3/table/update/#{e db}/#{e table}", {'expire_days'=>expire_days})
85
+ if code != "200"
86
+ raise_error("Update table expiration failed", res)
87
+ end
88
+ return true
89
+ end
90
+
91
+ # @param [String] db
92
+ # @param [String] table
93
+ # @return [Symbol]
94
+ def delete_table(db, table)
95
+ code, body, res = post("/v3/table/delete/#{e db}/#{e table}")
96
+ if code != "200"
97
+ raise_error("Delete table failed", res)
98
+ end
99
+ js = checked_json(body, %w[])
100
+ type = (js['type'] || '?').to_sym
101
+ return type
102
+ end
103
+
104
+ # @param [String] db
105
+ # @param [String] table
106
+ # @param [Fixnum] count
107
+ # @param [Proc] block
108
+ # @return [Array, nil]
109
+ def tail(db, table, count, to = nil, from = nil, &block)
110
+ unless to.nil? and from.nil?
111
+ warn('parameter "to" and "from" no longer work')
112
+ end
113
+ params = {'format' => 'msgpack'}
114
+ params['count'] = count.to_s if count
115
+ code, body, res = get("/v3/table/tail/#{e db}/#{e table}", params)
116
+ if code != "200"
117
+ raise_error("Tail table failed", res)
118
+ end
119
+ if block
120
+ MessagePack::Unpacker.new.feed_each(body, &block)
121
+ nil
122
+ else
123
+ result = []
124
+ MessagePack::Unpacker.new.feed_each(body) {|row|
125
+ result << row
126
+ }
127
+ return result
128
+ end
129
+ end
130
+
131
+ end
132
+ end
@@ -0,0 +1,134 @@
1
+ class TreasureData::API
2
+ module User
3
+
4
+ ####
5
+ ## User API
6
+ ##
7
+
8
+ # @param [String] user
9
+ # @param [String] password
10
+ # @return [String] API key
11
+ def authenticate(user, password)
12
+ code, body, res = post("/v3/user/authenticate", {'user'=>user, 'password'=>password})
13
+ if code != "200"
14
+ if code == "400"
15
+ raise_error("Authentication failed", res, AuthError)
16
+ else
17
+ raise_error("Authentication failed", res)
18
+ end
19
+ end
20
+ js = checked_json(body, %w[apikey])
21
+ apikey = js['apikey']
22
+ return apikey
23
+ end
24
+
25
+ # @return [Array]
26
+ def list_users
27
+ code, body, res = get("/v3/user/list")
28
+ if code != "200"
29
+ raise_error("List users failed", res)
30
+ end
31
+ js = checked_json(body, %w[users])
32
+ result = js["users"].map {|roleinfo|
33
+ name = roleinfo['name']
34
+ email = roleinfo['email']
35
+ [name, nil, nil, email] # set nil to org and role for API compatibility
36
+ }
37
+ return result
38
+ end
39
+
40
+ # @param [String] name
41
+ # @param [String] org
42
+ # @param [String] email
43
+ # @param [String] password
44
+ # @return [true]
45
+ def add_user(name, org, email, password)
46
+ params = {'organization'=>org, :email=>email, :password=>password}
47
+ code, body, res = post("/v3/user/add/#{e name}", params)
48
+ if code != "200"
49
+ raise_error("Adding user failed", res)
50
+ end
51
+ return true
52
+ end
53
+
54
+ # @param [String] user
55
+ # @return [true]
56
+ def remove_user(user)
57
+ code, body, res = post("/v3/user/remove/#{e user}")
58
+ if code != "200"
59
+ raise_error("Removing user failed", res)
60
+ end
61
+ return true
62
+ end
63
+
64
+ # @param [String] user
65
+ # @param [String] email
66
+ # @return [true]
67
+ def change_email(user, email)
68
+ params = {'email' => email}
69
+ code, body, res = post("/v3/user/email/change/#{e user}", params)
70
+ if code != "200"
71
+ raise_error("Changing email failed", res)
72
+ end
73
+ return true
74
+ end
75
+
76
+ # @param [String] user
77
+ # @return [Array<String>] API keys as array
78
+ def list_apikeys(user)
79
+ code, body, res = get("/v3/user/apikey/list/#{e user}")
80
+ if code != "200"
81
+ raise_error("List API keys failed", res)
82
+ end
83
+ js = checked_json(body, %w[apikeys])
84
+ return js['apikeys']
85
+ end
86
+
87
+ # @param [String] user
88
+ # @return [true]
89
+ def add_apikey(user)
90
+ code, body, res = post("/v3/user/apikey/add/#{e user}")
91
+ if code != "200"
92
+ raise_error("Adding API key failed", res)
93
+ end
94
+ return true
95
+ end
96
+
97
+ # @param [String] user
98
+ # @param [String] apikey
99
+ # @return [true]
100
+ def remove_apikey(user, apikey)
101
+ params = {'apikey' => apikey}
102
+ code, body, res = post("/v3/user/apikey/remove/#{e user}", params)
103
+ if code != "200"
104
+ raise_error("Removing API key failed", res)
105
+ end
106
+ return true
107
+ end
108
+
109
+ # @param [String] user
110
+ # @param [String] password
111
+ # @return [true]
112
+ def change_password(user, password)
113
+ params = {'password' => password}
114
+ code, body, res = post("/v3/user/password/change/#{e user}", params)
115
+ if code != "200"
116
+ raise_error("Changing password failed", res)
117
+ end
118
+ return true
119
+ end
120
+
121
+ # @param [String] old_password
122
+ # @param [String] password
123
+ # @return [true]
124
+ def change_my_password(old_password, password)
125
+ params = {'old_password' => old_password, 'password' => password}
126
+ code, body, res = post("/v3/user/password/change", params)
127
+ if code != "200"
128
+ raise_error("Changing password failed", res)
129
+ end
130
+ return true
131
+ end
132
+
133
+ end
134
+ end