Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions google/cloud/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)

Expand Down Expand Up @@ -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)


Expand Down
7 changes: 1 addition & 6 deletions google/cloud/bigtable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions google/cloud/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 10 additions & 10 deletions unit_tests/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
Expand Down Expand Up @@ -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()
Expand All @@ -957,16 +953,20 @@ 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'}))
self.assertEqual(
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):
Expand Down
2 changes: 1 addition & 1 deletion unit_tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down