Skip to content

Commit d4bf896

Browse files
committed
Merge pull request abronte#38 from kysnm/change_google_api_client_0_9_3
Change google api client 0.9.3
2 parents 92152c0 + d84cedf commit d4bf896

File tree

14 files changed

+328
-192
lines changed

14 files changed

+328
-192
lines changed

Gemfile.lock

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,54 @@ PATH
22
remote: .
33
specs:
44
bigquery (0.9.0)
5-
google-api-client (= 0.8.6)
5+
google-api-client (= 0.9.3)
66

77
GEM
88
remote: https://rubygems.org/
99
specs:
10-
activesupport (4.2.5)
11-
i18n (~> 0.7)
12-
json (~> 1.7, >= 1.7.7)
13-
minitest (~> 5.1)
14-
thread_safe (~> 0.3, >= 0.3.4)
15-
tzinfo (~> 1.1)
16-
addressable (2.3.8)
17-
autoparse (0.3.3)
18-
addressable (>= 2.3.1)
19-
extlib (>= 0.9.15)
20-
multi_json (>= 1.0.0)
10+
addressable (2.4.0)
2111
byebug (2.7.0)
2212
columnize (~> 0.3)
2313
debugger-linecache (~> 1.2)
2414
coderay (1.1.0)
2515
columnize (0.8.9)
2616
debugger-linecache (1.2.0)
27-
extlib (0.9.16)
28-
faraday (0.9.1)
17+
faraday (0.9.2)
2918
multipart-post (>= 1.2, < 3)
30-
google-api-client (0.8.6)
31-
activesupport (>= 3.2)
19+
google-api-client (0.9.3)
3220
addressable (~> 2.3)
33-
autoparse (~> 0.3)
34-
extlib (~> 0.9)
35-
faraday (~> 0.9)
36-
googleauth (~> 0.3)
37-
launchy (~> 2.4)
38-
multi_json (~> 1.10)
39-
retriable (~> 1.4)
40-
signet (~> 0.6)
41-
googleauth (0.4.1)
21+
googleauth (~> 0.5)
22+
httpclient (~> 2.7)
23+
hurley (~> 0.1)
24+
memoist (~> 0.11)
25+
mime-types (>= 1.6)
26+
representable (~> 2.3.0)
27+
retriable (~> 2.0)
28+
thor (~> 0.19)
29+
googleauth (0.5.1)
4230
faraday (~> 0.9)
4331
jwt (~> 1.4)
4432
logging (~> 2.0)
4533
memoist (~> 0.12)
46-
multi_json (= 1.11)
47-
signet (~> 0.6)
48-
i18n (0.7.0)
49-
json (1.8.3)
50-
jwt (1.5.1)
51-
launchy (2.4.3)
52-
addressable (~> 2.3)
53-
little-plugger (1.1.3)
34+
multi_json (~> 1.11)
35+
os (~> 0.9)
36+
signet (~> 0.7)
37+
httpclient (2.7.1)
38+
hurley (0.2)
39+
jwt (1.5.3)
40+
little-plugger (1.1.4)
5441
logging (2.0.0)
5542
little-plugger (~> 1.1)
5643
multi_json (~> 1.10)
57-
memoist (0.12.0)
44+
memoist (0.14.0)
5845
method_source (0.8.2)
46+
mime-types (3.0)
47+
mime-types-data (~> 3.2015)
48+
mime-types-data (3.2016.0221)
5949
minitest (5.3.4)
60-
multi_json (1.11.0)
50+
multi_json (1.11.2)
6151
multipart-post (2.0.0)
52+
os (0.9.6)
6253
pry (0.9.12.6)
6354
coderay (~> 1.0)
6455
method_source (~> 0.8)
@@ -67,17 +58,17 @@ GEM
6758
byebug (~> 2.7)
6859
pry (~> 0.9.12)
6960
rake (10.3.2)
70-
retriable (1.4.1)
71-
signet (0.6.1)
61+
representable (2.3.0)
62+
uber (~> 0.0.7)
63+
retriable (2.1.0)
64+
signet (0.7.2)
7265
addressable (~> 2.3)
73-
extlib (~> 0.9)
7466
faraday (~> 0.9)
7567
jwt (~> 1.5)
7668
multi_json (~> 1.10)
7769
slop (3.5.0)
78-
thread_safe (0.3.5)
79-
tzinfo (1.2.2)
80-
thread_safe (~> 0.1)
70+
thor (0.19.1)
71+
uber (0.0.15)
8172

