Skip to content

Commit

Permalink
Add client_options to constructor. (googleapis#8999)
Browse files Browse the repository at this point in the history
  • Loading branch information
emar-kar committed Sep 18, 2019
1 parent 6166193 commit 3f2d3e3
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 8 deletions.
9 changes: 4 additions & 5 deletions bigquery/google/cloud/bigquery/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
18 changes: 17 additions & 1 deletion bigquery/google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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
Expand Down Expand Up @@ -142,6 +143,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:
Expand All @@ -163,11 +167,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

Expand Down
2 changes: 1 addition & 1 deletion bigquery/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand Down
8 changes: 7 additions & 1 deletion bigquery/tests/unit/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
50 changes: 50 additions & 0 deletions bigquery/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,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
Expand Down

0 comments on commit 3f2d3e3

Please sign in to comment.