Skip to content

Commit

Permalink
Removing Connection base class.
Browse files Browse the repository at this point in the history
As a result, removing _testing._Credentials, moving the user
agent into MetadataTransformer, and making a few other
tweaks to affected code.
  • Loading branch information
dhermes committed Jul 28, 2015
1 parent 4042289 commit 73c7ba8
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 134 deletions.
4 changes: 1 addition & 3 deletions gcloud_bigtable/_grpc_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ def _grpc_call_helper(self, call_method, method_name, request_obj,
result = call_method(connection)

self.assertTrue(result is expected_result)
self.assertEqual(credentials._called, [
('create_scoped_required', (), {}),
])
self.assertEqual(credentials._called, [])

# Check all the stubs that were created and used as a context
# manager (should be just one).
Expand Down
13 changes: 0 additions & 13 deletions gcloud_bigtable/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@
"""Shared testing utilities."""


class _Credentials(object):

_scopes = None

@staticmethod
def create_scoped_required():
return True

def create_scoped(self, scope):
self._scopes = scope
return self


class _MockCalled(object):

def __init__(self, result=None):
Expand Down
52 changes: 5 additions & 47 deletions gcloud_bigtable/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


TIMEOUT_SECONDS = 10
USER_AGENT = 'gcloud-bigtable-python'


class MetadataTransformer(object):
Expand All @@ -35,53 +36,10 @@ def __init__(self, credentials):
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)]


class Connection(object):
"""HTTP-RPC Connection base class for Google Cloud Bigtable.
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
:class:`NoneType`
:param credentials: The OAuth2 Credentials to use for this connection.
"""

USER_AGENT = 'gcloud-bigtable-python'
SCOPE = None

def __init__(self, credentials=None):
credentials = self._create_scoped_credentials(
credentials, (self.SCOPE,))
self._credentials = credentials

@property
def credentials(self):
"""Getter for current credentials.
:rtype: :class:`oauth2client.client.OAuth2Credentials` or
:class:`NoneType`
:returns: The credentials object associated with this connection.
"""
return self._credentials

@staticmethod
def _create_scoped_credentials(credentials, scope):
"""Create a scoped set of credentials if it is required.
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
:class:`NoneType`
:param credentials: The OAuth2 Credentials to add a scope to.
:type scope: list of URLs
:param scope: the effective service auth scopes for the connection.
:rtype: :class:`oauth2client.client.OAuth2Credentials` or
:class:`NoneType`
:returns: A new credentials object that has a scope added (if needed).
"""
if credentials and credentials.create_scoped_required():
credentials = credentials.create_scoped(scope)
return credentials
return [
('Authorization', 'Bearer ' + access_token),
('User-agent', USER_AGENT),
]


def make_stub(credentials, stub_factory, host, port):
Expand Down
10 changes: 8 additions & 2 deletions gcloud_bigtable/data_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from gcloud_bigtable._generated import bigtable_service_pb2
from gcloud_bigtable._generated import (
bigtable_service_messages_pb2 as messages_pb2)
from gcloud_bigtable.connection import Connection
from gcloud_bigtable.connection import TIMEOUT_SECONDS
from gcloud_bigtable.connection import make_stub

Expand Down Expand Up @@ -104,10 +103,14 @@ def _prepare_read(table_name, row_key=None, row_range=None,
return messages_pb2.ReadRowsRequest(**request_kwargs)


class DataConnection(Connection):
class DataConnection(object):
"""Connection to Google Cloud Bigtable Data API.
Enables interaction with data in an existing table.
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
:class:`NoneType`
:param credentials: The OAuth2 Credentials to use for this connection.
"""

SCOPE = 'https://www.googleapis.com/auth/cloud-bigtable.data'
Expand All @@ -117,6 +120,9 @@ class DataConnection(Connection):
'cloud-bigtable.data.readonly')
"""Read-only scope for data API requests."""

def __init__(self, credentials=None):
self._credentials = credentials

def read_rows(self, table_name, row_key=None, row_range=None,
filter_=None, allow_row_interleaving=None,
num_rows_limit=None, timeout_seconds=TIMEOUT_SECONDS):
Expand Down
59 changes: 5 additions & 54 deletions gcloud_bigtable/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def test_constructor(self):

