Skip to content

Commit 6dff50f

Browse files
fix: supplementary fix to env-based universe resolution (#1847)
* fix: promote env-based universe into client option parsing * lint * add client test * import --------- Co-authored-by: Chalmer Lowe <[email protected]>
1 parent 713ce2c commit 6dff50f

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

google/cloud/bigquery/client.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -249,20 +249,19 @@ def __init__(
249249
bq_host = _get_bigquery_host()
250250
kw_args["api_endpoint"] = bq_host if bq_host != _DEFAULT_HOST else None
251251
client_universe = None
252-
if client_options:
253-
if isinstance(client_options, dict):
254-
client_options = google.api_core.client_options.from_dict(
255-
client_options
252+
if client_options is None:
253+
client_options = {}
254+
if isinstance(client_options, dict):
255+
client_options = google.api_core.client_options.from_dict(client_options)
256+
if client_options.api_endpoint:
257+
api_endpoint = client_options.api_endpoint
258+
kw_args["api_endpoint"] = api_endpoint
259+
else:
260+
client_universe = _get_client_universe(client_options)
261+
if client_universe != _DEFAULT_UNIVERSE:
262+
kw_args["api_endpoint"] = _DEFAULT_HOST_TEMPLATE.replace(
263+
"{UNIVERSE_DOMAIN}", client_universe
256264
)
257-
if client_options.api_endpoint:
258-
api_endpoint = client_options.api_endpoint
259-
kw_args["api_endpoint"] = api_endpoint
260-
else:
261-
client_universe = _get_client_universe(client_options)
262-
if client_universe != _DEFAULT_UNIVERSE:
263-
kw_args["api_endpoint"] = _DEFAULT_HOST_TEMPLATE.replace(
264-
"{UNIVERSE_DOMAIN}", client_universe
265-
)
266265
# Ensure credentials and universe are not in conflict.
267266
if hasattr(self, "_credentials") and client_universe is not None:
268267
_validate_universe(client_universe, self._credentials)

tests/unit/test_client.py

+12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import itertools
2424
import json
2525
import operator
26+
import os
2627
import unittest
2728
from unittest import mock
2829
import warnings
@@ -171,6 +172,17 @@ def test_ctor_w_empty_client_options(self):
171172
client._connection.API_BASE_URL, client._connection.DEFAULT_API_ENDPOINT
172173
)
173174

175+
@mock.patch.dict(os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"})
176+
def test_ctor_w_only_env_universe(self):
177+
creds = _make_credentials()
178+
http = object()
179+
client = self._make_one(
180+
project=self.PROJECT,
181+
credentials=creds,
182+
_http=http,
183+
)
184+
self.assertEqual(client._connection.API_BASE_URL, "https://bigquery.foo.com")
185+
174186
def test_ctor_w_client_options_dict(self):
175187
creds = _make_credentials()
176188
http = object()

0 commit comments

Comments
 (0)