Skip to content

Commit

Permalink
Fully removing connection module.
Browse files Browse the repository at this point in the history
In the process, moving make_stub and MetadataTransformer
into _helpers module.
  • Loading branch information
dhermes committed Jul 28, 2015
1 parent 73c7ba8 commit 0f66b35
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 179 deletions.
7 changes: 0 additions & 7 deletions docs/base-connection.rst

This file was deleted.

1 change: 0 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ Documented Modules
.. toctree::
:maxdepth: 2

base-connection
data-connection
client
cluster
Expand Down
2 changes: 1 addition & 1 deletion gcloud_bigtable/_grpc_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __call__(self, *args, **kwargs):
class GRPCMockTestMixin(unittest2.TestCase):
"""Mix-in to allow easy mocking for gRPC methods.
Expects :func:`gcloud_bigtable.connection.make_stub` to be used
Expects :func:`gcloud_bigtable._helpers.make_stub` to be used
for making API calls.
Also expects that :meth:`setUpClass` will be called and the following
Expand Down
50 changes: 50 additions & 0 deletions gcloud_bigtable/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@
# See https://gist.github.com/dhermes/bbc5b7be1932bfffae77
# for appropriate values on other systems.
SSL_CERT_FILE = '/etc/ssl/certs/ca-certificates.crt'
TIMEOUT_SECONDS = 10
USER_AGENT = 'gcloud-bigtable-python'


class MetadataTransformer(object):
"""Callable class to transform metadata for gRPC requests.
:type credentials: :class:`oauth2client.client.OAuth2Credentials`
:param credentials: The OAuth2 Credentials to use for access tokens
to authorize requests.
"""

def __init__(self, credentials):
self._credentials = 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),
('User-agent', USER_AGENT),
]


class AuthInfo(object):
Expand Down Expand Up @@ -184,3 +206,31 @@ def get_certs():
"""
set_certs(reset=False)
return AuthInfo.ROOT_CERTIFICATES


def make_stub(credentials, stub_factory, host, port):
"""Makes a stub for the an API.
:type credentials: :class:`oauth2client.client.OAuth2Credentials`
:param credentials: The OAuth2 Credentials to use for access tokens
to authorize requests.
:type stub_factory: callable
:param stub_factory: A factory which will create a gRPC stub for
a given service.
:type host: string
:param host: The host for the service.
:type port: integer
:param port: The port for the service.
:rtype: :class:`grpc.early_adopter.implementations._Stub`
:returns: The stub object used to make gRPC requests to the
Data API.
"""
custom_metadata_transformer = MetadataTransformer(credentials)
return stub_factory(host, port,
metadata_transformer=custom_metadata_transformer,
secure=True,
root_certificates=get_certs())
4 changes: 2 additions & 2 deletions gcloud_bigtable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
from gcloud_bigtable._generated import bigtable_cluster_data_pb2 as data_pb2
from gcloud_bigtable._generated import (
bigtable_cluster_service_messages_pb2 as messages_pb2)
from gcloud_bigtable._helpers import TIMEOUT_SECONDS
from gcloud_bigtable._helpers import make_stub
from gcloud_bigtable.cluster import Cluster
from gcloud_bigtable.cluster import CLUSTER_ADMIN_PORT
from gcloud_bigtable.cluster import CLUSTER_ADMIN_HOST
from gcloud_bigtable.cluster import CLUSTER_STUB_FACTORY
from gcloud_bigtable.connection import TIMEOUT_SECONDS
from gcloud_bigtable.connection import make_stub


ADMIN_SCOPE = 'https://www.googleapis.com/auth/cloud-bigtable.admin'
Expand Down
4 changes: 2 additions & 2 deletions gcloud_bigtable/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
from gcloud_bigtable._generated import (
bigtable_table_service_messages_pb2 as table_messages_pb2)
from gcloud_bigtable._generated import operations_pb2
from gcloud_bigtable._helpers import TIMEOUT_SECONDS
from gcloud_bigtable._helpers import _parse_pb_any_to_native
from gcloud_bigtable._helpers import _pb_timestamp_to_datetime
from gcloud_bigtable._helpers import _require_pb_property
from gcloud_bigtable.connection import TIMEOUT_SECONDS
from gcloud_bigtable.connection import make_stub
from gcloud_bigtable._helpers import make_stub
from gcloud_bigtable.table import TABLE_ADMIN_HOST
from gcloud_bigtable.table import TABLE_ADMIN_PORT
from gcloud_bigtable.table import TABLE_STUB_FACTORY
Expand Down
70 changes: 0 additions & 70 deletions gcloud_bigtable/connection.py

