Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spanner): add client_options to constructor #9151

Merged
merged 4 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions spanner/google/cloud/spanner_v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ class Client(ClientWithProject):
:param user_agent:
(Deprecated) The user agent to be used with API request.
Not used.
:type client_options: :class:`~google.api_core.client_options.ClientOptions`
or :class:`dict`
:param client_options: (Optional) Client options used to set user options
on the client. API Endpoint should be set through client_options.

:raises: :class:`ValueError <exceptions.ValueError>` if both ``read_only``
and ``admin`` are :data:`True`
Expand All @@ -124,7 +128,12 @@ class Client(ClientWithProject):
"""The scopes required for Google Cloud Spanner."""

def __init__(
self, project=None, credentials=None, client_info=_CLIENT_INFO, user_agent=None
self,
project=None,
credentials=None,
client_info=_CLIENT_INFO,
user_agent=None,
client_options=None,
):
# NOTE: This API has no use for the _http argument, but sending it
# will have no impact since the _http() @property only lazily
Expand All @@ -133,6 +142,7 @@ def __init__(
project=project, credentials=credentials, _http=None
)
self._client_info = client_info
self._client_options = client_options

if user_agent is not None:
warnings.warn(_USER_AGENT_DEPRECATED, DeprecationWarning, stacklevel=2)
Expand Down Expand Up @@ -172,7 +182,9 @@ def instance_admin_api(self):
"""Helper for session-related API calls."""
if self._instance_admin_api is None:
self._instance_admin_api = InstanceAdminClient(
credentials=self.credentials, client_info=self._client_info
credentials=self.credentials,
client_info=self._client_info,
client_options=self._client_options,
)
return self._instance_admin_api

Expand All @@ -181,7 +193,9 @@ def database_admin_api(self):
"""Helper for session-related API calls."""
if self._database_admin_api is None:
self._database_admin_api = DatabaseAdminClient(
credentials=self.credentials, client_info=self._client_info
credentials=self.credentials,
client_info=self._client_info,
client_options=self._client_options,
)
return self._database_admin_api

Expand Down
5 changes: 4 additions & 1 deletion spanner/google/cloud/spanner_v1/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,11 @@ def spanner_api(self):
if isinstance(credentials, google.auth.credentials.Scoped):
credentials = credentials.with_scopes((SPANNER_DATA_SCOPE,))
client_info = self._instance._client._client_info
client_options = self._instance._client._client_options
self._spanner_api = SpannerClient(
credentials=credentials, client_info=client_info
credentials=credentials,
client_info=client_info,
client_options=client_options,
)
return self._spanner_api

Expand Down
22 changes: 18 additions & 4 deletions spanner/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def _constructor_test_helper(
expected_creds=None,
client_info=None,
user_agent=None,
client_options=None,
):
from google.cloud.spanner_v1 import client as MUT

Expand All @@ -79,6 +80,7 @@ def _constructor_test_helper(
self.assertEqual(client.project, self.PROJECT)
self.assertIs(client._client_info, expected_client_info)
self.assertEqual(client.user_agent, user_agent)
self.assertEqual(client._client_options, client_options)

def test_constructor_default_scopes(self):
from google.cloud.spanner_v1 import client as MUT
Expand Down Expand Up @@ -130,8 +132,12 @@ def test_instance_admin_api(self):

credentials = _make_credentials()
client_info = mock.Mock()
client_options = mock.Mock()
client = self._make_one(
project=self.PROJECT, credentials=credentials, client_info=client_info
project=self.PROJECT,
credentials=credentials,
client_info=client_info,
client_options=client_options,
)
expected_scopes = (SPANNER_ADMIN_SCOPE,)

Expand All @@ -146,7 +152,9 @@ def test_instance_admin_api(self):
self.assertIs(again, api)

instance_admin_client.assert_called_once_with(
credentials=credentials.with_scopes.return_value, client_info=client_info
credentials=credentials.with_scopes.return_value,
client_info=client_info,
client_options=client_options,
)

credentials.with_scopes.assert_called_once_with(expected_scopes)
Expand All @@ -156,8 +164,12 @@ def test_database_admin_api(self):

credentials = _make_credentials()
client_info = mock.Mock()
client_options = mock.Mock()
client = self._make_one(
project=self.PROJECT, credentials=credentials, client_info=client_info
project=self.PROJECT,
credentials=credentials,
client_info=client_info,
client_options=client_options,
)
expected_scopes = (SPANNER_ADMIN_SCOPE,)

Expand All @@ -172,7 +184,9 @@ def test_database_admin_api(self):
self.assertIs(again, api)

database_admin_client.assert_called_once_with(
credentials=credentials.with_scopes.return_value, client_info=client_info
credentials=credentials.with_scopes.return_value,
client_info=client_info,
client_options=client_options,
)

credentials.with_scopes.assert_called_once_with(expected_scopes)
Expand Down