def test___call__(self):
from gcloud_bigtable._testing import _MockWithAttachedMethods
from gcloud_bigtable.connection import USER_AGENT

access_token_expected = 'FOOBARBAZ'

Expand All @@ -45,63 +46,13 @@ class _ReturnVal(object):
result = transformer(None)
self.assertEqual(
result,
[('Authorization', 'Bearer ' + access_token_expected)])
[
('Authorization', 'Bearer ' + access_token_expected),
('User-agent', USER_AGENT),
])
self.assertEqual(credentials._called, [('get_access_token', (), {})])


class TestConnection(unittest2.TestCase):

def _getTargetClass(self):
from gcloud_bigtable.connection import Connection
return Connection

def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)

def test_constructor(self):
from gcloud_bigtable._testing import _MockWithAttachedMethods
scoped_creds = object()
credentials = _MockWithAttachedMethods(True, scoped_creds)
connection = self._makeOne(credentials=credentials)
self.assertTrue(connection._credentials is scoped_creds)
klass = self._getTargetClass()
self.assertEqual(credentials._called, [
('create_scoped_required', (), {}),
('create_scoped', ((klass.SCOPE,),), {}),
])

def test__create_scoped_credentials_with_no_credentials(self):
klass = self._getTargetClass()
credentials = None
scope = object()

new_credentials = klass._create_scoped_credentials(credentials, scope)
self.assertTrue(new_credentials is credentials)

def test__create_scoped_credentials_with_credentials(self):
from gcloud_bigtable._testing import _MockWithAttachedMethods
klass = self._getTargetClass()
scoped_creds = object()
credentials = _MockWithAttachedMethods(True, scoped_creds)
scope = object()

new_credentials = klass._create_scoped_credentials(credentials, scope)
self.assertTrue(new_credentials is scoped_creds)
self.assertEqual(credentials._called, [
('create_scoped_required', (), {}),
('create_scoped', (scope,), {}),
])

def test_credentials_property(self):
from gcloud_bigtable._testing import _MockWithAttachedMethods
credentials = _MockWithAttachedMethods(False)
connection = self._makeOne(credentials=credentials)
self.assertTrue(connection.credentials is credentials)
self.assertEqual(credentials._called, [
('create_scoped_required', (), {}),
])


class Test_make_stub(unittest2.TestCase):

def _callFUT(self, credentials, stub_factory, host, port):
Expand Down
20 changes: 5 additions & 15 deletions gcloud_bigtable/test_data_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,9 @@ def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)

def test_constructor(self):
from gcloud_bigtable._testing import _MockWithAttachedMethods
klass = self._getTargetClass()
scoped_creds = object()
credentials = _MockWithAttachedMethods(True, scoped_creds)
credentials = object()
connection = self._makeOne(credentials=credentials)
self.assertTrue(connection._credentials is scoped_creds)
self.assertEqual(credentials._called, [
('create_scoped_required', (), {}),
('create_scoped', ((klass.SCOPE,),), {}),
])
self.assertTrue(connection._credentials is credentials)

def test_read_rows(self):
from gcloud_bigtable._testing import _MockCalled
Expand Down Expand Up @@ -96,8 +89,7 @@ def call_method(connection):
self._grpc_call_helper(call_method, 'SampleRowKeys', request_obj)

def test_mutate_row(self):
from gcloud_bigtable._testing import _Credentials
credentials = _Credentials()
credentials = object()
connection = self._makeOne(credentials=credentials)

table_name = object()
Expand All @@ -106,8 +98,7 @@ def test_mutate_row(self):
table_name, row_key)

def test_check_and_mutate_row(self):
from gcloud_bigtable._testing import _Credentials
credentials = _Credentials()
credentials = object()
connection = self._makeOne(credentials=credentials)

table_name = object()
Expand All @@ -116,8 +107,7 @@ def test_check_and_mutate_row(self):
table_name, row_key)

def test_read_modify_write_row(self):
from gcloud_bigtable._testing import _Credentials
credentials = _Credentials()
credentials = object()
connection = self._makeOne(credentials=credentials)

table_name = object()
Expand Down

0 comments on commit 73c7ba8

Please sign in to comment.