diff --git a/dns/google/cloud/dns/_http.py b/dns/google/cloud/dns/_http.py index 28c4f31cca96..d04cd1f9090a 100644 --- a/dns/google/cloud/dns/_http.py +++ b/dns/google/cloud/dns/_http.py @@ -19,16 +19,22 @@ from google.cloud.dns import __version__ -_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) - - class Connection(_http.JSONConnection): """A connection to Google Cloud DNS via the JSON REST API. :type client: :class:`~google.cloud.dns.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://www.googleapis.com" """The base of the API call URL.""" @@ -37,5 +43,3 @@ class Connection(_http.JSONConnection): API_URL_TEMPLATE = "{api_base_url}/dns/{api_version}{path}" """A template for the URL of a particular API call.""" - - _EXTRA_HEADERS = {_http.CLIENT_INFO_HEADER: _CLIENT_INFO} diff --git a/dns/google/cloud/dns/client.py b/dns/google/cloud/dns/client.py index b2f6947ee79c..4bfa112d5226 100644 --- a/dns/google/cloud/dns/client.py +++ b/dns/google/cloud/dns/client.py @@ -43,16 +43,23 @@ class Client(ClientWithProject): ``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/ndev.clouddns.readwrite",) """The scopes required for authenticating as a Cloud DNS consumer.""" - def __init__(self, project=None, credentials=None, _http=None): + def __init__(self, project=None, credentials=None, _http=None, client_info=None): super(Client, self).__init__( project=project, credentials=credentials, _http=_http ) - self._connection = Connection(self) + self._connection = Connection(self, client_info=client_info) def quotas(self): """Return DNS quotas for the project associated with this client. diff --git a/dns/tests/unit/test__http.py b/dns/tests/unit/test__http.py index 002abb67c364..9c5198f0a7d2 100644 --- a/dns/tests/unit/test__http.py +++ b/dns/tests/unit/test__http.py @@ -46,9 +46,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.dns import _http as MUT http = mock.create_autospec(requests.Session, instance=True) response = requests.Response() @@ -65,8 +63,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/dns/tests/unit/test_client.py b/dns/tests/unit/test_client.py index af7b01355990..5ca6eec98a1f 100644 --- a/dns/tests/unit/test_client.py +++ b/dns/tests/unit/test_client.py @@ -38,6 +38,7 @@ def _make_one(self, *args, **kw): return self._get_target_class()(*args, **kw) def test_ctor(self): + from google.api_core.client_info import ClientInfo from google.cloud.dns._http import Connection creds = _make_credentials() @@ -46,6 +47,23 @@ def test_ctor(self): self.assertIsInstance(client._connection, Connection) self.assertIs(client._connection.credentials, creds) self.assertIs(client._connection.http, http) + self.assertIsInstance(client._connection._client_info, ClientInfo) + + def test_ctor_w_client_info(self): + from google.api_core.client_info import ClientInfo + from google.cloud.dns._http import Connection + + client_info = ClientInfo() + + creds = _make_credentials() + http = object() + client = self._make_one( + project=self.PROJECT, credentials=creds, _http=http, client_info=client_info + ) + self.assertIsInstance(client._connection, Connection) + self.assertIs(client._connection.credentials, creds) + self.assertIs(client._connection.http, http) + self.assertIs(client._connection._client_info, client_info) def test_quotas_defaults(self): PATH = "projects/%s" % (self.PROJECT,)