Repository: googleapis/google-cloud-python

Issue:
Revert change to `User-Agent` header
From #2223.

See https://github.com/GoogleCloudPlatform/gcloud-common/issues/179 for rationale, such as it is and what there is of it.



Discussion:

Resolving patch:
diff --git a/google/cloud/_helpers.py b/google/cloud/_helpers.py
--- a/google/cloud/_helpers.py
+++ b/google/cloud/_helpers.py
@@ -585,14 +585,10 @@ class MetadataPlugin(object):
     :type credentials: :class:`oauth2client.client.OAuth2Credentials`
     :param credentials: The OAuth2 Credentials to use for creating
                         access tokens.
-
-    :type user_agent: str
-    :param user_agent: The user agent to be used with API requests.
     """
 
-    def __init__(self, credentials, user_agent):
+    def __init__(self, credentials):
         self._credentials = credentials
-        self._user_agent = user_agent
 
     def __call__(self, unused_context, callback):
         """Adds authorization header to request metadata.
@@ -607,7 +603,6 @@ def __call__(self, unused_context, callback):
         access_token = self._credentials.get_access_token().access_token
         headers = [
             ('authorization', 'Bearer ' + access_token),
-            ('user-agent', self._user_agent),
         ]
         callback(headers, None)
 
@@ -636,13 +631,17 @@ def make_secure_stub(credentials, user_agent, stub_class, host):
     # ssl_channel_credentials() loads root certificates from
     # `grpc/_adapter/credentials/roots.pem`.
     transport_creds = grpc.ssl_channel_credentials()
-    custom_metadata_plugin = MetadataPlugin(credentials, user_agent)
+    custom_metadata_plugin = MetadataPlugin(credentials)
     auth_creds = grpc.metadata_call_credentials(
         custom_metadata_plugin, name='google_creds')
     channel_creds = grpc.composite_channel_credentials(
         transport_creds, auth_creds)
     target = '%s:%d' % (host, http_client.HTTPS_PORT)
-    channel = grpc.secure_channel(target, channel_creds)
+    channel_args = (
+        ('grpc.secondary_user_agent', user_agent),
+    )
+    channel = grpc.secure_channel(target, channel_creds,
+                                  options=channel_args)
     return stub_class(channel)
 
 
diff --git a/google/cloud/bigtable/client.py b/google/cloud/bigtable/client.py
--- a/google/cloud/bigtable/client.py
+++ b/google/cloud/bigtable/client.py
@@ -29,8 +29,6 @@
 
 import os
 
-from pkg_resources import get_distribution
-
 from google.cloud._helpers import make_insecure_stub
 from google.cloud._helpers import make_secure_stub
 from google.cloud.bigtable._generated import bigtable_instance_admin_pb2
@@ -42,6 +40,7 @@
 from google.cloud.bigtable.instance import _EXISTING_INSTANCE_LOCATION_ID
 from google.cloud.client import _ClientFactoryMixin
 from google.cloud.client import _ClientProjectMixin
+from google.cloud.connection import DEFAULT_USER_AGENT
 from google.cloud.credentials import get_credentials
 from google.cloud.environment_vars import BIGTABLE_EMULATOR
 
@@ -64,10 +63,6 @@
 READ_ONLY_SCOPE = 'https://www.googleapis.com/auth/bigtable.data.readonly'
 """Scope for reading table data."""
 
-DEFAULT_USER_AGENT = 'google-cloud-python/{0}'.format(
-    get_distribution('google-cloud').version)
-"""The default user agent for API requests."""
-
 
 def _make_data_stub(client):
     """Creates gRPC stub to make requests to the Data API.
diff --git a/google/cloud/connection.py b/google/cloud/connection.py
--- a/google/cloud/connection.py
+++ b/google/cloud/connection.py
@@ -27,6 +27,10 @@
 API_BASE_URL = 'https://www.googleapis.com'
 """The base of the API call URL."""
 
+DEFAULT_USER_AGENT = 'gcloud-python/{0}'.format(
+    get_distribution('google-cloud').version)
+"""The user agent for google-cloud-python requests."""
+
 
 class Connection(object):
     """A generic connection to Google Cloud Platform.
