diff --git a/bigquery/google/cloud/bigquery/_http.py b/bigquery/google/cloud/bigquery/_http.py index 643b24920bee..5dd660ea0b8d 100644 --- a/bigquery/google/cloud/bigquery/_http.py +++ b/bigquery/google/cloud/bigquery/_http.py @@ -29,15 +29,14 @@ class Connection(_http.JSONConnection): :param client_info: (Optional) instance used to generate user agent. """ - def __init__(self, client, client_info=None): - super(Connection, self).__init__(client, client_info) + DEFAULT_API_ENDPOINT = "https://www.googleapis.com" + def __init__(self, client, client_info=None, api_endpoint=DEFAULT_API_ENDPOINT): + super(Connection, self).__init__(client, client_info) + self.API_BASE_URL = api_endpoint self._client_info.gapic_version = __version__ self._client_info.client_library_version = __version__ - API_BASE_URL = "https://www.googleapis.com" - """The base of the API call URL.""" - API_VERSION = "v2" """The version of the API, used in building the API call's URL.""" diff --git a/bigquery/google/cloud/bigquery/client.py b/bigquery/google/cloud/bigquery/client.py index b8ce2d5a33f3..32891c08bdfd 100644 --- a/bigquery/google/cloud/bigquery/client.py +++ b/bigquery/google/cloud/bigquery/client.py @@ -40,6 +40,7 @@ from google.resumable_media.requests import MultipartUpload from google.resumable_media.requests import ResumableUpload +import google.api_core.client_options import google.api_core.exceptions from google.api_core import page_iterator import google.cloud._helpers @@ -141,6 +142,9 @@ class Client(ClientWithProject): requests. If ``None``, then default info will be used. Generally, you only need to set this if you're developing your own library or partner tool. + client_options (Union[~google.api_core.client_options.ClientOptions, dict]): + (Optional) Client options used to set user options on the client. + API Endpoint should be set through client_options. Raises: google.auth.exceptions.DefaultCredentialsError: @@ -162,11 +166,23 @@ def __init__( location=None, default_query_job_config=None, client_info=None, + client_options=None, ): super(Client, self).__init__( project=project, credentials=credentials, _http=_http ) - self._connection = Connection(self, client_info=client_info) + + kw_args = {"client_info": client_info} + if client_options: + if type(client_options) == dict: + client_options = google.api_core.client_options.from_dict( + client_options + ) + if client_options.api_endpoint: + api_endpoint = client_options.api_endpoint + kw_args["api_endpoint"] = api_endpoint + + self._connection = Connection(self, **kw_args) self._location = location self._default_query_job_config = default_query_job_config diff --git a/bigquery/setup.py b/bigquery/setup.py index a05c18053379..c48231753281 100644 --- a/bigquery/setup.py +++ b/bigquery/setup.py @@ -30,7 +30,7 @@ release_status = "Development Status :: 5 - Production/Stable" dependencies = [ 'enum34; python_version < "3.4"', - "google-cloud-core >= 1.0.0, < 2.0dev", + "google-cloud-core >= 1.0.3, < 2.0dev", "google-resumable-media >= 0.3.1", "protobuf >= 3.6.0", ] diff --git a/bigquery/tests/unit/test__http.py b/bigquery/tests/unit/test__http.py index 939b5668e1e2..629e8031a52f 100644 --- a/bigquery/tests/unit/test__http.py +++ b/bigquery/tests/unit/test__http.py @@ -30,7 +30,13 @@ def _make_one(self, *args, **kw): def test_build_api_url_no_extra_query_params(self): conn = self._make_one(object()) - URI = "/".join([conn.API_BASE_URL, "bigquery", conn.API_VERSION, "foo"]) + URI = "/".join([conn.DEFAULT_API_ENDPOINT, "bigquery", conn.API_VERSION, "foo"]) + self.assertEqual(conn.build_api_url("/foo"), URI) + + def test_build_api_url_w_custom_endpoint(self): + custom_endpoint = "https://www.foo-googleapis.com" + conn = self._make_one(object(), api_endpoint=custom_endpoint) + URI = "/".join([custom_endpoint, "bigquery", conn.API_VERSION, "foo"]) self.assertEqual(conn.build_api_url("/foo"), URI) def test_build_api_url_w_extra_query_params(self): diff --git a/bigquery/tests/unit/test_client.py b/bigquery/tests/unit/test_client.py index 2be40a52e1fc..7673c3978fa9 100644 --- a/bigquery/tests/unit/test_client.py +++ b/bigquery/tests/unit/test_client.py @@ -111,6 +111,56 @@ def test_ctor_defaults(self): self.assertIs(client._connection.credentials, creds) self.assertIs(client._connection.http, http) self.assertIsNone(client.location) + self.assertEqual( + client._connection.API_BASE_URL, Connection.DEFAULT_API_ENDPOINT + ) + + def test_ctor_w_empty_client_options(self): + from google.api_core.client_options import ClientOptions + + creds = _make_credentials() + http = object() + client_options = ClientOptions() + client = self._make_one( + project=self.PROJECT, + credentials=creds, + _http=http, + client_options=client_options, + ) + self.assertEqual( + client._connection.API_BASE_URL, client._connection.DEFAULT_API_ENDPOINT + ) + + def test_ctor_w_client_options_dict(self): + + creds = _make_credentials() + http = object() + client_options = {"api_endpoint": "https://www.foo-googleapis.com"} + client = self._make_one( + project=self.PROJECT, + credentials=creds, + _http=http, + client_options=client_options, + ) + self.assertEqual( + client._connection.API_BASE_URL, "https://www.foo-googleapis.com" + ) + + def test_ctor_w_client_options_object(self): + from google.api_core.client_options import ClientOptions + + creds = _make_credentials() + http = object() + client_options = ClientOptions(api_endpoint="https://www.foo-googleapis.com") + client = self._make_one( + project=self.PROJECT, + credentials=creds, + _http=http, + client_options=client_options, + ) + self.assertEqual( + client._connection.API_BASE_URL, "https://www.foo-googleapis.com" + ) def test_ctor_w_location(self): from google.cloud.bigquery._http import Connection