From 7695fd71bd10484577a8829407d000011c77b2e0 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Wed, 8 May 2019 14:09:50 -0400 Subject: [PATCH 1/2] Plumb 'Client._client_info' through to GAPIC API wrapper. Toward #7825. --- logging/google/cloud/logging/_gapic.py | 11 +++-------- logging/google/cloud/logging/client.py | 4 +++- logging/tests/unit/test__gapic.py | 12 ++++++------ 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/logging/google/cloud/logging/_gapic.py b/logging/google/cloud/logging/_gapic.py index 00e9f5f2ee15..32897c088142 100644 --- a/logging/google/cloud/logging/_gapic.py +++ b/logging/google/cloud/logging/_gapic.py @@ -17,7 +17,6 @@ import functools -from google.api_core.gapic_v1 import client_info from google.cloud.logging_v2.gapic.config_service_v2_client import ConfigServiceV2Client from google.cloud.logging_v2.gapic.logging_service_v2_client import ( LoggingServiceV2Client, @@ -31,15 +30,11 @@ from google.protobuf.json_format import MessageToDict from google.protobuf.json_format import ParseDict -from google.cloud.logging import __version__ from google.cloud.logging._helpers import entry_from_resource from google.cloud.logging.sink import Sink from google.cloud.logging.metric import Metric -_CLIENT_INFO = client_info.ClientInfo(client_library_version=__version__) - - class _LoggingAPI(object): """Helper mapping logging-related APIs. @@ -544,7 +539,7 @@ def make_logging_api(client): :returns: A metrics API instance with the proper credentials. """ generated = LoggingServiceV2Client( - credentials=client._credentials, client_info=_CLIENT_INFO + credentials=client._credentials, client_info=client._client_info ) return _LoggingAPI(generated, client) @@ -559,7 +554,7 @@ def make_metrics_api(client): :returns: A metrics API instance with the proper credentials. """ generated = MetricsServiceV2Client( - credentials=client._credentials, client_info=_CLIENT_INFO + credentials=client._credentials, client_info=client._client_info ) return _MetricsAPI(generated, client) @@ -574,6 +569,6 @@ def make_sinks_api(client): :returns: A metrics API instance with the proper credentials. """ generated = ConfigServiceV2Client( - credentials=client._credentials, client_info=_CLIENT_INFO + credentials=client._credentials, client_info=client._client_info ) return _SinksAPI(generated, client) diff --git a/logging/google/cloud/logging/client.py b/logging/google/cloud/logging/client.py index af65a83f1c9b..b9dd0ec606e8 100644 --- a/logging/google/cloud/logging/client.py +++ b/logging/google/cloud/logging/client.py @@ -87,7 +87,9 @@ class Client(ClientWithProject): This parameter should be considered private, and could change in the future. - :type client_info: :class:`~google.api_core.client_info.ClientInfo` + :type client_info: + :class:`google.api_core.client_info.ClientInfo` or + :class:`google.api_core.gapic_v1.client_info.ClientInfo` :param client_info: The client info used to send a user-agent string along with API requests. If ``None``, then default info will be used. Generally, diff --git a/logging/tests/unit/test__gapic.py b/logging/tests/unit/test__gapic.py index 03ff0a7a14f6..ad6ded2bd1f7 100644 --- a/logging/tests/unit/test__gapic.py +++ b/logging/tests/unit/test__gapic.py @@ -584,32 +584,32 @@ def test_registered_type(self): @mock.patch("google.cloud.logging._gapic.LoggingServiceV2Client", autospec=True) def test_make_logging_api(gapic_client): - client = mock.Mock(spec=["_credentials"]) + client = mock.Mock(spec=["_credentials", "_client_info"]) api = _gapic.make_logging_api(client) assert api._client == client assert api._gapic_api == gapic_client.return_value gapic_client.assert_called_once_with( - credentials=client._credentials, client_info=_gapic._CLIENT_INFO + credentials=client._credentials, client_info=client._client_info ) @mock.patch("google.cloud.logging._gapic.MetricsServiceV2Client", autospec=True) def test_make_metrics_api(gapic_client): - client = mock.Mock(spec=["_credentials"]) + client = mock.Mock(spec=["_credentials", "_client_info"]) api = _gapic.make_metrics_api(client) assert api._client == client assert api._gapic_api == gapic_client.return_value gapic_client.assert_called_once_with( - credentials=client._credentials, client_info=_gapic._CLIENT_INFO + credentials=client._credentials, client_info=client._client_info ) @mock.patch("google.cloud.logging._gapic.ConfigServiceV2Client", autospec=True) def test_make_sinks_api(gapic_client): - client = mock.Mock(spec=["_credentials"]) + client = mock.Mock(spec=["_credentials", "_client_info"]) api = _gapic.make_sinks_api(client) assert api._client == client assert api._gapic_api == gapic_client.return_value gapic_client.assert_called_once_with( - credentials=client._credentials, client_info=_gapic._CLIENT_INFO + credentials=client._credentials, client_info=client._client_info ) From 67dd4af5b99548dbe46b48877b057c8028442116 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Wed, 8 May 2019 15:47:08 -0400 Subject: [PATCH 2/2] Store 'client_info' on the client, for later use by GAPIC APIs. --- logging/google/cloud/logging/client.py | 1 + logging/tests/unit/test_client.py | 1 + 2 files changed, 2 insertions(+) diff --git a/logging/google/cloud/logging/client.py b/logging/google/cloud/logging/client.py index b9dd0ec606e8..18bd8a335dcd 100644 --- a/logging/google/cloud/logging/client.py +++ b/logging/google/cloud/logging/client.py @@ -121,6 +121,7 @@ def __init__( project=project, credentials=credentials, _http=_http ) self._connection = Connection(self, client_info=client_info) + self._client_info = client_info if _use_grpc is None: self._use_grpc = _USE_GRPC else: diff --git a/logging/tests/unit/test_client.py b/logging/tests/unit/test_client.py index 740feb8d5a17..e750df7de454 100644 --- a/logging/tests/unit/test_client.py +++ b/logging/tests/unit/test_client.py @@ -63,6 +63,7 @@ def test_ctor_explicit(self): project=self.PROJECT, credentials=creds, client_info=client_info ) self.assertEqual(client.project, self.PROJECT) + self.assertIs(client._client_info, client_info) self.assertIsInstance(client._connection, Connection) self.assertIs(client._connection._client_info, client_info)