@@ -63,9 +67,7 @@ class Connection(object):
     :param http: An optional HTTP object to make requests.
     """
 
-    USER_AGENT = "google-cloud-python/{0}".format(
-        get_distribution('google-cloud').version)
-    """The user agent for google-cloud-python requests."""
+    USER_AGENT = DEFAULT_USER_AGENT
 
     SCOPE = None
     """The scopes required for authenticating with a service.


Test patch:
diff --git a/unit_tests/test__helpers.py b/unit_tests/test__helpers.py
--- a/unit_tests/test__helpers.py
+++ b/unit_tests/test__helpers.py
@@ -866,25 +866,21 @@ def _makeOne(self, *args, **kwargs):
 
     def test_constructor(self):
         credentials = object()
-        user_agent = object()
-        plugin = self._makeOne(credentials, user_agent)
+        plugin = self._makeOne(credentials)
         self.assertIs(plugin._credentials, credentials)
-        self.assertIs(plugin._user_agent, user_agent)
 
     def test___call__(self):
         access_token_expected = 'FOOBARBAZ'
         credentials = _Credentials(access_token=access_token_expected)
-        user_agent = 'USER_AGENT'
         callback_args = []
 
         def callback(*args):
             callback_args.append(args)
 
-        transformer = self._makeOne(credentials, user_agent)
+        transformer = self._makeOne(credentials)
         result = transformer(None, callback)
         cb_headers = [
             ('authorization', 'Bearer ' + access_token_expected),
-            ('user-agent', user_agent),
         ]
         self.assertEqual(result, None)
         self.assertEqual(callback_args, [(cb_headers, None)])
@@ -930,8 +926,8 @@ def composite_channel_credentials(self, *args):
                 self.composite_channel_credentials_args = args
                 return COMPOSITE_CREDS
 
-            def secure_channel(self, *args):
-                self.secure_channel_args = args
+            def secure_channel(self, *args, **kwargs):
+                self.secure_channel_args = (args, kwargs)
                 return CHANNEL
 
         grpc_mod = _GRPCModule()
@@ -957,7 +953,7 @@ def mock_plugin(*args):
 
         self.assertTrue(result is mock_result)
         self.assertEqual(stub_inputs, [CHANNEL])
-        self.assertEqual(plugin_args, [(credentials, user_agent)])
+        self.assertEqual(plugin_args, [(credentials,)])
         self.assertEqual(grpc_mod.ssl_channel_credentials_args, ())
         self.assertEqual(grpc_mod.metadata_call_credentials_args,
                          ((metadata_plugin,), {'name': 'google_creds'}))
@@ -965,8 +961,12 @@ def mock_plugin(*args):
             grpc_mod.composite_channel_credentials_args,
             (SSL_CREDS, METADATA_CREDS))
         target = '%s:%d' % (host, http_client.HTTPS_PORT)
+        secure_args = (target, COMPOSITE_CREDS)
+        secure_kwargs = {
+            'options': (('grpc.secondary_user_agent', user_agent),)
+        }
         self.assertEqual(grpc_mod.secure_channel_args,
-                         (target, COMPOSITE_CREDS))
+                         (secure_args, secure_kwargs))
 
 
 class Test_make_insecure_stub(unittest.TestCase):
diff --git a/unit_tests/test_connection.py b/unit_tests/test_connection.py
--- a/unit_tests/test_connection.py
+++ b/unit_tests/test_connection.py
@@ -69,7 +69,7 @@ def test_http_w_creds(self):
 
     def test_user_agent_format(self):
         from pkg_resources import get_distribution
-        expected_ua = 'google-cloud-python/{0}'.format(
+        expected_ua = 'gcloud-python/{0}'.format(
             get_distribution('google-cloud').version)
         conn = self._makeOne()
         self.assertEqual(conn.USER_AGENT, expected_ua)