Skip to content

Commit

Permalink
Moving user agent to client class from _helpers module.
Browse files Browse the repository at this point in the history
This allows a user to over-ride rather than digging into a
private module.
  • Loading branch information
dhermes committed Jul 28, 2015
1 parent ac8ac24 commit f9c8807
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions gcloud_bigtable/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
# See https://gist.github.com/dhermes/bbc5b7be1932bfffae77
# for appropriate values on other systems.
SSL_CERT_FILE = '/etc/ssl/certs/ca-certificates.crt'
USER_AGENT = 'gcloud-bigtable-python'


class MetadataTransformer(object):
Expand All @@ -57,13 +56,14 @@ class MetadataTransformer(object):

def __init__(self, client):
self._credentials = client.credentials
self._user_agent = client.user_agent

def __call__(self, ignored_val):
"""Adds authorization header to request metadata."""
access_token = self._credentials.get_access_token().access_token
return [
('Authorization', 'Bearer ' + access_token),
('User-agent', USER_AGENT),
('User-agent', self._user_agent),
]


Expand Down
8 changes: 7 additions & 1 deletion gcloud_bigtable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"""Environment variable used to provide an implicit project ID."""

DEFAULT_TIMEOUT_SECONDS = 10
DEFAULT_USER_AGENT = 'gcloud-bigtable-python'


def _project_id_from_environment():
Expand Down Expand Up @@ -174,6 +175,10 @@ class Client(object):
interact with the Cluster Admin or Table Admin APIs. This
requires the ``ADMIN_SCOPE``. Defaults to ``False``.
:type user_agent: string
:param user_agent: (Optional) The user agent to be used with API request.
Defaults to ``DEFAULT_USER_AGENT``.
:type timeout_seconds: integer
:param timeout_seconds: Number of seconds for request time-out. If not
passed, defaults to ``DEFAULT_TIMEOUT_SECONDS``.
Expand All @@ -183,7 +188,7 @@ class Client(object):
"""

def __init__(self, credentials=None, project_id=None,
read_only=False, admin=False,
read_only=False, admin=False, user_agent=DEFAULT_USER_AGENT,
timeout_seconds=DEFAULT_TIMEOUT_SECONDS):
if read_only and admin:
raise ValueError('A read-only client cannot also perform'
Expand All @@ -203,6 +208,7 @@ def __init__(self, credentials=None, project_id=None,

self._credentials = credentials.create_scoped(scopes)
self._project_id = _determine_project_id(project_id)
self.user_agent = user_agent
self.timeout_seconds = timeout_seconds

@classmethod
Expand Down
9 changes: 6 additions & 3 deletions gcloud_bigtable/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,21 @@ def test_constructor(self):
scoped_creds = object()
credentials = _MockWithAttachedMethods(scoped_creds)
project_id = 'PROJECT_ID'
client = Client(credentials, project_id=project_id)
user_agent = 'USER_AGENT'
client = Client(credentials, project_id=project_id,
user_agent=user_agent)
transformer = self._makeOne(client)
self.assertTrue(transformer._credentials is scoped_creds)
self.assertEqual(transformer._user_agent, user_agent)
self.assertEqual(credentials._called, [
('create_scoped', ([DATA_SCOPE],), {}),
])

def test___call__(self):
from gcloud_bigtable._testing import _MockWithAttachedMethods
from gcloud_bigtable._helpers import USER_AGENT
from gcloud_bigtable.client import Client
from gcloud_bigtable.client import DATA_SCOPE
from gcloud_bigtable.client import DEFAULT_USER_AGENT

access_token_expected = 'FOOBARBAZ'

Expand All @@ -62,7 +65,7 @@ class _ReturnVal(object):
result,
[
('Authorization', 'Bearer ' + access_token_expected),
('User-agent', USER_AGENT),
('User-agent', DEFAULT_USER_AGENT),
])
self.assertEqual(credentials._called, [
('create_scoped', ([DATA_SCOPE],), {}),
Expand Down
14 changes: 12 additions & 2 deletions gcloud_bigtable/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)

def _constructor_test_helper(self, expected_scopes, project_id=None,
admin=False, read_only=False):
read_only=False, admin=False,
user_agent=None):
from gcloud_bigtable._testing import _MockCalled
from gcloud_bigtable._testing import _MockWithAttachedMethods
from gcloud_bigtable._testing import _Monkey
Expand All @@ -263,14 +264,16 @@ def _constructor_test_helper(self, expected_scopes, project_id=None,
mock_determine_project_id = _MockCalled(determined_project_id)
with _Monkey(MUT, _determine_project_id=mock_determine_project_id):
client = self._makeOne(credentials, project_id=project_id,
admin=admin, read_only=read_only)
read_only=read_only, admin=admin,
user_agent=user_agent)

self.assertTrue(client._credentials is scoped_creds)
self.assertEqual(credentials._called, [
('create_scoped', (expected_scopes,), {}),
])
self.assertTrue(client._project_id is determined_project_id)
self.assertEqual(client.timeout_seconds, MUT.DEFAULT_TIMEOUT_SECONDS)
self.assertEqual(client.user_agent, user_agent)
mock_determine_project_id.check_called(self, [(project_id,)])

def test_constructor_default(self):
Expand All @@ -287,6 +290,7 @@ def test_constructor_default(self):

self.assertEqual(client.project_id, PROJECT_ID)
self.assertTrue(client._credentials is scoped_creds)
self.assertEqual(client.user_agent, MUT.DEFAULT_USER_AGENT)
self.assertEqual(mock_creds_class._called,
[('get_application_default', (), {})])
expected_scopes = [MUT.DATA_SCOPE]
Expand All @@ -304,6 +308,12 @@ def test_constructor_with_explicit_project_id(self):
expected_scopes = [MUT.DATA_SCOPE]
self._constructor_test_helper(expected_scopes, project_id=PROJECT_ID)

def test_constructor_with_explicit_user_agent(self):
from gcloud_bigtable import client as MUT
user_agent = 'USER_AGENT'
expected_scopes = [MUT.DATA_SCOPE]
self._constructor_test_helper(expected_scopes, user_agent=user_agent)

def test_constructor_with_admin(self):
from gcloud_bigtable import client as MUT
expected_scopes = [MUT.DATA_SCOPE, MUT.ADMIN_SCOPE]
Expand Down

0 comments on commit f9c8807

Please sign in to comment.