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

Firestore: add client_info support to V1 client. #7877

Merged
merged 1 commit into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 17 additions & 2 deletions firestore/google/cloud/firestore_v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
* a :class:`~.firestore_v1.client.Client` owns a
:class:`~.firestore_v1.document.DocumentReference`
"""
from google.api_core.gapic_v1 import client_info
from google.cloud.client import ClientWithProject

from google.cloud.firestore_v1 import _helpers
from google.cloud.firestore_v1 import __version__
from google.cloud.firestore_v1 import query
from google.cloud.firestore_v1 import types
from google.cloud.firestore_v1.batch import WriteBatch
Expand All @@ -47,6 +49,7 @@
)
_ACTIVE_TXN = "There is already an active transaction."
_INACTIVE_TXN = "There is no active transaction."
_CLIENT_INFO = client_info.ClientInfo(client_library_version=__version__)


class Client(ClientWithProject):
Expand All @@ -67,6 +70,11 @@ class Client(ClientWithProject):
database (Optional[str]): The database name that the client targets.
For now, :attr:`DEFAULT_DATABASE` (the default value) is the
only valid database.
client_info (Optional[google.api_core.client_info.ClientInfo]):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be documented as a google.api_core.gapic_v1.client_info.ClientInfo, instead?

Same comment as #7876 (comment)

The client info used to send a user-agent string along with API
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.
"""

SCOPE = (
Expand All @@ -79,13 +87,20 @@ class Client(ClientWithProject):
_database_string_internal = None
_rpc_metadata_internal = None

def __init__(self, project=None, credentials=None, database=DEFAULT_DATABASE):
def __init__(
self,
project=None,
credentials=None,
database=DEFAULT_DATABASE,
client_info=_CLIENT_INFO,
):
# NOTE: This API has no use for the _http argument, but sending it
# will have no impact since the _http() @property only lazily
# creates a working HTTP object.
super(Client, self).__init__(
project=project, credentials=credentials, _http=None
)
self._client_info = client_info
self._database = database

@property
Expand All @@ -98,7 +113,7 @@ def _firestore_api(self):
"""
if self._firestore_api_internal is None:
self._firestore_api_internal = firestore_client.FirestoreClient(
credentials=self._credentials
credentials=self._credentials, client_info=self._client_info
)

return self._firestore_api_internal
Expand Down
14 changes: 12 additions & 2 deletions firestore/tests/unit/v1/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,30 @@ def _make_default_one(self):
return self._make_one(project=self.PROJECT, credentials=credentials)

def test_constructor(self):
from google.cloud.firestore_v1.client import _CLIENT_INFO
from google.cloud.firestore_v1.client import DEFAULT_DATABASE

credentials = _make_credentials()
client = self._make_one(project=self.PROJECT, credentials=credentials)
self.assertEqual(client.project, self.PROJECT)
self.assertEqual(client._credentials, credentials)
self.assertEqual(client._database, DEFAULT_DATABASE)
self.assertIs(client._client_info, _CLIENT_INFO)

def test_constructor_explicit(self):
credentials = _make_credentials()
database = "now-db"
client_info = mock.Mock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Autospec?

client = self._make_one(
project=self.PROJECT, credentials=credentials, database=database
project=self.PROJECT,
credentials=credentials,
database=database,
client_info=client_info,
)
self.assertEqual(client.project, self.PROJECT)
self.assertEqual(client._credentials, credentials)
self.assertEqual(client._database, database)
self.assertIs(client._client_info, client_info)

@mock.patch(
"google.cloud.firestore_v1.gapic.firestore_client." "FirestoreClient",
Expand All @@ -63,11 +70,14 @@ def test_constructor_explicit(self):
)
def test__firestore_api_property(self, mock_client):
client = self._make_default_one()
client_info = client._client_info = mock.Mock()
self.assertIsNone(client._firestore_api_internal)
firestore_api = client._firestore_api
self.assertIs(firestore_api, mock_client.return_value)
self.assertIs(firestore_api, client._firestore_api_internal)
mock_client.assert_called_once_with(credentials=client._credentials)
mock_client.assert_called_once_with(
credentials=client._credentials, client_info=client_info
)

# Call again to show that it is cached, but call count is still 1.
self.assertIs(client._firestore_api, mock_client.return_value)
Expand Down