This file was deleted.

4 changes: 2 additions & 2 deletions gcloud_bigtable/data_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
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 TIMEOUT_SECONDS
from gcloud_bigtable.connection import make_stub
from gcloud_bigtable._helpers import TIMEOUT_SECONDS
from gcloud_bigtable._helpers import make_stub


DATA_STUB_FACTORY = (bigtable_service_pb2.
Expand Down
4 changes: 2 additions & 2 deletions gcloud_bigtable/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
from gcloud_bigtable._generated import (
bigtable_table_service_messages_pb2 as messages_pb2)
from gcloud_bigtable._generated import bigtable_table_service_pb2
from gcloud_bigtable._helpers import TIMEOUT_SECONDS
from gcloud_bigtable._helpers import _timedelta_to_duration_pb
from gcloud_bigtable.connection import TIMEOUT_SECONDS
from gcloud_bigtable.connection import make_stub
from gcloud_bigtable._helpers import make_stub


TABLE_STUB_FACTORY = (bigtable_table_service_pb2.
Expand Down
76 changes: 76 additions & 0 deletions gcloud_bigtable/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,43 @@
import unittest2


class TestMetadataTransformer(unittest2.TestCase):

def _getTargetClass(self):
from gcloud_bigtable._helpers import MetadataTransformer
return MetadataTransformer

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

def test_constructor(self):
from gcloud_bigtable._testing import _MockWithAttachedMethods
credentials = _MockWithAttachedMethods()
transformer = self._makeOne(credentials)
self.assertTrue(transformer._credentials is credentials)
self.assertEqual(credentials._called, [])

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

access_token_expected = 'FOOBARBAZ'

class _ReturnVal(object):
access_token = access_token_expected

credentials = _MockWithAttachedMethods(_ReturnVal)
transformer = self._makeOne(credentials)
result = transformer(None)
self.assertEqual(
result,
[
('Authorization', 'Bearer ' + access_token_expected),
('User-agent', USER_AGENT),
])
self.assertEqual(credentials._called, [('get_access_token', (), {})])


class Test__pb_timestamp_to_datetime(unittest2.TestCase):

def _callFUT(self, timestamp):
Expand Down Expand Up @@ -275,3 +312,42 @@ class _AuthInfo(object):

self.assertEqual(call_kwargs, [{'reset': False}])
self.assertTrue(result is return_val)


class Test_make_stub(unittest2.TestCase):

def _callFUT(self, credentials, stub_factory, host, port):
from gcloud_bigtable._helpers import make_stub
return make_stub(credentials, stub_factory, host, port)

def test_it(self):
from gcloud_bigtable._testing import _MockCalled
from gcloud_bigtable._testing import _MockWithAttachedMethods
from gcloud_bigtable._testing import _Monkey
from gcloud_bigtable import _helpers as MUT

mock_result = object()
custom_factory = _MockCalled(mock_result)
transformed = object()
transformer = _MockCalled(transformed)

host = 'HOST'
port = 1025
certs = 'FOOBAR'
credentials = _MockWithAttachedMethods()
with _Monkey(MUT, get_certs=lambda: certs,
MetadataTransformer=transformer):
result = self._callFUT(credentials, custom_factory, host, port)

self.assertTrue(result is mock_result)
custom_factory.check_called(
self,
[(host, port)],
[{
'metadata_transformer': transformed,
'secure': True,
'root_certificates': certs,
}],
)
transformer.check_called(self, [(credentials,)])
self.assertEqual(credentials._called, [])
92 changes: 0 additions & 92 deletions gcloud_bigtable/test_connection.py

This file was deleted.

0 comments on commit 0f66b35

Please sign in to comment.