From 0dc7f154916b9c2ec76a51b370306931b16dedc0 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Wed, 8 May 2019 11:50:05 -0400 Subject: [PATCH] Add client_info support to client / connection. (#7873) --- .../google/cloud/translate_v2/_http.py | 14 +++++++++----- .../google/cloud/translate_v2/client.py | 17 +++++++++++++++-- .../tests/unit/test__http.py | 6 ++---- .../tests/unit/test_client.py | 13 ++++++++++--- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/packages/google-cloud-translate/google/cloud/translate_v2/_http.py b/packages/google-cloud-translate/google/cloud/translate_v2/_http.py index a89ea0114a3f..b73f5f595450 100644 --- a/packages/google-cloud-translate/google/cloud/translate_v2/_http.py +++ b/packages/google-cloud-translate/google/cloud/translate_v2/_http.py @@ -19,16 +19,22 @@ from google.cloud.translate_v2 import __version__ -_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) - - class Connection(_http.JSONConnection): """A connection to Google Cloud Translation API via the JSON REST API. :type client: :class:`~google.cloud.translate.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://translation.googleapis.com" """The base of the API call URL.""" @@ -37,5 +43,3 @@ class Connection(_http.JSONConnection): API_URL_TEMPLATE = "{api_base_url}/language/translate/{api_version}{path}" """A template for the URL of a particular API call.""" - - _EXTRA_HEADERS = {_http.CLIENT_INFO_HEADER: _CLIENT_INFO} diff --git a/packages/google-cloud-translate/google/cloud/translate_v2/client.py b/packages/google-cloud-translate/google/cloud/translate_v2/client.py index f5945f793cd0..9e7e4d351478 100644 --- a/packages/google-cloud-translate/google/cloud/translate_v2/client.py +++ b/packages/google-cloud-translate/google/cloud/translate_v2/client.py @@ -54,15 +54,28 @@ class Client(BaseClient): ``credentials`` for the current object. 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. """ SCOPE = ("https://www.googleapis.com/auth/cloud-platform",) """The scopes required for authenticating.""" - def __init__(self, target_language=ENGLISH_ISO_639, credentials=None, _http=None): + def __init__( + self, + target_language=ENGLISH_ISO_639, + credentials=None, + _http=None, + client_info=None, + ): self.target_language = target_language super(Client, self).__init__(credentials=credentials, _http=_http) - self._connection = Connection(self) + self._connection = Connection(self, client_info=client_info) def get_languages(self, target_language=None): """Get list of supported languages for translation. diff --git a/packages/google-cloud-translate/tests/unit/test__http.py b/packages/google-cloud-translate/tests/unit/test__http.py index 714ace292c0b..d179ce330653 100644 --- a/packages/google-cloud-translate/tests/unit/test__http.py +++ b/packages/google-cloud-translate/tests/unit/test__http.py @@ -50,9 +50,7 @@ def test_build_api_url_w_extra_query_params(self): def test_extra_headers(self): import requests - from google.cloud import _http as base_http - from google.cloud.translate_v2 import _http as MUT http = mock.create_autospec(requests.Session, instance=True) response = requests.Response() @@ -69,8 +67,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/packages/google-cloud-translate/tests/unit/test_client.py b/packages/google-cloud-translate/tests/unit/test_client.py index 1fe19451e39e..e014657bc882 100644 --- a/packages/google-cloud-translate/tests/unit/test_client.py +++ b/packages/google-cloud-translate/tests/unit/test_client.py @@ -25,7 +25,8 @@ def _get_target_class(): def _make_one(self, *args, **kw): return self._get_target_class()(*args, **kw) - def test_constructor(self): + def test_constructor_defaults(self): + from google.cloud._http import ClientInfo from google.cloud.translate_v2._http import Connection from google.cloud.translate_v2.client import ENGLISH_ISO_639 @@ -35,17 +36,23 @@ def test_constructor(self): self.assertIsNone(client._connection.credentials) self.assertIs(client._connection.http, http) self.assertEqual(client.target_language, ENGLISH_ISO_639) + self.assertIsInstance(client._connection._client_info, ClientInfo) - def test_constructor_non_default(self): + def test_constructor_explicit(self): + from google.cloud._http import ClientInfo from google.cloud.translate_v2._http import Connection http = object() target = "es" - client = self._make_one(target_language=target, _http=http) + client_info = ClientInfo() + client = self._make_one( + target_language=target, _http=http, client_info=client_info + ) self.assertIsInstance(client._connection, Connection) self.assertIsNone(client._connection.credentials) self.assertIs(client._connection.http, http) self.assertEqual(client.target_language, target) + self.assertIs(client._connection._client_info, client_info) def test_get_languages(self): from google.cloud.translate_v2.client import ENGLISH_ISO_639