From dfd76c90355a0841c14ecc73eecbd9a423e3809d Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Tue, 7 May 2019 11:51:24 -0400 Subject: [PATCH 1/3] Add client_info support to client / connection. Toward #7825. --- logging/google/cloud/logging/_http.py | 14 +++++++++----- logging/google/cloud/logging/client.py | 18 ++++++++++++++++-- logging/tests/unit/test__http.py | 4 ++-- logging/tests/unit/test_client.py | 20 +++++++++++++++++++- 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/logging/google/cloud/logging/_http.py b/logging/google/cloud/logging/_http.py index d13baa175c1e..aa6d511f0106 100644 --- a/logging/google/cloud/logging/_http.py +++ b/logging/google/cloud/logging/_http.py @@ -25,16 +25,22 @@ from google.cloud.logging.metric import Metric -_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) - - class Connection(_http.JSONConnection): """A connection to Google Stackdriver Logging via the JSON REST API. :type client: :class:`~google.cloud.logging.client.Client` :param client: The client that owns the current connection. + + :type client_info: :class:`~google.api_core.client_info.ClientInfo` + :param client_info: (Optional) instance used to generate user agent. """ + def __init__(self, client, client_info=None): + super(Connection, self).__init__(client, client_info) + + self._client_info.gapic_version = __version__ + self._client_info.client_library_version = __version__ + API_BASE_URL = "https://logging.googleapis.com" """The base of the API call URL.""" @@ -44,8 +50,6 @@ class Connection(_http.JSONConnection): API_URL_TEMPLATE = "{api_base_url}/{api_version}{path}" """A template for the URL of a particular API call.""" - _EXTRA_HEADERS = {_http.CLIENT_INFO_HEADER: _CLIENT_INFO} - class _LoggingAPI(object): """Helper mapping logging-related APIs. diff --git a/logging/google/cloud/logging/client.py b/logging/google/cloud/logging/client.py index b5f0b02daaf9..642a9c6cf60b 100644 --- a/logging/google/cloud/logging/client.py +++ b/logging/google/cloud/logging/client.py @@ -86,6 +86,13 @@ class Client(ClientWithProject): environment variable This parameter should be considered private, and could change in the future. + + :type client_info: :class:`~google.api_core.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, + you only need to set this if you're developing your own library + or partner tool. """ _logging_api = None @@ -100,11 +107,18 @@ class Client(ClientWithProject): ) """The scopes required for authenticating as a Logging consumer.""" - def __init__(self, project=None, credentials=None, _http=None, _use_grpc=None): + def __init__( + self, + project=None, + credentials=None, + _http=None, + _use_grpc=None, + client_info=None, + ): super(Client, self).__init__( project=project, credentials=credentials, _http=_http ) - self._connection = Connection(self) + self._connection = Connection(self, client_info=client_info) if _use_grpc is None: self._use_grpc = _USE_GRPC else: diff --git a/logging/tests/unit/test__http.py b/logging/tests/unit/test__http.py index e4c4ecb279ef..e92eb1636a93 100644 --- a/logging/tests/unit/test__http.py +++ b/logging/tests/unit/test__http.py @@ -63,8 +63,8 @@ def test_extra_headers(self): expected_headers = { "Accept-Encoding": "gzip", - base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO, - "User-Agent": conn.USER_AGENT, + base_http.CLIENT_INFO_HEADER: conn.user_agent, + "User-Agent": conn.user_agent, } expected_uri = conn.build_api_url("/rainbow") http.request.assert_called_once_with( diff --git a/logging/tests/unit/test_client.py b/logging/tests/unit/test_client.py index 5ea17eb78f0b..740feb8d5a17 100644 --- a/logging/tests/unit/test_client.py +++ b/logging/tests/unit/test_client.py @@ -43,10 +43,28 @@ def _get_target_class(): def _make_one(self, *args, **kw): return self._get_target_class()(*args, **kw) - def test_ctor(self): + def test_ctor_defaults(self): + from google.cloud._http import ClientInfo + from google.cloud.logging._http import Connection + creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) self.assertEqual(client.project, self.PROJECT) + self.assertIsInstance(client._connection, Connection) + self.assertIsInstance(client._connection._client_info, ClientInfo) + + def test_ctor_explicit(self): + from google.cloud._http import ClientInfo + from google.cloud.logging._http import Connection + + creds = _make_credentials() + client_info = ClientInfo() + client = self._make_one( + project=self.PROJECT, credentials=creds, client_info=client_info + ) + self.assertEqual(client.project, self.PROJECT) + self.assertIsInstance(client._connection, Connection) + self.assertIs(client._connection._client_info, client_info) def test_logging_api_wo_gapic(self): from google.cloud.logging._http import _LoggingAPI From 386602d3b8d53efea04b181ccdf338a8fa570222 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Tue, 7 May 2019 12:38:06 -0400 Subject: [PATCH 2/3] Docstring typo. --- logging/google/cloud/logging/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logging/google/cloud/logging/client.py b/logging/google/cloud/logging/client.py index 642a9c6cf60b..af65a83f1c9b 100644 --- a/logging/google/cloud/logging/client.py +++ b/logging/google/cloud/logging/client.py @@ -87,7 +87,7 @@ 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` :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, From ae60382ba45ab7c7475a73e73d486d1a733f1c21 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Tue, 7 May 2019 12:39:05 -0400 Subject: [PATCH 3/3] Lint. --- logging/tests/unit/test__http.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/logging/tests/unit/test__http.py b/logging/tests/unit/test__http.py index e92eb1636a93..49abeafdcb77 100644 --- a/logging/tests/unit/test__http.py +++ b/logging/tests/unit/test__http.py @@ -44,9 +44,7 @@ def test_default_url(self): def test_extra_headers(self): import requests - from google.cloud import _http as base_http - from google.cloud.logging import _http as MUT http = mock.create_autospec(requests.Session, instance=True) response = requests.Response()