Skip to content

Commit 15b066c

Browse files
committed
Merge pull request #81 from andrewgardners/master
Add service_url parameter to get_client and _get_bq_service
2 parents 4ab6674 + 12231de commit 15b066c

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

bigquery/client.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import httplib2
1010
import six
11-
from apiclient.discovery import build
11+
from apiclient.discovery import build, DISCOVERY_URI
1212
from apiclient.errors import HttpError
1313

1414
from bigquery.errors import (BigQueryTimeoutException, JobExecutingException,
@@ -43,7 +43,8 @@
4343
JOB_DESTINATION_FORMAT_CSV = JOB_FORMAT_CSV
4444

4545

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,
4748
private_key=None, private_key_file=None,
4849
json_key=None, json_key_file=None,
4950
readonly=True, swallow_results=True):
@@ -55,6 +56,12 @@ def get_client(project_id, credentials=None, service_account=None,
5556
project_id: the BigQuery project id.
5657
credentials: an AssertionCredentials instance to authenticate requests
5758
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.
5865
service_account: the Google API service account name.
5966
private_key: the private key associated with the service account in
6067
PKCS12 or PEM format.
@@ -77,6 +84,9 @@ def get_client(project_id, credentials=None, service_account=None,
7784
assert (service_account and (private_key or private_key_file)) or (json_key or json_key_file), \
7885
'Must provide AssertionCredentials or service account and P12 key or JSON key'
7986

87+
if service_url is None:
88+
service_url = DISCOVERY_URI
89+
8090
if private_key_file:
8191
with open(private_key_file, 'rb') as key_file:
8292
private_key = key_file.read()
@@ -90,14 +100,15 @@ def get_client(project_id, credentials=None, service_account=None,
90100
private_key = json_key['private_key']
91101

92102
bq_service = _get_bq_service(credentials=credentials,
103+
service_url=service_url,
93104
service_account=service_account,
94105
private_key=private_key,
95106
readonly=readonly)
96107

97108
return BigQueryClient(bq_service, project_id, swallow_results)
98109

99110

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,
101112
readonly=True):
102113
"""Construct an authorized BigQuery service object."""
103114

@@ -110,7 +121,7 @@ def _get_bq_service(credentials=None, service_account=None, private_key=None,
110121

111122
http = httplib2.Http()
112123
http = credentials.authorize(http)
113-
service = build('bigquery', 'v2', http=http)
124+
service = build('bigquery', 'v2', http=http, discoveryServiceUrl=service_url)
114125

115126
return service
116127

bigquery/tests/test_client.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def test_initialize_readonly(self, mock_build, mock_return_cred):
5050

5151
mock_cred = mock.Mock()
5252
mock_http = mock.Mock()
53+
mock_service_url = mock.Mock()
5354
mock_cred.return_value.authorize.return_value = mock_http
5455
mock_bq = mock.Mock()
5556
mock_build.return_value = mock_bq
@@ -59,14 +60,16 @@ def test_initialize_readonly(self, mock_build, mock_return_cred):
5960
mock_return_cred.return_value = mock_cred
6061

6162
bq_client = client.get_client(
62-
project_id, service_account=service_account, private_key=key,
63+
project_id, service_url=mock_service_url,
64+
service_account=service_account, private_key=key,
6365
readonly=True)
6466

6567
mock_return_cred.assert_called_once_with()
6668
mock_cred.assert_called_once_with(service_account, key,
6769
scope=BIGQUERY_SCOPE_READ_ONLY)
6870
self.assertTrue(mock_cred.return_value.authorize.called)
69-
mock_build.assert_called_once_with('bigquery', 'v2', http=mock_http)
71+
mock_build.assert_called_once_with('bigquery', 'v2', http=mock_http,
72+
discoveryServiceUrl=mock_service_url)
7073
self.assertEquals(mock_bq, bq_client.bigquery)
7174
self.assertEquals(project_id, bq_client.project_id)
7275

@@ -80,6 +83,7 @@ def test_initialize_read_write(self, mock_build, mock_return_cred):
8083

8184
mock_cred = mock.Mock()
8285
mock_http = mock.Mock()
86+
mock_service_url = mock.Mock()
8387
mock_cred.return_value.authorize.return_value = mock_http
8488
mock_bq = mock.Mock()
8589
mock_build.return_value = mock_bq
@@ -89,14 +93,16 @@ def test_initialize_read_write(self, mock_build, mock_return_cred):
8993
mock_return_cred.return_value = mock_cred
9094

9195
bq_client = client.get_client(
92-
project_id, service_account=service_account, private_key=key,
96+
project_id, service_url=mock_service_url,
97+
service_account=service_account, private_key=key,
9398
readonly=False)
9499

95100
mock_return_cred.assert_called_once_with()
96101
mock_cred.assert_called_once_with(service_account, key,
97102
scope=BIGQUERY_SCOPE)
98103
self.assertTrue(mock_cred.return_value.authorize.called)
99-
mock_build.assert_called_once_with('bigquery', 'v2', http=mock_http)
104+
mock_build.assert_called_once_with('bigquery', 'v2', http=mock_http,
105+
discoveryServiceUrl=mock_service_url)
100106
self.assertEquals(mock_bq, bq_client.bigquery)
101107
self.assertEquals(project_id, bq_client.project_id)
102108

@@ -112,6 +118,7 @@ def test_initialize_key_file(self, mock_open, mock_build,
112118

113119
mock_cred = mock.Mock()
114120
mock_http = mock.Mock()
121+
mock_service_url = mock.Mock()
115122
mock_cred.return_value.authorize.return_value = mock_http
116123
mock_bq = mock.Mock()
117124
mock_build.return_value = mock_bq
@@ -123,15 +130,17 @@ def test_initialize_key_file(self, mock_open, mock_build,
123130
mock_return_cred.return_value = mock_cred
124131

125132
bq_client = client.get_client(
126-
project_id, service_account=service_account,
133+
project_id, service_url=mock_service_url,
134+
service_account=service_account,
127135
private_key_file=key_file, readonly=False)
128136

129137
mock_open.assert_called_once_with(key_file, 'rb')
130138
mock_return_cred.assert_called_once_with()
131139
mock_cred.assert_called_once_with(service_account, key,
132140
scope=BIGQUERY_SCOPE)
133141
self.assertTrue(mock_cred.return_value.authorize.called)
134-
mock_build.assert_called_once_with('bigquery', 'v2', http=mock_http)
142+
mock_build.assert_called_once_with('bigquery', 'v2', http=mock_http,
143+
discoveryServiceUrl=mock_service_url)
135144
self.assertEquals(mock_bq, bq_client.bigquery)
136145
self.assertEquals(project_id, bq_client.project_id)
137146

@@ -147,6 +156,7 @@ def test_initialize_json_key_file(self, mock_open, mock_build, mock_return_cred)
147156

148157
mock_cred = mock.Mock()
149158
mock_http = mock.Mock()
159+
mock_service_url = mock.Mock()
150160
mock_cred.return_value.authorize.return_value = mock_http
151161
mock_bq = mock.Mock()
152162
mock_build.return_value = mock_bq
@@ -156,13 +166,14 @@ def test_initialize_json_key_file(self, mock_open, mock_build, mock_return_cred)
156166
project_id = 'project'
157167
mock_return_cred.return_value = mock_cred
158168

159-
bq_client = client.get_client(project_id, json_key_file=json_key_file, readonly=False)
169+
bq_client = client.get_client(
170+
project_id, service_url=mock_service_url, json_key_file=json_key_file, readonly=False)
160171

161172
mock_open.assert_called_once_with(json_key_file, 'r')
162173
mock_return_cred.assert_called_once_with()
163174
mock_cred.assert_called_once_with(json_key['client_email'], json_key['private_key'], scope=BIGQUERY_SCOPE)
164175
self.assertTrue(mock_cred.return_value.authorize.called)
165-
mock_build.assert_called_once_with('bigquery', 'v2', http=mock_http)
176+
mock_build.assert_called_once_with('bigquery', 'v2', http=mock_http, discoveryServiceUrl=mock_service_url)
166177
self.assertEquals(mock_bq, bq_client.bigquery)
167178
self.assertEquals(project_id, bq_client.project_id)
168179

0 commit comments

Comments
 (0)