Skip to content

Commit 07b9ccb

Browse files
authored
Add client_info support to client / connection. (#7874)
1 parent 1c19fbf commit 07b9ccb

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

google/cloud/logging/_http.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,22 @@
2525
from google.cloud.logging.metric import Metric
2626

2727

28-
_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)
29-
30-
3128
class Connection(_http.JSONConnection):
3229
"""A connection to Google Stackdriver Logging via the JSON REST API.
3330
3431
:type client: :class:`~google.cloud.logging.client.Client`
3532
:param client: The client that owns the current connection.
33+
34+
:type client_info: :class:`~google.api_core.client_info.ClientInfo`
35+
:param client_info: (Optional) instance used to generate user agent.
3636
"""
3737

38+
def __init__(self, client, client_info=None):
39+
super(Connection, self).__init__(client, client_info)
40+
41+
self._client_info.gapic_version = __version__
42+
self._client_info.client_library_version = __version__
43+
3844
API_BASE_URL = "https://logging.googleapis.com"
3945
"""The base of the API call URL."""
4046

@@ -44,8 +50,6 @@ class Connection(_http.JSONConnection):
4450
API_URL_TEMPLATE = "{api_base_url}/{api_version}{path}"
4551
"""A template for the URL of a particular API call."""
4652

47-
_EXTRA_HEADERS = {_http.CLIENT_INFO_HEADER: _CLIENT_INFO}
48-
4953

5054
class _LoggingAPI(object):
5155
"""Helper mapping logging-related APIs.

google/cloud/logging/client.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ class Client(ClientWithProject):
8686
environment variable
8787
This parameter should be considered private, and could
8888
change in the future.
89+
90+
:type client_info: :class:`~google.api_core.client_info.ClientInfo`
91+
:param client_info:
92+
The client info used to send a user-agent string along with API
93+
requests. If ``None``, then default info will be used. Generally,
94+
you only need to set this if you're developing your own library
95+
or partner tool.
8996
"""
9097

9198
_logging_api = None
@@ -100,11 +107,18 @@ class Client(ClientWithProject):
100107
)
101108
"""The scopes required for authenticating as a Logging consumer."""
102109

103-
def __init__(self, project=None, credentials=None, _http=None, _use_grpc=None):
110+
def __init__(
111+
self,
112+
project=None,
113+
credentials=None,
114+
_http=None,
115+
_use_grpc=None,
116+
client_info=None,
117+
):
104118
super(Client, self).__init__(
105119
project=project, credentials=credentials, _http=_http
106120
)
107-
self._connection = Connection(self)
121+
self._connection = Connection(self, client_info=client_info)
108122
if _use_grpc is None:
109123
self._use_grpc = _USE_GRPC
110124
else:

tests/unit/test__http.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ def test_default_url(self):
4444

4545
def test_extra_headers(self):
4646
import requests
47-
4847
from google.cloud import _http as base_http
49-
from google.cloud.logging import _http as MUT
5048

5149
http = mock.create_autospec(requests.Session, instance=True)
5250
response = requests.Response()
@@ -63,8 +61,8 @@ def test_extra_headers(self):
6361

6462
expected_headers = {
6563
"Accept-Encoding": "gzip",
66-
base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO,
67-
"User-Agent": conn.USER_AGENT,
64+
base_http.CLIENT_INFO_HEADER: conn.user_agent,
65+
"User-Agent": conn.user_agent,
6866
}
6967
expected_uri = conn.build_api_url("/rainbow")
7068
http.request.assert_called_once_with(

tests/unit/test_client.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,28 @@ def _get_target_class():
4343
def _make_one(self, *args, **kw):
4444
return self._get_target_class()(*args, **kw)
4545

46-
def test_ctor(self):
46+
def test_ctor_defaults(self):
47+
from google.cloud._http import ClientInfo
48+
from google.cloud.logging._http import Connection
49+
4750
creds = _make_credentials()
4851
client = self._make_one(project=self.PROJECT, credentials=creds)
4952
self.assertEqual(client.project, self.PROJECT)
53+
self.assertIsInstance(client._connection, Connection)
54+
self.assertIsInstance(client._connection._client_info, ClientInfo)
55+
56+
def test_ctor_explicit(self):
57+
from google.cloud._http import ClientInfo
58+
from google.cloud.logging._http import Connection
59+
60+
creds = _make_credentials()
61+
client_info = ClientInfo()
62+
client = self._make_one(
63+
project=self.PROJECT, credentials=creds, client_info=client_info
64+
)
65+
self.assertEqual(client.project, self.PROJECT)
66+
self.assertIsInstance(client._connection, Connection)
67+
self.assertIs(client._connection._client_info, client_info)
5068

5169
def test_logging_api_wo_gapic(self):
5270
from google.cloud.logging._http import _LoggingAPI

0 commit comments

Comments
 (0)