Skip to content

Commit 6df3112

Browse files
authored
Add 'client_info' support to client / connection. (#7870)
1 parent 237e068 commit 6df3112

4 files changed

Lines changed: 38 additions & 12 deletions

File tree

resource_manager/google/cloud/resource_manager/_http.py

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

2222

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

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

@@ -38,5 +44,3 @@ class Connection(_http.JSONConnection):
3844

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

resource_manager/google/cloud/resource_manager/client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,21 @@ class Client(BaseClient):
4949
``credentials`` for the current object.
5050
This parameter should be considered private, and could
5151
change in the future.
52+
53+
:type client_info: :class:`~google.api_core.client_info.ClientInfo`
54+
:param client_info:
55+
The client info used to send a user-agent string along with API
56+
requests. If ``None``, then default info will be used. Generally,
57+
you only need to set this if you're developing your own library
58+
or partner tool.
5259
"""
5360

5461
SCOPE = ("https://www.googleapis.com/auth/cloud-platform",)
5562
"""The scopes required for authenticating as a Resouce Manager consumer."""
5663

57-
def __init__(self, credentials=None, _http=None):
64+
def __init__(self, credentials=None, _http=None, client_info=None):
5865
super(Client, self).__init__(credentials=credentials, _http=_http)
59-
self._connection = Connection(self)
66+
self._connection = Connection(self, client_info=client_info)
6067

6168
def new_project(self, project_id, name=None, labels=None):
6269
"""Create a project bound to the current client.

resource_manager/tests/unit/test__http.py

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

4747
def test_extra_headers(self):
4848
import requests
49-
5049
from google.cloud import _http as base_http
51-
from google.cloud.resource_manager import _http as MUT
5250

5351
http = mock.create_autospec(requests.Session, instance=True)
5452
response = requests.Response()
@@ -65,8 +63,8 @@ def test_extra_headers(self):
6563

6664
expected_headers = {
6765
"Accept-Encoding": "gzip",
68-
base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO,
69-
"User-Agent": conn.USER_AGENT,
66+
base_http.CLIENT_INFO_HEADER: conn.user_agent,
67+
"User-Agent": conn.user_agent,
7068
}
7169
expected_uri = conn.build_api_url("/rainbow")
7270
http.request.assert_called_once_with(

resource_manager/tests/unit/test_client.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def _get_target_class():
3333
def _make_one(self, *args, **kw):
3434
return self._get_target_class()(*args, **kw)
3535

36-
def test_constructor(self):
36+
def test_ctor_wo_client_info(self):
37+
from google.cloud._http import ClientInfo
3738
from google.cloud.resource_manager._http import Connection
3839

3940
http = object()
@@ -42,6 +43,22 @@ def test_constructor(self):
4243
self.assertIsInstance(client._connection, Connection)
4344
self.assertIs(client._credentials, credentials)
4445
self.assertIs(client._http_internal, http)
46+
self.assertIsInstance(client._connection._client_info, ClientInfo)
47+
48+
def test_ctor_w_client_info(self):
49+
from google.cloud._http import ClientInfo
50+
from google.cloud.resource_manager._http import Connection
51+
52+
http = object()
53+
client_info = ClientInfo()
54+
credentials = _make_credentials()
55+
client = self._make_one(
56+
credentials=credentials, _http=http, client_info=client_info
57+
)
58+
self.assertIsInstance(client._connection, Connection)
59+
self.assertIs(client._credentials, credentials)
60+
self.assertIs(client._http_internal, http)
61+
self.assertIs(client._connection._client_info, client_info)
4562

4663
def test_new_project_factory(self):
4764
from google.cloud.resource_manager.project import Project

0 commit comments

Comments
 (0)