File tree Expand file tree Collapse file tree 3 files changed +42
-2
lines changed
Expand file tree Collapse file tree 3 files changed +42
-2
lines changed Original file line number Diff line number Diff line change 1919import decimal
2020import math
2121import re
22+ import os
2223from typing import Optional , Union
2324
2425from dateutil import relativedelta
2829from google .cloud ._helpers import _RFC3339_MICROS
2930from google .cloud ._helpers import _RFC3339_NO_FRACTION
3031from 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"
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
5566class BQStorageVersions :
5667 """Version comparisons for google-cloud-bigqueyr-storage package."""
Original file line number Diff line number Diff line change 5656import google .cloud ._helpers # type: ignore
5757from google .cloud import exceptions # pytype: disable=import-error
5858from google .cloud .client import ClientWithProject # type: ignore # pytype: disable=import-error
59-
6059from google .cloud .bigquery_storage_v1 .services .big_query_read .client import (
6160 DEFAULT_CLIENT_INFO as DEFAULT_BQSTORAGE_CLIENT_INFO ,
6261)
6766from google .cloud .bigquery ._helpers import _record_field_to_json
6867from google .cloud .bigquery ._helpers import _str_or_none
6968from 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
7071from google .cloud .bigquery ._http import Connection
7172from google .cloud .bigquery import _pandas_helpers
7273from 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 (
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments