44require 'big_query/client/tables'
55require 'big_query/client/datasets'
66require '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
812module 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
8081end
0 commit comments