8
8
9
9
import httplib2
10
10
import six
11
- from apiclient .discovery import build
11
+ from apiclient .discovery import build , DISCOVERY_URI
12
12
from apiclient .errors import HttpError
13
13
14
14
from bigquery .errors import (BigQueryTimeoutException , JobExecutingException ,
43
43
JOB_DESTINATION_FORMAT_CSV = JOB_FORMAT_CSV
44
44
45
45
46
- def get_client (project_id , credentials = None , service_account = None ,
46
+ def get_client (project_id , credentials = None ,
47
+ service_url = None , service_account = None ,
47
48
private_key = None , private_key_file = None ,
48
49
json_key = None , json_key_file = None ,
49
50
readonly = True , swallow_results = True ):
@@ -55,6 +56,12 @@ def get_client(project_id, credentials=None, service_account=None,
55
56
project_id: the BigQuery project id.
56
57
credentials: an AssertionCredentials instance to authenticate requests
57
58
to BigQuery.
59
+ service_url: a URI string template pointing to the location of
60
+ Google's API discovery service. Requires two parameters
61
+ {api} and {apiVersion} that when filled in produce an
62
+ absolute URI to the discovery document for that service.
63
+ If not set then the default googleapiclient disovery URI
64
+ is used.
58
65
service_account: the Google API service account name.
59
66
private_key: the private key associated with the service account in
60
67
PKCS12 or PEM format.
@@ -77,6 +84,9 @@ def get_client(project_id, credentials=None, service_account=None,
77
84
assert (service_account and (private_key or private_key_file )) or (json_key or json_key_file ), \
78
85
'Must provide AssertionCredentials or service account and P12 key or JSON key'
79
86
87
+ if service_url is None :
88
+ service_url = DISCOVERY_URI
89
+
80
90
if private_key_file :
81
91
with open (private_key_file , 'rb' ) as key_file :
82
92
private_key = key_file .read ()
@@ -90,14 +100,15 @@ def get_client(project_id, credentials=None, service_account=None,
90
100
private_key = json_key ['private_key' ]
91
101
92
102
bq_service = _get_bq_service (credentials = credentials ,
103
+ service_url = service_url ,
93
104
service_account = service_account ,
94
105
private_key = private_key ,
95
106
readonly = readonly )
96
107
97
108
return BigQueryClient (bq_service , project_id , swallow_results )
98
109
99
110
100
- def _get_bq_service (credentials = None , service_account = None , private_key = None ,
111
+ def _get_bq_service (credentials = None , service_url = None , service_account = None , private_key = None ,
101
112
readonly = True ):
102
113
"""Construct an authorized BigQuery service object."""
103
114
@@ -110,7 +121,7 @@ def _get_bq_service(credentials=None, service_account=None, private_key=None,
110
121
111
122
http = httplib2 .Http ()
112
123
http = credentials .authorize (http )
113
- service = build ('bigquery' , 'v2' , http = http )
124
+ service = build ('bigquery' , 'v2' , http = http , discoveryServiceUrl = service_url )
114
125
115
126
return service
116
127
0 commit comments