Skip to content

Commit d4ab911

Browse files
authored
Add 'client_info' support to client / connection. (#7869)
1 parent f46e446 commit d4ab911

4 files changed

Lines changed: 38 additions & 11 deletions

File tree

packages/google-cloud-dns/google/cloud/dns/_http.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,22 @@
1919
from google.cloud.dns 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 DNS via the JSON REST API.
2724
2825
:type client: :class:`~google.cloud.dns.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://www.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}/dns/{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-dns/google/cloud/dns/client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,23 @@ class Client(ClientWithProject):
4343
``credentials`` for the current object.
4444
This parameter should be considered private, and could
4545
change in the future.
46+
47+
:type client_info: :class:`~google.api_core.client_info.ClientInfo`
48+
:param client_info:
49+
The client info used to send a user-agent string along with API
50+
requests. If ``None``, then default info will be used. Generally,
51+
you only need to set this if you're developing your own library
52+
or partner tool.
4653
"""
4754

4855
SCOPE = ("https://www.googleapis.com/auth/ndev.clouddns.readwrite",)
4956
"""The scopes required for authenticating as a Cloud DNS consumer."""
5057

51-
def __init__(self, project=None, credentials=None, _http=None):
58+
def __init__(self, project=None, credentials=None, _http=None, client_info=None):
5259
super(Client, self).__init__(
5360
project=project, credentials=credentials, _http=_http
5461
)
55-
self._connection = Connection(self)
62+
self._connection = Connection(self, client_info=client_info)
5663

5764
def quotas(self):
5865
"""Return DNS quotas for the project associated with this client.

packages/google-cloud-dns/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.dns 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(

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def _make_one(self, *args, **kw):
3838
return self._get_target_class()(*args, **kw)
3939

4040
def test_ctor(self):
41+
from google.api_core.client_info import ClientInfo
4142
from google.cloud.dns._http import Connection
4243

4344
creds = _make_credentials()
@@ -46,6 +47,23 @@ def test_ctor(self):
4647
self.assertIsInstance(client._connection, Connection)
4748
self.assertIs(client._connection.credentials, creds)
4849
self.assertIs(client._connection.http, http)
50+
self.assertIsInstance(client._connection._client_info, ClientInfo)
51+
52+
def test_ctor_w_client_info(self):
53+
from google.api_core.client_info import ClientInfo
54+
from google.cloud.dns._http import Connection
55+
56+
client_info = ClientInfo()
57+
58+
creds = _make_credentials()
59+
http = object()
60+
client = self._make_one(
61+
project=self.PROJECT, credentials=creds, _http=http, client_info=client_info
62+
)
63+
self.assertIsInstance(client._connection, Connection)
64+
self.assertIs(client._connection.credentials, creds)
65+
self.assertIs(client._connection.http, http)
66+
self.assertIs(client._connection._client_info, client_info)
4967

5068
def test_quotas_defaults(self):
5169
PATH = "projects/%s" % (self.PROJECT,)

0 commit comments

Comments
 (0)