Skip to content

Commit 39294b4

Browse files
feat: support using BIGQUERY_EMULATOR_HOST environment variable (#1222)
* feat:adding emulator * apb_feature_bq_emulator_test * apb_feature_bq_emulator_variable_name_change * abecerrilsalas_feature_bq_emulator_typo_changes * feat:fixing var names and start of test * abecerrilsalas_feature_additional_tests * feat: testing update * feat: fixed failed lint test
1 parent 9387e76 commit 39294b4

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

google/cloud/bigquery/_helpers.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import decimal
2020
import math
2121
import re
22+
import os
2223
from typing import Optional, Union
2324

2425
from dateutil import relativedelta
@@ -28,8 +29,8 @@
2829
from google.cloud._helpers import _RFC3339_MICROS
2930
from google.cloud._helpers import _RFC3339_NO_FRACTION
3031
from google.cloud._helpers import _to_bytes
31-
import packaging.version
3232

33+
import packaging.version
3334

3435
_RFC3339_MICROS_NO_ZULU = "%Y-%m-%dT%H:%M:%S.%f"
3536
_TIMEONLY_WO_MICROS = "%H:%M:%S"
@@ -51,6 +52,16 @@
5152

5253
_BQ_STORAGE_OPTIONAL_READ_SESSION_VERSION = packaging.version.Version("2.6.0")
5354

55+
BIGQUERY_EMULATOR_HOST = "BIGQUERY_EMULATOR_HOST"
56+
"""Environment variable defining host for emulator."""
57+
58+
_DEFAULT_HOST = "https://bigquery.googleapis.com"
59+
"""Default host for JSON API."""
60+
61+
62+
def _get_bigquery_host():
63+
return os.environ.get(BIGQUERY_EMULATOR_HOST, _DEFAULT_HOST)
64+
5465

5566
class BQStorageVersions:
5667
"""Version comparisons for google-cloud-bigqueyr-storage package."""

google/cloud/bigquery/client.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import google.cloud._helpers # type: ignore
5757
from google.cloud import exceptions # pytype: disable=import-error
5858
from google.cloud.client import ClientWithProject # type: ignore # pytype: disable=import-error
59-
6059
from google.cloud.bigquery_storage_v1.services.big_query_read.client import (
6160
DEFAULT_CLIENT_INFO as DEFAULT_BQSTORAGE_CLIENT_INFO,
6261
)
@@ -67,6 +66,8 @@
6766
from google.cloud.bigquery._helpers import _record_field_to_json
6867
from google.cloud.bigquery._helpers import _str_or_none
6968
from google.cloud.bigquery._helpers import _verify_job_config_type
69+
from google.cloud.bigquery._helpers import _get_bigquery_host
70+
from google.cloud.bigquery._helpers import _DEFAULT_HOST
7071
from google.cloud.bigquery._http import Connection
7172
from google.cloud.bigquery import _pandas_helpers
7273
from google.cloud.bigquery.dataset import Dataset
@@ -230,6 +231,8 @@ def __init__(
230231
)
231232

232233
kw_args = {"client_info": client_info}
234+
bq_host = _get_bigquery_host()
235+
kw_args["api_endpoint"] = bq_host if bq_host != _DEFAULT_HOST else None
233236
if client_options:
234237
if type(client_options) == dict:
235238
client_options = google.api_core.client_options.from_dict(

tests/unit/test__helpers.py

+26
Original file line numberDiff line numberDiff line change
@@ -1288,3 +1288,29 @@ def test_decimal_as_float_api_repr():
12881288
"parameterValue": {"value": 42.0},
12891289
"name": "x",
12901290
}
1291+
1292+
1293+
class Test__get_bigquery_host(unittest.TestCase):
1294+
@staticmethod
1295+
def _call_fut():
1296+
from google.cloud.bigquery._helpers import _get_bigquery_host
1297+
1298+
return _get_bigquery_host()
1299+
1300+
def test_wo_env_var(self):
1301+
from google.cloud.bigquery._helpers import _DEFAULT_HOST
1302+
1303+
with mock.patch("os.environ", {}):
1304+
host = self._call_fut()
1305+
1306+
self.assertEqual(host, _DEFAULT_HOST)
1307+
1308+
def test_w_env_var(self):
1309+
from google.cloud.bigquery._helpers import BIGQUERY_EMULATOR_HOST
1310+
1311+
HOST = "https://api.example.com"
1312+
1313+
with mock.patch("os.environ", {BIGQUERY_EMULATOR_HOST: HOST}):
1314+
host = self._call_fut()
1315+
1316+
self.assertEqual(host, HOST)

0 commit comments

Comments
 (0)