Skip to content

Commit 0dc7f15

Browse files
authored
Add client_info support to client / connection. (#7873)
1 parent 09dbed8 commit 0dc7f15

4 files changed

Lines changed: 36 additions & 14 deletions

File tree

packages/google-cloud-translate/google/cloud/translate_v2/_http.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,22 @@
1919
from google.cloud.translate_v2 import __version__
2020

2121

22-
_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)
23-
24-
2522
class Connection(_http.JSONConnection):
2623
"""A connection to Google Cloud Translation API via the JSON REST API.
2724
2825
:type client: :class:`~google.cloud.translate.client.Client`
2926
:param client: The client that owns the current connection.
27+
28+
:type client_info: :class:`~google.api_core.client_info.ClientInfo`
29+
:param client_info: (Optional) instance used to generate user agent.
3030
"""
3131

32+
def __init__(self, client, client_info=None):
33+
super(Connection, self).__init__(client, client_info)
34+
35+
self._client_info.gapic_version = __version__
36+
self._client_info.client_library_version = __version__
37+
3238
API_BASE_URL = "https://translation.googleapis.com"
3339
"""The base of the API call URL."""
3440

@@ -37,5 +43,3 @@ class Connection(_http.JSONConnection):
3743

3844
API_URL_TEMPLATE = "{api_base_url}/language/translate/{api_version}{path}"
3945
"""A template for the URL of a particular API call."""
40-
41-
_EXTRA_HEADERS = {_http.CLIENT_INFO_HEADER: _CLIENT_INFO}

packages/google-cloud-translate/google/cloud/translate_v2/client.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,28 @@ class Client(BaseClient):
5454
``credentials`` for the current object.
5555
This parameter should be considered private, and could
5656
change in the future.
57+
58+
:type client_info: :class:`~google.api_core.client_info.ClientInfo`
59+
:param client_info:
60+
The client info used to send a user-agent string along with API
61+
requests. If ``None``, then default info will be used. Generally,
62+
you only need to set this if you're developing your own library
63+
or partner tool.
5764
"""
5865

5966
SCOPE = ("https://www.googleapis.com/auth/cloud-platform",)
6067
"""The scopes required for authenticating."""
6168

62-
def __init__(self, target_language=ENGLISH_ISO_639, credentials=None, _http=None):
69+
def __init__(
70+
self,
71+
target_language=ENGLISH_ISO_639,
72+
credentials=None,
73+
_http=None,
74+
client_info=None,
75+
):
6376
self.target_language = target_language
6477
super(Client, self).__init__(credentials=credentials, _http=_http)
65-
self._connection = Connection(self)
78+
self._connection = Connection(self, client_info=client_info)
6679

6780
def get_languages(self, target_language=None):
6881
"""Get list of supported languages for translation.

packages/google-cloud-translate/tests/unit/test__http.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ def test_build_api_url_w_extra_query_params(self):
5050

5151
def test_extra_headers(self):
5252
import requests
53-
5453
from google.cloud import _http as base_http
55-
from google.cloud.translate_v2 import _http as MUT
5654

5755
http = mock.create_autospec(requests.Session, instance=True)
5856
response = requests.Response()
@@ -69,8 +67,8 @@ def test_extra_headers(self):
6967

7068
expected_headers = {
7169
"Accept-Encoding": "gzip",
72-
base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO,
73-
"User-Agent": conn.USER_AGENT,
70+
base_http.CLIENT_INFO_HEADER: conn.user_agent,
71+
"User-Agent": conn.user_agent,
7472
}
7573
expected_uri = conn.build_api_url("/rainbow")
7674
http.request.assert_called_once_with(

packages/google-cloud-translate/tests/unit/test_client.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def _get_target_class():
2525
def _make_one(self, *args, **kw):
2626
return self._get_target_class()(*args, **kw)
2727

28-
def test_constructor(self):
28+
def test_constructor_defaults(self):
29+
from google.cloud._http import ClientInfo
2930
from google.cloud.translate_v2._http import Connection
3031
from google.cloud.translate_v2.client import ENGLISH_ISO_639
3132

@@ -35,17 +36,23 @@ def test_constructor(self):
3536
self.assertIsNone(client._connection.credentials)
3637
self.assertIs(client._connection.http, http)
3738
self.assertEqual(client.target_language, ENGLISH_ISO_639)
39+
self.assertIsInstance(client._connection._client_info, ClientInfo)
3840

39-
def test_constructor_non_default(self):
41+
def test_constructor_explicit(self):
42+
from google.cloud._http import ClientInfo
4043
from google.cloud.translate_v2._http import Connection
4144

4245
http = object()
4346
target = "es"
44-
client = self._make_one(target_language=target, _http=http)
47+
client_info = ClientInfo()
48+
client = self._make_one(
49+
target_language=target, _http=http, client_info=client_info
50+
)
4551
self.assertIsInstance(client._connection, Connection)
4652
self.assertIsNone(client._connection.credentials)
4753
self.assertIs(client._connection.http, http)
4854
self.assertEqual(client.target_language, target)
55+
self.assertIs(client._connection._client_info, client_info)
4956

5057
def test_get_languages(self):
5158
from google.cloud.translate_v2.client import ENGLISH_ISO_639

0 commit comments

Comments
 (0)