8273
PLATFORMS
8374
ruby
@@ -88,3 +79,6 @@ DEPENDENCIES
8879
minitest
8980
pry-byebug
9081
rake
82+
83+
BUNDLED WITH
84+
1.11.2

bigquery.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
1515
s.files = `git ls-files`.split("\n")
1616
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
1717

18-
s.add_dependency "google-api-client", "0.8.6"
18+
s.add_dependency "google-api-client", "0.9.3"
1919

2020
s.add_development_dependency "bundler"
2121
s.add_development_dependency "rake"

lib/big_query.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'json'
2-
require 'google/api_client'
2+
require 'google/apis/bigquery_v2'
3+
require 'google/api_client/auth/key_utils'
34
require 'big_query/version'
45
require 'big_query/errors'
56
require 'big_query/client'

lib/big_query/client.rb

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
require 'big_query/client/tables'
55
require 'big_query/client/datasets'
66
require 'big_query/client/load'
7+
require 'big_query/client/hashable'
8+
require 'big_query/client/options'
9+
require 'big_query/client/response'
10+
require 'big_query/client/job_types'
711

812
module BigQuery
913
class Client
@@ -13,15 +17,35 @@ class Client
1317
include BigQuery::Client::Tables
1418
include BigQuery::Client::Datasets
1519
include BigQuery::Client::Insert
20+
include BigQuery::Client::Hashable
21+
include BigQuery::Client::Options
22+
include BigQuery::Client::Response
23+
include BigQuery::Client::JobTypes
1624

1725
attr_accessor :dataset, :project_id
1826

1927
def initialize(opts = {})
20-
@client = Google::APIClient.new(
21-
application_name: 'BigQuery ruby app',
22-
application_version: BigQuery::VERSION,
23-
faraday_option: opts['faraday_option']
24-
)
28+
# for debug
29+
# Google::Apis.logger.level = Logger::DEBUG
30+
31+
@client = Google::Apis::BigqueryV2::BigqueryService.new
32+
33+
@client.client_options.application_name = 'BigQuery ruby app'
34+
@client.client_options.application_version = BigQuery::VERSION
35+
36+
# Memo:
37+
# The google-api-client 0.9 is HTTP Client no longer in Faraday
38+
# We accepts the options for backward compatibility
39+
# (HTTP Client is replaced in the near future HTTP::Client...)
40+
# https://github.com/google/google-api-ruby-client/issues/336#issuecomment-179400592
41+
if opts['faraday_option'].is_a?(Hash)
42+
@client.request_options.timeout_sec = opts['faraday_option']['timeout']
43+
@client.request_options.open_timeout_sec = opts['faraday_option']['open_timeout']
44+
# We accept the request_option instead of faraday_option
45+
elsif opts['request_option'].is_a?(Hash)
46+
@client.request_options.timeout_sec = opts['request_option']['timeout_sec']
47+
@client.request_options.open_timeout_sec = opts['request_option']['open_timeout_sec']
48+
end
2549

2650
begin
2751
key = Google::APIClient::KeyUtils.load_from_pkcs12(opts['key'], 'notasecret')
@@ -38,8 +62,6 @@ def initialize(opts = {})
3862

3963
refresh_auth
4064

41-
@bq = @client.discovered_api("bigquery", "v2")
42-
4365
@project_id = opts['project_id']
4466
@dataset = opts['dataset']
4567
end
@@ -50,31 +72,10 @@ def refresh_auth
5072

5173
private
5274

53-
# Performs the api calls with the given params adding the defined project and
54-
# dataset params if not defined
55-
#
56-
# @param opts [Hash] options for the api call
57-
# @return [Hash] json response
58-
def api(opts)
59-
if opts[:parameters]
60-
opts[:parameters] = opts[:parameters].merge({"projectId" => @project_id})
61-
else
62-
opts[:parameters] = {"projectId" => @project_id}
63-
end
64-
65-
resp = @client.execute(opts)
66-
data = parse_body(resp)
75+
def api(resp)
76+
data = deep_stringify_keys(resp.to_h)
6777
handle_error(data) if data && is_error?(data)
6878
data
6979
end
70-
71-
# Parses json body if present and is a json formatted
72-
#
73-
# @param resp [Faraday::Response] response object
74-
# @return [Hash]
75-
def parse_body(resp)
76-
return nil unless resp.body && !resp.body.empty?
77-
JSON.parse(resp.body)
78-
end
7980
end
8081
end

