Skip to content

Commit 891b060

Browse files
authored
Merge pull request #2284 from dhermes/user-agent-options
Reverting user-agent change that came with rename.
2 parents 298b28a + bf8dcce commit 891b060

File tree

5 files changed

+24
-28
lines changed

5 files changed

+24
-28
lines changed

google/cloud/_helpers.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -598,14 +598,10 @@ class MetadataPlugin(object):
598598
:type credentials: :class:`oauth2client.client.OAuth2Credentials`
599599
:param credentials: The OAuth2 Credentials to use for creating
600600
access tokens.
601-
602-
:type user_agent: str
603-
:param user_agent: The user agent to be used with API requests.
604601
"""
605602

606-
def __init__(self, credentials, user_agent):
603+
def __init__(self, credentials):
607604
self._credentials = credentials
608-
self._user_agent = user_agent
609605

610606
def __call__(self, unused_context, callback):
611607
"""Adds authorization header to request metadata.
@@ -620,7 +616,6 @@ def __call__(self, unused_context, callback):
620616
access_token = self._credentials.get_access_token().access_token
621617
headers = [
622618
('authorization', 'Bearer ' + access_token),
623-
('user-agent', self._user_agent),
624619
]
625620
callback(headers, None)
626621

@@ -649,13 +644,17 @@ def make_secure_stub(credentials, user_agent, stub_class, host):
649644
# ssl_channel_credentials() loads root certificates from
650645
# `grpc/_adapter/credentials/roots.pem`.
651646
transport_creds = grpc.ssl_channel_credentials()
652-
custom_metadata_plugin = MetadataPlugin(credentials, user_agent)
647+
custom_metadata_plugin = MetadataPlugin(credentials)
653648
auth_creds = grpc.metadata_call_credentials(
654649
custom_metadata_plugin, name='google_creds')
655650
channel_creds = grpc.composite_channel_credentials(
656651
transport_creds, auth_creds)
657652
target = '%s:%d' % (host, http_client.HTTPS_PORT)
658-
channel = grpc.secure_channel(target, channel_creds)
653+
channel_args = (
654+
('grpc.secondary_user_agent', user_agent),
655+
)
656+
channel = grpc.secure_channel(target, channel_creds,
657+
options=channel_args)
659658
return stub_class(channel)
660659

661660

google/cloud/bigtable/client.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929

3030
import os
3131

32-
from pkg_resources import get_distribution
33-
3432
from google.cloud._helpers import make_insecure_stub
3533
from google.cloud._helpers import make_secure_stub
3634
from google.cloud.bigtable._generated import bigtable_instance_admin_pb2
@@ -42,6 +40,7 @@
4240
from google.cloud.bigtable.instance import _EXISTING_INSTANCE_LOCATION_ID
4341
from google.cloud.client import _ClientFactoryMixin
4442
from google.cloud.client import _ClientProjectMixin
43+
from google.cloud.connection import DEFAULT_USER_AGENT
4544
from google.cloud.credentials import get_credentials
4645
from google.cloud.environment_vars import BIGTABLE_EMULATOR
4746

@@ -64,10 +63,6 @@
6463
READ_ONLY_SCOPE = 'https://www.googleapis.com/auth/bigtable.data.readonly'
6564
"""Scope for reading table data."""
6665

67-
DEFAULT_USER_AGENT = 'google-cloud-python/{0}'.format(
68-
get_distribution('google-cloud').version)
69-
"""The default user agent for API requests."""
70-
7166

7267
def _make_data_stub(client):
7368
"""Creates gRPC stub to make requests to the Data API.

google/cloud/connection.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
API_BASE_URL = 'https://www.googleapis.com'
2828
"""The base of the API call URL."""
2929

30+
DEFAULT_USER_AGENT = 'gcloud-python/{0}'.format(
31+
get_distribution('google-cloud').version)
32+
"""The user agent for google-cloud-python requests."""
33+
3034

3135
class Connection(object):
3236
"""A generic connection to Google Cloud Platform.
@@ -63,9 +67,7 @@ class Connection(object):
6367
:param http: An optional HTTP object to make requests.
6468
"""
6569

66-
USER_AGENT = "google-cloud-python/{0}".format(
67-
get_distribution('google-cloud').version)
68-
"""The user agent for google-cloud-python requests."""
70+
USER_AGENT = DEFAULT_USER_AGENT
6971

7072
SCOPE = None
7173
"""The scopes required for authenticating with a service.

unit_tests/test__helpers.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -878,25 +878,21 @@ def _makeOne(self, *args, **kwargs):
878878

879879
def test_constructor(self):
880880
credentials = object()
881-
user_agent = object()
882-
plugin = self._makeOne(credentials, user_agent)
881+
plugin = self._makeOne(credentials)
883882
self.assertIs(plugin._credentials, credentials)
884-
self.assertIs(plugin._user_agent, user_agent)
885883

886884
def test___call__(self):
887885
access_token_expected = 'FOOBARBAZ'
888886
credentials = _Credentials(access_token=access_token_expected)
889-
user_agent = 'USER_AGENT'
890887
callback_args = []
891888

892889
def callback(*args):
893890
callback_args.append(args)
894891

895-
transformer = self._makeOne(credentials, user_agent)
892+
transformer = self._makeOne(credentials)
896893
result = transformer(None, callback)
897894
cb_headers = [
898895
('authorization', 'Bearer ' + access_token_expected),
899-
('user-agent', user_agent),
900896
]
901897
self.assertEqual(result, None)
902898
self.assertEqual(callback_args, [(cb_headers, None)])
@@ -942,8 +938,8 @@ def composite_channel_credentials(self, *args):
942938
self.composite_channel_credentials_args = args
943939
return COMPOSITE_CREDS
944940

945-
def secure_channel(self, *args):
946-
self.secure_channel_args = args
941+
def secure_channel(self, *args, **kwargs):
942+
self.secure_channel_args = (args, kwargs)
947943
return CHANNEL
948944

949945
grpc_mod = _GRPCModule()
@@ -969,16 +965,20 @@ def mock_plugin(*args):
969965

970966
self.assertTrue(result is mock_result)
971967
self.assertEqual(stub_inputs, [CHANNEL])
972-
self.assertEqual(plugin_args, [(credentials, user_agent)])
968+
self.assertEqual(plugin_args, [(credentials,)])
973969
self.assertEqual(grpc_mod.ssl_channel_credentials_args, ())
974970
self.assertEqual(grpc_mod.metadata_call_credentials_args,
975971
((metadata_plugin,), {'name': 'google_creds'}))
976972
self.assertEqual(
977973
grpc_mod.composite_channel_credentials_args,
978974
(SSL_CREDS, METADATA_CREDS))
979975
target = '%s:%d' % (host, http_client.HTTPS_PORT)
976+
secure_args = (target, COMPOSITE_CREDS)
977+
secure_kwargs = {
978+
'options': (('grpc.secondary_user_agent', user_agent),)
979+
}
980980
self.assertEqual(grpc_mod.secure_channel_args,
981-
(target, COMPOSITE_CREDS))
981+
(secure_args, secure_kwargs))
982982

983983

984984
class Test_make_insecure_stub(unittest.TestCase):

unit_tests/test_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test_http_w_creds(self):
6969

7070
def test_user_agent_format(self):
7171
from pkg_resources import get_distribution
72-
expected_ua = 'google-cloud-python/{0}'.format(
72+
expected_ua = 'gcloud-python/{0}'.format(
7373
get_distribution('google-cloud').version)
7474
conn = self._makeOne()
7575
self.assertEqual(conn.USER_AGENT, expected_ua)

0 commit comments

Comments
 (0)