diff --git a/bigtable/google/cloud/bigtable/__init__.py b/bigtable/google/cloud/bigtable/__init__.py index c22cb3fc5379..0c815b8b0988 100644 --- a/bigtable/google/cloud/bigtable/__init__.py +++ b/bigtable/google/cloud/bigtable/__init__.py @@ -15,4 +15,7 @@ """Google Cloud Bigtable API package.""" +from pkg_resources import get_distribution +__version__ = get_distribution('google-cloud-bigtable').version + from google.cloud.bigtable.client import Client diff --git a/bigtable/google/cloud/bigtable/client.py b/bigtable/google/cloud/bigtable/client.py index 06b35c6d9e94..2f552b1c2564 100644 --- a/bigtable/google/cloud/bigtable/client.py +++ b/bigtable/google/cloud/bigtable/client.py @@ -32,21 +32,24 @@ import os import google.auth.credentials +from google.gax.utils import metrics from google.longrunning import operations_grpc from google.cloud._helpers import make_insecure_stub from google.cloud._helpers import make_secure_stub +from google.cloud._http import DEFAULT_USER_AGENT +from google.cloud.client import _ClientFactoryMixin +from google.cloud.client import _ClientProjectMixin +from google.cloud.credentials import get_credentials +from google.cloud.environment_vars import BIGTABLE_EMULATOR + +from google.cloud.bigtable import __version__ from google.cloud.bigtable._generated import bigtable_instance_admin_pb2 from google.cloud.bigtable._generated import bigtable_pb2 from google.cloud.bigtable._generated import bigtable_table_admin_pb2 from google.cloud.bigtable.cluster import DEFAULT_SERVE_NODES from google.cloud.bigtable.instance import Instance 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._http import DEFAULT_USER_AGENT -from google.cloud.credentials import get_credentials -from google.cloud.environment_vars import BIGTABLE_EMULATOR TABLE_ADMIN_HOST = 'bigtableadmin.googleapis.com' @@ -67,10 +70,17 @@ READ_ONLY_SCOPE = 'https://www.googleapis.com/auth/bigtable.data.readonly' """Scope for reading table data.""" +_METRICS_HEADERS = ( + ('gccl', __version__), +) +_HEADER_STR = metrics.stringify(metrics.fill(_METRICS_HEADERS)) +_GRPC_EXTRA_OPTIONS = ( + ('x-goog-api-client', _HEADER_STR), +) # NOTE: 'grpc.max_message_length' will no longer be recognized in # grpcio 1.1 and later. _MAX_MSG_LENGTH_100MB = 100 * 1024 * 1024 -_GRPC_MAX_LENGTH_OPTIONS = ( +_GRPC_MAX_LENGTH_OPTIONS = _GRPC_EXTRA_OPTIONS + ( ('grpc.max_message_length', _MAX_MSG_LENGTH_100MB), ('grpc.max_receive_message_length', _MAX_MSG_LENGTH_100MB), ) @@ -107,7 +117,7 @@ def _make_instance_stub(client): return make_secure_stub( client.credentials, client.user_agent, bigtable_instance_admin_pb2.BigtableInstanceAdminStub, - INSTANCE_ADMIN_HOST) + INSTANCE_ADMIN_HOST, extra_options=_GRPC_EXTRA_OPTIONS) else: return make_insecure_stub( bigtable_instance_admin_pb2.BigtableInstanceAdminStub, @@ -127,9 +137,10 @@ def _make_operations_stub(client): :returns: A gRPC stub object. """ if client.emulator_host is None: - return make_secure_stub(client.credentials, client.user_agent, - operations_grpc.OperationsStub, - OPERATIONS_API_HOST) + return make_secure_stub( + client.credentials, client.user_agent, + operations_grpc.OperationsStub, + OPERATIONS_API_HOST, extra_options=_GRPC_EXTRA_OPTIONS) else: return make_insecure_stub(operations_grpc.OperationsStub, client.emulator_host) @@ -148,7 +159,7 @@ def _make_table_stub(client): return make_secure_stub( client.credentials, client.user_agent, bigtable_table_admin_pb2.BigtableTableAdminStub, - TABLE_ADMIN_HOST) + TABLE_ADMIN_HOST, extra_options=_GRPC_EXTRA_OPTIONS) else: return make_insecure_stub( bigtable_table_admin_pb2.BigtableTableAdminStub, diff --git a/bigtable/setup.py b/bigtable/setup.py index 06db1ac10ce1..25e12f76b011 100644 --- a/bigtable/setup.py +++ b/bigtable/setup.py @@ -51,7 +51,7 @@ REQUIREMENTS = [ 'google-cloud-core >= 0.23.1, < 0.24dev', - 'grpcio >= 1.0.2, < 2.0dev', + 'google-gax>=0.15.7, <0.16dev', ] setup( diff --git a/bigtable/unit_tests/test_client.py b/bigtable/unit_tests/test_client.py index b7b666e819df..8761dc360c81 100644 --- a/bigtable/unit_tests/test_client.py +++ b/bigtable/unit_tests/test_client.py @@ -36,39 +36,24 @@ def _call_fut(self, client): return _make_data_stub(client) - def test_without_emulator(self): - from google.cloud._testing import _Monkey + @mock.patch('google.cloud.bigtable.client.make_secure_stub', + return_value=mock.sentinel.stub) + def test_without_emulator(self, make_stub): from google.cloud.bigtable import client as MUT credentials = _make_credentials() user_agent = 'you-sir-age-int' client = _Client(credentials, user_agent) - fake_stub = object() - make_secure_stub_args = [] - - def mock_make_secure_stub(*args, **kwargs): - make_secure_stub_args.append(args) - make_secure_stub_args.append(kwargs) - return fake_stub - - with _Monkey(MUT, make_secure_stub=mock_make_secure_stub): - result = self._call_fut(client) - - extra_options = {'extra_options': ( - ('grpc.max_message_length', 104857600), - ('grpc.max_receive_message_length', 104857600) - )} - self.assertIs(result, fake_stub) - self.assertEqual(make_secure_stub_args, [ - ( - client.credentials, - client.user_agent, - MUT.bigtable_pb2.BigtableStub, - MUT.DATA_API_HOST, - ), - extra_options, - ]) + result = self._call_fut(client) + self.assertIs(result, mock.sentinel.stub) + make_stub.assert_called_once_with( + client.credentials, + client.user_agent, + MUT.bigtable_pb2.BigtableStub, + MUT.DATA_API_HOST, + extra_options=MUT._GRPC_MAX_LENGTH_OPTIONS, + ) def test_with_emulator(self): from google.cloud._testing import _Monkey @@ -103,33 +88,24 @@ def _call_fut(self, client): return _make_instance_stub(client) - def test_without_emulator(self): - from google.cloud._testing import _Monkey + @mock.patch('google.cloud.bigtable.client.make_secure_stub', + return_value=mock.sentinel.stub) + def test_without_emulator(self, make_stub): from google.cloud.bigtable import client as MUT credentials = _make_credentials() user_agent = 'you-sir-age-int' client = _Client(credentials, user_agent) - fake_stub = object() - make_secure_stub_args = [] - - def mock_make_secure_stub(*args): - make_secure_stub_args.append(args) - return fake_stub - - with _Monkey(MUT, make_secure_stub=mock_make_secure_stub): - result = self._call_fut(client) - - self.assertIs(result, fake_stub) - self.assertEqual(make_secure_stub_args, [ - ( - client.credentials, - client.user_agent, - MUT.bigtable_instance_admin_pb2.BigtableInstanceAdminStub, - MUT.INSTANCE_ADMIN_HOST, - ), - ]) + result = self._call_fut(client) + self.assertIs(result, mock.sentinel.stub) + make_stub.assert_called_once_with( + client.credentials, + client.user_agent, + MUT.bigtable_instance_admin_pb2.BigtableInstanceAdminStub, + MUT.INSTANCE_ADMIN_HOST, + extra_options=MUT._GRPC_EXTRA_OPTIONS, + ) def test_with_emulator(self): from google.cloud._testing import _Monkey @@ -164,35 +140,25 @@ def _call_fut(self, client): return _make_operations_stub(client) - def test_without_emulator(self): + @mock.patch('google.cloud.bigtable.client.make_secure_stub', + return_value=mock.sentinel.stub) + def test_without_emulator(self, make_stub): from google.longrunning import operations_grpc - - from google.cloud._testing import _Monkey from google.cloud.bigtable import client as MUT credentials = _make_credentials() user_agent = 'you-sir-age-int' client = _Client(credentials, user_agent) - fake_stub = object() - make_secure_stub_args = [] - - def mock_make_secure_stub(*args): - make_secure_stub_args.append(args) - return fake_stub - - with _Monkey(MUT, make_secure_stub=mock_make_secure_stub): - result = self._call_fut(client) - - self.assertIs(result, fake_stub) - self.assertEqual(make_secure_stub_args, [ - ( - client.credentials, - client.user_agent, - operations_grpc.OperationsStub, - MUT.OPERATIONS_API_HOST, - ), - ]) + result = self._call_fut(client) + self.assertIs(result, mock.sentinel.stub) + make_stub.assert_called_once_with( + client.credentials, + client.user_agent, + operations_grpc.OperationsStub, + MUT.OPERATIONS_API_HOST, + extra_options=MUT._GRPC_EXTRA_OPTIONS, + ) def test_with_emulator(self): from google.longrunning import operations_grpc @@ -229,33 +195,24 @@ def _call_fut(self, client): return _make_table_stub(client) - def test_without_emulator(self): - from google.cloud._testing import _Monkey + @mock.patch('google.cloud.bigtable.client.make_secure_stub', + return_value=mock.sentinel.stub) + def test_without_emulator(self, make_stub): from google.cloud.bigtable import client as MUT credentials = _make_credentials() user_agent = 'you-sir-age-int' client = _Client(credentials, user_agent) - fake_stub = object() - make_secure_stub_args = [] - - def mock_make_secure_stub(*args): - make_secure_stub_args.append(args) - return fake_stub - - with _Monkey(MUT, make_secure_stub=mock_make_secure_stub): - result = self._call_fut(client) - - self.assertIs(result, fake_stub) - self.assertEqual(make_secure_stub_args, [ - ( - client.credentials, - client.user_agent, - MUT.bigtable_table_admin_pb2.BigtableTableAdminStub, - MUT.TABLE_ADMIN_HOST, - ), - ]) + result = self._call_fut(client) + self.assertIs(result, mock.sentinel.stub) + make_stub.assert_called_once_with( + client.credentials, + client.user_agent, + MUT.bigtable_table_admin_pb2.BigtableTableAdminStub, + MUT.TABLE_ADMIN_HOST, + extra_options=MUT._GRPC_EXTRA_OPTIONS, + ) def test_with_emulator(self): from google.cloud._testing import _Monkey