lib/big_query/client/datasets.rb

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ module Datasets
66
#
77
# @return [Hash] json api response
88
def datasets(parameters = {})
9-
response = api({
10-
api_method: @bq.datasets.list,
11-
parameters: parameters
12-
})
13-
9+
response = api(
10+
@client.list_datasets(
11+
@project_id,
12+
parameters
13+
)
14+
)
1415
response['datasets'] || []
1516
end
1617

@@ -29,23 +30,27 @@ def datasets_formatted(parameters = {})
2930
# examples:
3031
#
3132
# @bq.create_dataset('new_dataset')
32-
def create_dataset(datasetId)
33+
def create_dataset(dataset_id)
34+
dataset = Google::Apis::BigqueryV2::Dataset.new(
35+
dataset_reference: { project_id: @project_id, dataset_id: dataset_id }
36+
)
3337
api(
34-
api_method: @bq.datasets.insert,
35-
body_object: { "datasetReference" => {
36-
"datasetId" => datasetId,
37-
"projectId" => @project_id,
38-
}
39-
}
38+
@client.insert_dataset(
39+
@project_id,
40+
dataset
41+
)
4042
)
4143
end
4244

4345
# Deletes the given datasetId
4446
#
4547
# @param datasetId [String] dataset id to insert into
46-
def delete_dataset(datasetId)
47-
api(api_method: @bq.datasets.delete,
48-
parameters: { 'datasetId' => datasetId }
48+
def delete_dataset(dataset_id)
49+
api(
50+
@client.delete_dataset(
51+
@project_id,
52+
dataset_id
53+
)
4954
)
5055
end
5156
end

lib/big_query/client/hashable.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module BigQuery
2+
class Client
3+
module Hashable
4+
5+
def process_value(val, convert_key_proc)
6+
case val
7+
when Hash
8+
Hash[val.map {|k, v| [convert_key_proc.call(k), process_value(v, convert_key_proc)] }]
9+
when Array
10+
val.map{ |v| process_value(v, convert_key_proc) }
11+
else
12+
val
13+
end
14+
end
15+
end
16+
end
17+
end

lib/big_query/client/job_types.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module BigQuery
2+
class Client
3+
module JobTypes
4+
5+
def _copy(opts)
6+
_opts = opts.dup
7+
if (_opts[:source_tables])
8+
_opts[:source_tables] = _opts[:source_tables].dup.map { |source_table| Google::Apis::BigqueryV2::TableReference.new(source_table) }
9+
else
10+
_opts[:source_table] = Google::Apis::BigqueryV2::TableReference.new(_opts[:source_table])
11+
end
12+
_opts[:destination_table] = Google::Apis::BigqueryV2::TableReference.new(_opts[:destination_table])
13+
14+
Google::Apis::BigqueryV2::JobConfigurationTableCopy.new(
15+
_opts
16+
)
17+
end
18+
19+
def _extract(opts)
20+
_opts = opts.dup
21+
_opts[:source_table] = Google::Apis::BigqueryV2::TableReference.new(_opts[:source_table])
22+
Google::Apis::BigqueryV2::JobConfigurationExtract.new(
23+
_opts
24+
)
25+
end
26+
27+
def _load(opts)
28+
_opts = opts.dup
29+
_opts[:destination_table] = Google::Apis::BigqueryV2::TableReference.new(_opts[:destination_table])
30+
_opts[:schema] = Google::Apis::BigqueryV2::TableSchema.new(_opts[:schema]) if _opts[:schema]
31+
Google::Apis::BigqueryV2::JobConfigurationLoad.new(
32+
_opts
33+
)
34+
end
35+
36+
def _query(opts)
37+
_opts = opts.dup
38+
Google::Apis::BigqueryV2::JobConfigurationQuery.new(
39+
_opts
40+
)
41+
end
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)