From 686691ec6ffe65945860a824afef4728d7312b2d Mon Sep 17 00:00:00 2001
From: Tres Seaver <tseaver@palladion.com>
Date: Tue, 7 May 2019 11:35:38 -0400
Subject: [PATCH 1/3] Add 'client_info' support to resource_manager client /
 connection. > > Toward #7825.

---
 .../google/cloud/resource_manager/_http.py    | 14 +++++++++-----
 .../google/cloud/resource_manager/client.py   | 11 +++++++++--
 resource_manager/tests/unit/test__http.py     |  4 ++--
 resource_manager/tests/unit/test_client.py    | 19 ++++++++++++++++++-
 4 files changed, 38 insertions(+), 10 deletions(-)

diff --git a/resource_manager/google/cloud/resource_manager/_http.py b/resource_manager/google/cloud/resource_manager/_http.py
index d0118c810385..147b5649f9f7 100644
--- a/resource_manager/google/cloud/resource_manager/_http.py
+++ b/resource_manager/google/cloud/resource_manager/_http.py
@@ -20,16 +20,22 @@
 from google.cloud.resource_manager import __version__
 
 
-_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)
-
-
 class Connection(_http.JSONConnection):
     """A connection to Google Cloud Resource Manager via the JSON REST API.
 
     :type client: :class:`~google.cloud.resource_manager.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://cloudresourcemanager.googleapis.com"
     """The base of the API call URL."""
 
@@ -38,5 +44,3 @@ class Connection(_http.JSONConnection):
 
     API_URL_TEMPLATE = "{api_base_url}/{api_version}{path}"
     """A template for the URL of a particular API call."""
-
-    _EXTRA_HEADERS = {_http.CLIENT_INFO_HEADER: _CLIENT_INFO}
diff --git a/resource_manager/google/cloud/resource_manager/client.py b/resource_manager/google/cloud/resource_manager/client.py
index 7a2dca7f2a8a..4721de78befe 100644
--- a/resource_manager/google/cloud/resource_manager/client.py
+++ b/resource_manager/google/cloud/resource_manager/client.py
@@ -49,14 +49,21 @@ class Client(BaseClient):
                   ``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/cloud-platform",)
     """The scopes required for authenticating as a Resouce Manager consumer."""
 
-    def __init__(self, credentials=None, _http=None):
+    def __init__(self, credentials=None, _http=None, client_info=None):
         super(Client, self).__init__(credentials=credentials, _http=_http)
-        self._connection = Connection(self)
+        self._connection = Connection(self, client_info=client_info)
 
     def new_project(self, project_id, name=None, labels=None):
         """Create a project bound to the current client.
diff --git a/resource_manager/tests/unit/test__http.py b/resource_manager/tests/unit/test__http.py
index 77be1a34ee87..b1b3187c05a2 100644
--- a/resource_manager/tests/unit/test__http.py
+++ b/resource_manager/tests/unit/test__http.py
@@ -65,8 +65,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/resource_manager/tests/unit/test_client.py b/resource_manager/tests/unit/test_client.py
index d0223649ed59..57d0085d15e7 100644
--- a/resource_manager/tests/unit/test_client.py
+++ b/resource_manager/tests/unit/test_client.py
@@ -33,7 +33,8 @@ def _get_target_class():
     def _make_one(self, *args, **kw):
         return self._get_target_class()(*args, **kw)
 
-    def test_constructor(self):
+    def test_ctor_wo_client_info(self):
+        from google.cloud._http import ClientInfo
         from google.cloud.resource_manager._http import Connection
 
         http = object()
@@ -42,6 +43,22 @@ def test_constructor(self):
         self.assertIsInstance(client._connection, Connection)
         self.assertIs(client._credentials, credentials)
         self.assertIs(client._http_internal, http)
+        self.assertIsInstance(client._connection._client_info, ClientInfo)
+
+    def test_ctor_w_client_info(self):
+        from google.cloud._http import ClientInfo
+        from google.cloud.resource_manager._http import Connection
+
+        http = object()
+        client_info = ClientInfo()
+        credentials = _make_credentials()
+        client = self._make_one(
+            credentials=credentials, _http=http, client_info=client_info
+        )
+        self.assertIsInstance(client._connection, Connection)
+        self.assertIs(client._credentials, credentials)
+        self.assertIs(client._http_internal, http)
+        self.assertIs(client._connection._client_info, client_info)
 
     def test_new_project_factory(self):
         from google.cloud.resource_manager.project import Project

From 5d79fb933f6df37ac76c3c7461131d10c0f0a98a Mon Sep 17 00:00:00 2001
From: Tres Seaver <tseaver@palladion.com>
Date: Tue, 7 May 2019 12:27:59 -0400
Subject: [PATCH 2/3] Docstring typo.

---
 resource_manager/google/cloud/resource_manager/client.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/resource_manager/google/cloud/resource_manager/client.py b/resource_manager/google/cloud/resource_manager/client.py
index 4721de78befe..f9e180e29b18 100644
--- a/resource_manager/google/cloud/resource_manager/client.py
+++ b/resource_manager/google/cloud/resource_manager/client.py
@@ -50,7 +50,7 @@ class Client(BaseClient):
                   This parameter should be considered private, and could
                   change in the future.
 
-    :type client_info: :class:`~google.api_core.client_info.ClientInfo:
+    :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,

From 902e9112f072e3e5daab408091ac42719218b06b Mon Sep 17 00:00:00 2001
From: Tres Seaver <tseaver@palladion.com>
Date: Tue, 7 May 2019 12:29:00 -0400
Subject: [PATCH 3/3] Lint.

---
 resource_manager/tests/unit/test__http.py | 2 --
 1 file changed, 2 deletions(-)

diff --git a/resource_manager/tests/unit/test__http.py b/resource_manager/tests/unit/test__http.py
index b1b3187c05a2..f2d665874954 100644
--- a/resource_manager/tests/unit/test__http.py
+++ b/resource_manager/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.resource_manager import _http as MUT
 
         http = mock.create_autospec(requests.Session, instance=True)
         response = requests.Response()
