def test_catch_error(self): def abortion_error_func(*dummy_args, **dummy_kwargs): raise CustomException(None, None) def other_error_func(*dummy_args, **dummy_kwargs): raise AnotherException gax_error_callable = api_callable.create_api_call( abortion_error_func, _CallSettings()) self.assertRaises(GaxError, gax_error_callable, None) other_error_callable = api_callable.create_api_call( other_error_func, _CallSettings()) self.assertRaises(AnotherException, other_error_callable, None)
def test_catch_error(self): def abortion_error_func(*dummy_args, **dummy_kwargs): raise CustomException(None, None) def other_error_func(*dummy_args, **dummy_kwargs): raise AnotherException gax_error_callable = api_callable.create_api_call( abortion_error_func, CallSettings()) self.assertRaises(GaxError, gax_error_callable, None) other_error_callable = api_callable.create_api_call( other_error_func, CallSettings()) self.assertRaises(AnotherException, other_error_callable, None)
def __init__(self, service_path=SERVICE_ADDRESS, port=DEFAULT_SERVICE_PORT, channel=None, metadata_transformer=None, ssl_creds=None, scopes=None, client_config=None, app_name='gax', app_version=_GAX_VERSION): """Constructor. Args: service_path (string): The domain name of the API remote host. port (int): The port on which to connect to the remote host. channel (:class:`grpc.beta.implementations.Channel`): A ``Channel`` object through which to make calls. ssl_creds (:class:`grpc.beta.implementations.ClientCredentials`): A `ClientCredentials` for use with an SSL-enabled channel. client_config (dict): A dictionary for call options for each method. See :func:`google.gax.construct_settings` for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points. metadata_transformer (Callable[[], list]): A function that creates the metadata for requests. app_name (string): The codename of the calling service. app_version (string): The version of the calling service. Returns: A ImageAnnotatorApi object. """ if scopes is None: scopes = self._ALL_SCOPES if client_config is None: client_config = {} goog_api_client = '{}/{} {} gax/{} python/{}'.format( app_name, app_version, self._CODE_GEN_NAME_VERSION, self._GAX_VERSION, platform.python_version()) metadata = [('x-goog-api-client', goog_api_client)] default_client_config = json.loads( pkg_resources.resource_string( __name__, 'image_annotator_client_config.json').decode()) defaults = api_callable.construct_settings( 'google.cloud.vision.v1.ImageAnnotator', default_client_config, client_config, config.STATUS_CODE_NAMES, kwargs={'metadata': metadata}) self.stub = config.create_stub( image_annotator_pb2.beta_create_ImageAnnotator_stub, service_path, port, ssl_creds=ssl_creds, channel=channel, metadata_transformer=metadata_transformer, scopes=scopes) self._batch_annotate_images = api_callable.create_api_call( self.stub.BatchAnnotateImages, settings=defaults['batch_annotate_images'])
def test_call_kwargs(self): settings = CallSettings(kwargs={'key': 'value'}) my_callable = api_callable.create_api_call( lambda _req, _timeout, **kwargs: kwargs['key'], settings) self.assertEqual(my_callable(None), 'value') self.assertEqual(my_callable(None, CallOptions(key='updated')), 'updated')
def test_call_kwargs(self): settings = _CallSettings(kwargs={'key': 'value'}) my_callable = api_callable.create_api_call( lambda _req, _timeout, **kwargs: kwargs['key'], settings) self.assertEqual(my_callable(None), 'value') self.assertEqual(my_callable(None, CallOptions(key='updated')), 'updated')
def test_retry_times_out_no_response(self, mock_time): mock_time.return_value = 1 retry = RetryOptions([_FAKE_STATUS_CODE_1], BackoffSettings(0, 0, 0, 0, 0, 0, 0)) settings = _CallSettings(timeout=0, retry=retry) my_callable = api_callable.create_api_call(lambda: None, settings) self.assertRaises(RetryError, my_callable, None)
def test_retry_times_out_no_response(self, mock_time): mock_time.return_value = 1 retry = RetryOptions( [_FAKE_STATUS_CODE_1], BackoffSettings(0, 0, 0, 0, 0, 0, 0)) settings = CallSettings(timeout=0, retry=retry) my_callable = api_callable.create_api_call(lambda: None, settings) self.assertRaises(RetryError, my_callable, None)
def test_wrap_value_error(self): invalid_attribute_exc = grpc.RpcError() invalid_attribute_exc.code = lambda: grpc.StatusCode.INVALID_ARGUMENT def value_error_func(*dummy_args, **dummy_kwargs): raise invalid_attribute_exc value_error_callable = api_callable.create_api_call( value_error_func, _CallSettings()) self.assertRaises(ValueError, value_error_callable, None)
def test_no_retry_if_no_codes(self, mock_time): retry = RetryOptions([], BackoffSettings(1, 2, 3, 4, 5, 6, 7)) mock_call = mock.Mock() mock_call.side_effect = CustomException('', _FAKE_STATUS_CODE_1) mock_time.return_value = 0 settings = CallSettings(timeout=0, retry=retry) my_callable = api_callable.create_api_call(mock_call, settings) self.assertRaises(CustomException, my_callable, None) self.assertEqual(mock_call.call_count, 1)
def test_retry_aborts_on_unexpected_exception(self, mock_exc_to_code, mock_time): mock_exc_to_code.side_effect = lambda e: e.code retry = RetryOptions([_FAKE_STATUS_CODE_1], BackoffSettings(0, 0, 0, 0, 0, 0, 1)) mock_call = mock.Mock() mock_call.side_effect = CustomException('', _FAKE_STATUS_CODE_2) mock_time.return_value = 0 settings = _CallSettings(timeout=0, retry=retry) my_callable = api_callable.create_api_call(mock_call, settings) self.assertRaises(Exception, my_callable, None) self.assertEqual(mock_call.call_count, 1)
def test_retry_aborts_on_unexpected_exception( self, mock_exc_to_code, mock_time): mock_exc_to_code.side_effect = lambda e: e.code retry = RetryOptions( [_FAKE_STATUS_CODE_1], BackoffSettings(0, 0, 0, 0, 0, 0, 1)) mock_call = mock.Mock() mock_call.side_effect = CustomException('', _FAKE_STATUS_CODE_2) mock_time.return_value = 0 settings = CallSettings(timeout=0, retry=retry) my_callable = api_callable.create_api_call(mock_call, settings) self.assertRaises(Exception, my_callable, None) self.assertEqual(mock_call.call_count, 1)
def test_retry_aborts_simple(self, mock_exc_to_code, mock_time): def fake_call(dummy_request, dummy_timeout): raise CustomException('', _FAKE_STATUS_CODE_1) retry = RetryOptions([_FAKE_STATUS_CODE_1], BackoffSettings(0, 0, 0, 0, 0, 0, 1)) mock_time.side_effect = [0, 2] mock_exc_to_code.side_effect = lambda e: e.code settings = _CallSettings(timeout=0, retry=retry) my_callable = api_callable.create_api_call(fake_call, settings) try: my_callable(None) except RetryError as exc: self.assertIsInstance(exc.cause, CustomException)
def test_retry_aborts_simple(self, mock_exc_to_code, mock_time): def fake_call(dummy_request, dummy_timeout): raise CustomException('', _FAKE_STATUS_CODE_1) retry = RetryOptions( [_FAKE_STATUS_CODE_1], BackoffSettings(0, 0, 0, 0, 0, 0, 1)) mock_time.side_effect = [0, 2] mock_exc_to_code.side_effect = lambda e: e.code settings = CallSettings(timeout=0, retry=retry) my_callable = api_callable.create_api_call(fake_call, settings) try: my_callable(None) except RetryError as exc: self.assertIsInstance(exc.cause, CustomException)
def test_retry(self, mock_exc_to_code, mock_time): mock_exc_to_code.side_effect = lambda e: e.code to_attempt = 3 retry = RetryOptions([_FAKE_STATUS_CODE_1], BackoffSettings(0, 0, 0, 0, 0, 0, 1)) # Succeeds on the to_attempt'th call, and never again afterward mock_call = mock.Mock() mock_call.side_effect = ([CustomException('', _FAKE_STATUS_CODE_1)] * (to_attempt - 1) + [mock.DEFAULT]) mock_call.return_value = 1729 mock_time.return_value = 0 settings = _CallSettings(timeout=0, retry=retry) my_callable = api_callable.create_api_call(mock_call, settings) self.assertEqual(my_callable(None), 1729) self.assertEqual(mock_call.call_count, to_attempt)
def test_retry_times_out_simple(self, mock_exc_to_code, mock_time): mock_exc_to_code.side_effect = lambda e: e.code to_attempt = 3 retry = RetryOptions([_FAKE_STATUS_CODE_1], BackoffSettings(0, 0, 0, 0, 0, 0, 1)) mock_call = mock.Mock() mock_call.side_effect = CustomException('', _FAKE_STATUS_CODE_1) mock_time.side_effect = ([0] * to_attempt + [2]) settings = _CallSettings(timeout=0, retry=retry) my_callable = api_callable.create_api_call(mock_call, settings) try: my_callable(None) except RetryError as exc: self.assertIsInstance(exc.cause, CustomException) self.assertEqual(mock_call.call_count, to_attempt)
def test_retry(self, mock_exc_to_code, mock_time): mock_exc_to_code.side_effect = lambda e: e.code to_attempt = 3 retry = RetryOptions( [_FAKE_STATUS_CODE_1], BackoffSettings(0, 0, 0, 0, 0, 0, 1)) # Succeeds on the to_attempt'th call, and never again afterward mock_call = mock.Mock() mock_call.side_effect = ([CustomException('', _FAKE_STATUS_CODE_1)] * (to_attempt - 1) + [mock.DEFAULT]) mock_call.return_value = 1729 mock_time.return_value = 0 settings = CallSettings(timeout=0, retry=retry) my_callable = api_callable.create_api_call(mock_call, settings) self.assertEqual(my_callable(None), 1729) self.assertEqual(mock_call.call_count, to_attempt)
def test_retry_times_out_simple(self, mock_exc_to_code, mock_time): mock_exc_to_code.side_effect = lambda e: e.code to_attempt = 3 retry = RetryOptions( [_FAKE_STATUS_CODE_1], BackoffSettings(0, 0, 0, 0, 0, 0, 1)) mock_call = mock.Mock() mock_call.side_effect = CustomException('', _FAKE_STATUS_CODE_1) mock_time.side_effect = ([0] * to_attempt + [2]) settings = CallSettings(timeout=0, retry=retry) my_callable = api_callable.create_api_call(mock_call, settings) try: my_callable(None) except RetryError as exc: self.assertIsInstance(exc.cause, CustomException) self.assertEqual(mock_call.call_count, to_attempt)
def test_wrap_value_error(self): from google.gax.errors import InvalidArgumentError invalid_attribute_exc = grpc.RpcError() invalid_attribute_exc.code = lambda: grpc.StatusCode.INVALID_ARGUMENT def value_error_func(*dummy_args, **dummy_kwargs): raise invalid_attribute_exc value_error_callable = api_callable.create_api_call( value_error_func, _CallSettings()) with self.assertRaises(ValueError) as exc_info: value_error_callable(None) self.assertIsInstance(exc_info.exception, InvalidArgumentError) self.assertEqual(exc_info.exception.args, (u'RPC failed', )) self.assertIs(exc_info.exception.cause, invalid_attribute_exc)
def test_call_merge_options_metadata(self): settings_kwargs = { 'key': 'value', 'metadata': [('key1', 'val1'), ('key2', 'val2')] } settings = _CallSettings(kwargs=settings_kwargs) my_callable = api_callable.create_api_call( lambda _req, _timeout, **kwargs: kwargs, settings) # Merge empty options, settings.kwargs['metadata'] remain unchanged expected_kwargs = settings_kwargs self.assertEqual(my_callable(None), expected_kwargs) # Override an existing key in settings.kwargs['metadata'] expected_kwargs = { 'key': 'value', 'metadata': [('key1', '_val1'), ('key2', 'val2')] } self.assertEqual( my_callable(None, CallOptions(metadata=[('key1', '_val1')])), expected_kwargs) # Add a new key in settings.kwargs['metadata'] expected_kwargs = { 'key': 'value', 'metadata': [('key3', 'val3'), ('key1', 'val1'), ('key2', 'val2')] } self.assertEqual( my_callable(None, CallOptions(metadata=[('key3', 'val3')])), expected_kwargs) # Do all: add a new key and override an existing one in # settings.kwargs['metadata'] expected_kwargs = { 'key': 'value', 'metadata': [('key3', 'val3'), ('key2', '_val2'), ('key1', 'val1')] } self.assertEqual( my_callable( None, CallOptions(metadata=[('key3', 'val3'), ('key2', '_val2')])), expected_kwargs)
def test_page_streaming(self): # A mock grpc function that page streams a list of consecutive # integers, returning `page_size` integers with each call and using # the next integer to return as the page token, until `pages_to_stream` # pages have been returned. page_size = 3 pages_to_stream = 5 # pylint: disable=abstract-method, too-few-public-methods class PageStreamingRequest(object): def __init__(self, page_token=0): self.page_token = page_token class PageStreamingResponse(object): def __init__(self, nums=(), next_page_token=0): self.nums = nums self.next_page_token = next_page_token fake_grpc_func_descriptor = PageDescriptor( 'page_token', 'next_page_token', 'nums') def grpc_return_value(request, *dummy_args, **dummy_kwargs): if (request.page_token > 0 and request.page_token < page_size * pages_to_stream): return PageStreamingResponse( nums=iter(range(request.page_token, request.page_token + page_size)), next_page_token=request.page_token + page_size) elif request.page_token >= page_size * pages_to_stream: return PageStreamingResponse() else: return PageStreamingResponse(nums=iter(range(page_size)), next_page_token=page_size) with mock.patch('grpc.framework.crust.implementations.' '_UnaryUnaryMultiCallable') as mock_grpc: mock_grpc.side_effect = grpc_return_value settings = CallSettings( page_descriptor=fake_grpc_func_descriptor, timeout=0) my_callable = api_callable.create_api_call(mock_grpc, settings=settings) self.assertEqual(list(my_callable(PageStreamingRequest())), list(range(page_size * pages_to_stream)))
def test_bundling(self): # pylint: disable=abstract-method, too-few-public-methods class BundlingRequest(object): def __init__(self, elements=None): self.elements = elements fake_grpc_func_descriptor = BundleDescriptor('elements', []) bundler = bundling.Executor(BundleOptions(element_count_threshold=8)) def my_func(request, dummy_timeout): return len(request.elements) settings = _CallSettings(bundler=bundler, bundle_descriptor=fake_grpc_func_descriptor, timeout=0) my_callable = api_callable.create_api_call(my_func, settings) first = my_callable(BundlingRequest([0] * 3)) self.assertIsInstance(first, bundling.Event) self.assertIsNone(first.result) # pylint: disable=no-member second = my_callable(BundlingRequest([0] * 5)) self.assertEqual(second.result, 8) # pylint: disable=no-member
def test_bundling(self): # pylint: disable=abstract-method, too-few-public-methods class BundlingRequest(object): def __init__(self, elements=None): self.elements = elements fake_grpc_func_descriptor = BundleDescriptor('elements', []) bundler = bundling.Executor(BundleOptions(element_count_threshold=8)) def my_func(request, dummy_timeout): return len(request.elements) settings = CallSettings( bundler=bundler, bundle_descriptor=fake_grpc_func_descriptor, timeout=0) my_callable = api_callable.create_api_call(my_func, settings) first = my_callable(BundlingRequest([0] * 3)) self.assertIsInstance(first, bundling.Event) self.assertIsNone(first.result) # pylint: disable=no-member second = my_callable(BundlingRequest([0] * 5)) self.assertEquals(second.result, 8) # pylint: disable=no-member
def test_retry_exponential_backoff(self, mock_exc_to_code, mock_time, mock_sleep): # pylint: disable=too-many-locals mock_exc_to_code.side_effect = lambda e: e.code MILLIS_PER_SEC = 1000 mock_time.return_value = 0 def incr_time(secs): mock_time.return_value += secs def api_call(dummy_request, timeout, **dummy_kwargs): incr_time(timeout) raise CustomException(str(timeout), _FAKE_STATUS_CODE_1) mock_call = mock.Mock() mock_sleep.side_effect = incr_time mock_call.side_effect = api_call params = BackoffSettings(3, 2, 24, 5, 2, 80, 2500) retry = RetryOptions([_FAKE_STATUS_CODE_1], params) settings = CallSettings(timeout=0, retry=retry) my_callable = api_callable.create_api_call(mock_call, settings) try: my_callable(None) except RetryError as exc: self.assertIsInstance(exc.cause, CustomException) self.assertGreaterEqual(mock_time(), params.total_timeout_millis / MILLIS_PER_SEC) # Very rough bounds calls_lower_bound = params.total_timeout_millis / ( params.max_retry_delay_millis + params.max_rpc_timeout_millis) self.assertGreater(mock_call.call_count, calls_lower_bound) calls_upper_bound = (params.total_timeout_millis / params.initial_retry_delay_millis) self.assertLess(mock_call.call_count, calls_upper_bound)
def __init__(self, service_path=SERVICE_ADDRESS, port=DEFAULT_SERVICE_PORT, channel=None, metadata_transformer=None, ssl_creds=None, scopes=None, client_config=None, app_name='gax', app_version=_GAX_VERSION): """Constructor. Args: service_path (string): The domain name of the API remote host. port (int): The port on which to connect to the remote host. channel (:class:`grpc.beta.implementations.Channel`): A ``Channel`` object through which to make calls. ssl_creds (:class:`grpc.beta.implementations.ClientCredentials`): A `ClientCredentials` for use with an SSL-enabled channel. client_config (dict): A dictionary for call options for each method. See :func:`google.gax.construct_settings` for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points. metadata_transformer (Callable[[], list]): A function that creates the metadata for requests. app_name (string): The codename of the calling service. app_version (string): The version of the calling service. Returns: A SubscriberApi object. """ if scopes is None: scopes = self._ALL_SCOPES if client_config is None: client_config = {} goog_api_client = '{}/{} {} gax/{} python/{}'.format( app_name, app_version, self._CODE_GEN_NAME_VERSION, self._GAX_VERSION, platform.python_version()) metadata = [('x-goog-api-client', goog_api_client)] default_client_config = json.loads(pkg_resources.resource_string( __name__, 'subscriber_client_config.json')) defaults = api_callable.construct_settings( 'google.pubsub.v1.Subscriber', default_client_config, client_config, config.STATUS_CODE_NAMES, kwargs={'metadata': metadata}, page_descriptors=self._PAGE_DESCRIPTORS) self.stub = config.create_stub( pubsub_pb2.beta_create_Subscriber_stub, service_path, port, ssl_creds=ssl_creds, channel=channel, metadata_transformer=metadata_transformer, scopes=scopes) self._create_subscription = api_callable.create_api_call( self.stub.CreateSubscription, settings=defaults['create_subscription']) self._get_subscription = api_callable.create_api_call( self.stub.GetSubscription, settings=defaults['get_subscription']) self._list_subscriptions = api_callable.create_api_call( self.stub.ListSubscriptions, settings=defaults['list_subscriptions']) self._delete_subscription = api_callable.create_api_call( self.stub.DeleteSubscription, settings=defaults['delete_subscription']) self._modify_ack_deadline = api_callable.create_api_call( self.stub.ModifyAckDeadline, settings=defaults['modify_ack_deadline']) self._acknowledge = api_callable.create_api_call( self.stub.Acknowledge, settings=defaults['acknowledge']) self._pull = api_callable.create_api_call(self.stub.Pull, settings=defaults['pull']) self._modify_push_config = api_callable.create_api_call( self.stub.ModifyPushConfig, settings=defaults['modify_push_config'])
def test_call_override(self): settings = CallSettings(timeout=10) my_callable = api_callable.create_api_call( lambda _req, timeout: timeout, settings) self.assertEqual(my_callable(None, CallOptions(timeout=20)), 20)
def __init__(self, service_path=SERVICE_ADDRESS, port=DEFAULT_SERVICE_PORT, channel=None, metadata_transformer=None, ssl_creds=None, scopes=None, client_config=None, timeout=_DEFAULT_TIMEOUT, app_name='gax', app_version=_GAX_VERSION): """Constructor. Args: service_path (string): The domain name of the API remote host. port (int): The port on which to connect to the remote host. channel (:class:`grpc.beta.implementations.Channel`): A ``Channel`` object through which to make calls. ssl_creds (:class:`grpc.beta.implementations.ClientCredentials`): A `ClientCredentials` for use with an SSL-enabled channel. client_config (dict): A dictionary for call options for each method. See :func:`google.gax.construct_settings` for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points. metadata_transformer (Callable[[], list]): A function that creates the metadata for requests. timeout (int): The default timeout, in seconds, for calls made through this client app_name (string): The codename of the calling service. app_version (string): The version of the calling service. Returns: A MetricsServiceV2Api object. """ if scopes is None: scopes = self._ALL_SCOPES if client_config is None: client_config = {} goog_api_client = '{}/{} {} gax/{} python/{}'.format( app_name, app_version, self._CODE_GEN_NAME_VERSION, self._GAX_VERSION, platform.python_version()) metadata = [('x-goog-api-client', goog_api_client)] default_client_config = json.loads(pkg_resources.resource_string( __name__, 'metrics_service_v2_client_config.json')) defaults = api_callable.construct_settings( 'google.logging.v2.MetricsServiceV2', default_client_config, client_config, config.STATUS_CODE_NAMES, timeout, kwargs={'metadata': metadata}, page_descriptors=self._PAGE_DESCRIPTORS) self.stub = config.create_stub( logging_metrics_pb2.beta_create_MetricsServiceV2_stub, service_path, port, ssl_creds=ssl_creds, channel=channel, metadata_transformer=metadata_transformer, scopes=scopes) self._list_log_metrics = api_callable.create_api_call( self.stub.ListLogMetrics, settings=defaults['list_log_metrics']) self._get_log_metric = api_callable.create_api_call( self.stub.GetLogMetric, settings=defaults['get_log_metric']) self._create_log_metric = api_callable.create_api_call( self.stub.CreateLogMetric, settings=defaults['create_log_metric']) self._update_log_metric = api_callable.create_api_call( self.stub.UpdateLogMetric, settings=defaults['update_log_metric']) self._delete_log_metric = api_callable.create_api_call( self.stub.DeleteLogMetric, settings=defaults['delete_log_metric'])
def __init__(self, service_path=SERVICE_ADDRESS, port=DEFAULT_SERVICE_PORT, channel=None, metadata_transformer=None, ssl_creds=None, scopes=None, client_config=None, app_name='gax', app_version=_GAX_VERSION): """Constructor. Args: service_path (string): The domain name of the API remote host. port (int): The port on which to connect to the remote host. channel (:class:`grpc.Channel`): A ``Channel`` instance through which to make calls. ssl_creds (:class:`grpc.ChannelCredentials`): A ``ChannelCredentials`` instance for use with an SSL-enabled channel. client_config (dict): A dictionary for call options for each method. See :func:`google.gax.construct_settings` for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points. metadata_transformer (Callable[[], list]): A function that creates the metadata for requests. app_name (string): The codename of the calling service. app_version (string): The version of the calling service. Returns: A BigtableApi object. """ if scopes is None: scopes = self._ALL_SCOPES if client_config is None: client_config = {} goog_api_client = '{}/{} {} gax/{} python/{}'.format( app_name, app_version, self._CODE_GEN_NAME_VERSION, self._GAX_VERSION, platform.python_version()) metadata = [('x-goog-api-client', goog_api_client)] default_client_config = json.loads( pkg_resources.resource_string( __name__, 'bigtable_client_config.json').decode()) defaults = api_callable.construct_settings( 'google.bigtable.v2.Bigtable', default_client_config, client_config, config.STATUS_CODE_NAMES, kwargs={'metadata': metadata}) self.bigtable_stub = config.create_stub( bigtable_pb2.BigtableStub, service_path, port, ssl_creds=ssl_creds, channel=channel, metadata_transformer=metadata_transformer, scopes=scopes) self._read_rows = api_callable.create_api_call( self.bigtable_stub.ReadRows, settings=defaults['read_rows']) self._sample_row_keys = api_callable.create_api_call( self.bigtable_stub.SampleRowKeys, settings=defaults['sample_row_keys']) self._mutate_row = api_callable.create_api_call( self.bigtable_stub.MutateRow, settings=defaults['mutate_row']) self._mutate_rows = api_callable.create_api_call( self.bigtable_stub.MutateRows, settings=defaults['mutate_rows']) self._check_and_mutate_row = api_callable.create_api_call( self.bigtable_stub.CheckAndMutateRow, settings=defaults['check_and_mutate_row']) self._read_modify_write_row = api_callable.create_api_call( self.bigtable_stub.ReadModifyWriteRow, settings=defaults['read_modify_write_row'])
def __init__(self, service_path=SERVICE_ADDRESS, port=DEFAULT_SERVICE_PORT, channel=None, metadata_transformer=None, ssl_creds=None, scopes=None, client_config=None, app_name='gax', app_version=_GAX_VERSION): """Constructor. Args: service_path (string): The domain name of the API remote host. port (int): The port on which to connect to the remote host. channel (:class:`grpc.Channel`): A ``Channel`` instance through which to make calls. ssl_creds (:class:`grpc.ChannelCredentials`): A ``ChannelCredentials`` instance for use with an SSL-enabled channel. client_config (dict): A dictionary for call options for each method. See :func:`google.gax.construct_settings` for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points. metadata_transformer (Callable[[], list]): A function that creates the metadata for requests. app_name (string): The codename of the calling service. app_version (string): The version of the calling service. Returns: A IAMApi object. """ if scopes is None: scopes = self._ALL_SCOPES if client_config is None: client_config = {} goog_api_client = '{}/{} {} gax/{} python/{}'.format( app_name, app_version, self._CODE_GEN_NAME_VERSION, self._GAX_VERSION, platform.python_version()) metadata = [('x-goog-api-client', goog_api_client)] default_client_config = json.loads( pkg_resources.resource_string(__name__, 'iam_client_config.json').decode()) defaults = api_callable.construct_settings( 'google.iam.admin.v1.IAM', default_client_config, client_config, config.STATUS_CODE_NAMES, kwargs={'metadata': metadata}, page_descriptors=self._PAGE_DESCRIPTORS) self.iam_stub = config.create_stub( iam_pb2.IAMStub, service_path, port, ssl_creds=ssl_creds, channel=channel, metadata_transformer=metadata_transformer, scopes=scopes) self._list_service_accounts = api_callable.create_api_call( self.iam_stub.ListServiceAccounts, settings=defaults['list_service_accounts']) self._get_service_account = api_callable.create_api_call( self.iam_stub.GetServiceAccount, settings=defaults['get_service_account']) self._create_service_account = api_callable.create_api_call( self.iam_stub.CreateServiceAccount, settings=defaults['create_service_account']) self._update_service_account = api_callable.create_api_call( self.iam_stub.UpdateServiceAccount, settings=defaults['update_service_account']) self._delete_service_account = api_callable.create_api_call( self.iam_stub.DeleteServiceAccount, settings=defaults['delete_service_account']) self._list_service_account_keys = api_callable.create_api_call( self.iam_stub.ListServiceAccountKeys, settings=defaults['list_service_account_keys']) self._get_service_account_key = api_callable.create_api_call( self.iam_stub.GetServiceAccountKey, settings=defaults['get_service_account_key']) self._create_service_account_key = api_callable.create_api_call( self.iam_stub.CreateServiceAccountKey, settings=defaults['create_service_account_key']) self._delete_service_account_key = api_callable.create_api_call( self.iam_stub.DeleteServiceAccountKey, settings=defaults['delete_service_account_key']) self._sign_blob = api_callable.create_api_call( self.iam_stub.SignBlob, settings=defaults['sign_blob']) self._get_iam_policy = api_callable.create_api_call( self.iam_stub.GetIamPolicy, settings=defaults['get_iam_policy']) self._set_iam_policy = api_callable.create_api_call( self.iam_stub.SetIamPolicy, settings=defaults['set_iam_policy']) self._test_iam_permissions = api_callable.create_api_call( self.iam_stub.TestIamPermissions, settings=defaults['test_iam_permissions']) self._query_grantable_roles = api_callable.create_api_call( self.iam_stub.QueryGrantableRoles, settings=defaults['query_grantable_roles'])
def test_call_api_call(self): settings = CallSettings() my_callable = api_callable.create_api_call( lambda _req, _timeout: 42, settings) self.assertEqual(my_callable(None), 42)
def __init__(self, service_path=SERVICE_ADDRESS, port=DEFAULT_SERVICE_PORT, channel=None, credentials=None, ssl_credentials=None, scopes=None, client_config=None, app_name=None, app_version='', lib_name=None, lib_version='', metrics_headers=()): """Constructor. Args: service_path (string): The domain name of the API remote host. port (int): The port on which to connect to the remote host. channel (:class:`grpc.Channel`): A ``Channel`` instance through which to make calls. credentials (object): The authorization credentials to attach to requests. These credentials identify this application to the service. ssl_credentials (:class:`grpc.ChannelCredentials`): A ``ChannelCredentials`` instance for use with an SSL-enabled channel. scopes (list[string]): A list of OAuth2 scopes to attach to requests. client_config (dict): A dictionary for call options for each method. See :func:`google.gax.construct_settings` for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points. app_name (string): The name of the application calling the service. Recommended for analytics purposes. app_version (string): The version of the application calling the service. Recommended for analytics purposes. lib_name (string): The API library software used for calling the service. (Unless you are writing an API client itself, leave this as default.) lib_version (string): The API library software version used for calling the service. (Unless you are writing an API client itself, leave this as default.) metrics_headers (dict): A dictionary of values for tracking client library metrics. Ultimately serializes to a string (e.g. 'foo/1.2.3 bar/3.14.1'). This argument should be considered private. Returns: A OperationsClient object. """ # Unless the calling application specifically requested # OAuth scopes, request everything. if scopes is None: scopes = self._ALL_SCOPES # Initialize an empty client config, if none is set. if client_config is None: client_config = {} # Initialize metrics_headers as an ordered dictionary # (cuts down on cardinality of the resulting string slightly). metrics_headers = collections.OrderedDict(metrics_headers) metrics_headers['gl-python'] = platform.python_version() # The library may or may not be set, depending on what is # calling this client. Newer client libraries set the library name # and version. if lib_name: metrics_headers[lib_name] = lib_version # Load the configuration defaults. default_client_config = json.loads( pkg_resources.resource_string( __name__, 'operations_client_config.json').decode()) defaults = api_callable.construct_settings( 'google.longrunning.Operations', default_client_config, client_config, config.STATUS_CODE_NAMES, metrics_headers=metrics_headers, page_descriptors=self._PAGE_DESCRIPTORS, ) self.operations_stub = config.create_stub( operations_pb2.OperationsStub, channel=channel, service_path=service_path, service_port=port, credentials=credentials, scopes=scopes, ssl_credentials=ssl_credentials) self._get_operation = api_callable.create_api_call( self.operations_stub.GetOperation, settings=defaults['get_operation']) self._list_operations = api_callable.create_api_call( self.operations_stub.ListOperations, settings=defaults['list_operations']) self._cancel_operation = api_callable.create_api_call( self.operations_stub.CancelOperation, settings=defaults['cancel_operation']) self._delete_operation = api_callable.create_api_call( self.operations_stub.DeleteOperation, settings=defaults['delete_operation'])
def test_page_streaming(self): # A mock grpc function that page streams a list of consecutive # integers, returning `page_size` integers with each call and using # the next integer to return as the page token, until `pages_to_stream` # pages have been returned. # pylint:disable=too-many-locals page_size = 3 pages_to_stream = 5 # pylint: disable=abstract-method, too-few-public-methods class PageStreamingRequest(object): def __init__(self, page_token=0): self.page_token = page_token class PageStreamingResponse(object): def __init__(self, nums=(), next_page_token=0): self.nums = nums self.next_page_token = next_page_token fake_grpc_func_descriptor = PageDescriptor( 'page_token', 'next_page_token', 'nums') def grpc_return_value(request, *dummy_args, **dummy_kwargs): start = int(request.page_token) if request.page_token else 0 if start > 0 and start < page_size * pages_to_stream: return PageStreamingResponse( nums=list(range(start, start + page_size)), next_page_token=start + page_size) elif start >= page_size * pages_to_stream: return PageStreamingResponse() else: return PageStreamingResponse(nums=list(range(page_size)), next_page_token=page_size) with mock.patch('grpc.framework.crust.implementations.' '_UnaryUnaryMultiCallable') as mock_grpc: mock_grpc.side_effect = grpc_return_value settings = CallSettings( page_descriptor=fake_grpc_func_descriptor, timeout=0) my_callable = api_callable.create_api_call( mock_grpc, settings=settings) self.assertEqual(list(my_callable(PageStreamingRequest())), list(range(page_size * pages_to_stream))) unflattened_settings = CallSettings( page_descriptor=fake_grpc_func_descriptor, timeout=0, flatten_pages=False, page_token=INITIAL_PAGE) unflattened_callable = api_callable.create_api_call( mock_grpc, settings=unflattened_settings) # Expect a list of pages_to_stream pages, each of size page_size, # plus one empty page expected = [list(range(page_size * n, page_size * (n + 1))) for n in range(pages_to_stream)] + [()] self.assertEqual(list(unflattened_callable(PageStreamingRequest())), expected) pages_already_read = 2 explicit_page_token_settings = CallSettings( page_descriptor=fake_grpc_func_descriptor, timeout=0, flatten_pages=False, page_token=str(page_size * pages_already_read)) explicit_page_token_callable = api_callable.create_api_call( mock_grpc, settings=explicit_page_token_settings) # Expect a list of pages_to_stream pages, each of size page_size, # plus one empty page, minus the pages_already_read expected = [list(range(page_size * n, page_size * (n + 1))) for n in range(pages_already_read, pages_to_stream)] expected += [()] self.assertEqual( list(explicit_page_token_callable(PageStreamingRequest())), expected)
def __init__(self, service_path=SERVICE_ADDRESS, port=DEFAULT_SERVICE_PORT, channel=None, credentials=None, ssl_credentials=None, scopes=None, client_config=None, app_name='gax', app_version=_GAX_VERSION): """Constructor. Args: service_path (string): The domain name of the API remote host. port (int): The port on which to connect to the remote host. channel (:class:`grpc.Channel`): A ``Channel`` instance through which to make calls. credentials (object): The authorization credentials to attach to requests. These credentials identify this application to the service. ssl_credentials (:class:`grpc.ChannelCredentials`): A ``ChannelCredentials`` instance for use with an SSL-enabled channel. scopes (list[string]): A list of OAuth2 scopes to attach to requests. client_config (dict): A dictionary for call options for each method. See :func:`google.gax.construct_settings` for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points. app_name (string): The codename of the calling service. app_version (string): The version of the calling service. Returns: A MetricsServiceV2Client object. """ if scopes is None: scopes = self._ALL_SCOPES if client_config is None: client_config = {} goog_api_client = '{}/{} {} gax/{} python/{}'.format( app_name, app_version, self._CODE_GEN_NAME_VERSION, self._GAX_VERSION, platform.python_version()) metadata = [('x-goog-api-client', goog_api_client)] default_client_config = json.loads( pkg_resources.resource_string( __name__, 'metrics_service_v2_client_config.json').decode()) defaults = api_callable.construct_settings( 'google.logging.v2.MetricsServiceV2', default_client_config, client_config, config.STATUS_CODE_NAMES, kwargs={'metadata': metadata}, page_descriptors=self._PAGE_DESCRIPTORS) self.metrics_service_v2_stub = config.create_stub( logging_metrics_pb2.MetricsServiceV2Stub, channel=channel, service_path=service_path, service_port=port, credentials=credentials, scopes=scopes, ssl_credentials=ssl_credentials) self._list_log_metrics = api_callable.create_api_call( self.metrics_service_v2_stub.ListLogMetrics, settings=defaults['list_log_metrics']) self._get_log_metric = api_callable.create_api_call( self.metrics_service_v2_stub.GetLogMetric, settings=defaults['get_log_metric']) self._create_log_metric = api_callable.create_api_call( self.metrics_service_v2_stub.CreateLogMetric, settings=defaults['create_log_metric']) self._update_log_metric = api_callable.create_api_call( self.metrics_service_v2_stub.UpdateLogMetric, settings=defaults['update_log_metric']) self._delete_log_metric = api_callable.create_api_call( self.metrics_service_v2_stub.DeleteLogMetric, settings=defaults['delete_log_metric'])
def __init__(self, service_path=SERVICE_ADDRESS, port=DEFAULT_SERVICE_PORT, channel=None, credentials=None, ssl_credentials=None, scopes=None, client_config=None, app_name=None, app_version='', lib_name=None, lib_version='', metrics_headers=()): """Constructor. Args: service_path (string): The domain name of the API remote host. port (int): The port on which to connect to the remote host. channel (:class:`grpc.Channel`): A ``Channel`` instance through which to make calls. credentials (object): The authorization credentials to attach to requests. These credentials identify this application to the service. ssl_credentials (:class:`grpc.ChannelCredentials`): A ``ChannelCredentials`` instance for use with an SSL-enabled channel. scopes (list[string]): A list of OAuth2 scopes to attach to requests. client_config (dict): A dictionary for call options for each method. See :func:`google.gax.construct_settings` for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points. app_name (string): The name of the application calling the service. Recommended for analytics purposes. app_version (string): The version of the application calling the service. Recommended for analytics purposes. lib_name (string): The API library software used for calling the service. (Unless you are writing an API client itself, leave this as default.) lib_version (string): The API library software version used for calling the service. (Unless you are writing an API client itself, leave this as default.) metrics_headers (dict): A dictionary of values for tracking client library metrics. Ultimately serializes to a string (e.g. 'foo/1.2.3 bar/3.14.1'). This argument should be considered private. Returns: A SubscriberClient object. """ # Unless the calling application specifically requested # OAuth scopes, request everything. if scopes is None: scopes = self._ALL_SCOPES # Initialize an empty client config, if none is set. if client_config is None: client_config = {} # Initialize metrics_headers as an ordered dictionary # (cuts down on cardinality of the resulting string slightly). metrics_headers = collections.OrderedDict(metrics_headers) metrics_headers['gl-python'] = platform.python_version() # The library may or may not be set, depending on what is # calling this client. Newer client libraries set the library name # and version. if lib_name: metrics_headers[lib_name] = lib_version # Finally, track the GAPIC package version. metrics_headers['gapic'] = pkg_resources.get_distribution( 'google-cloud-pubsub', ).version # Load the configuration defaults. default_client_config = json.loads( pkg_resources.resource_string( __name__, 'subscriber_client_config.json').decode()) defaults = api_callable.construct_settings( 'google.pubsub.v1.Subscriber', default_client_config, client_config, config.STATUS_CODE_NAMES, metrics_headers=metrics_headers, page_descriptors=self._PAGE_DESCRIPTORS, ) self.iam_policy_stub = config.create_stub( iam_policy_pb2.IAMPolicyStub, channel=channel, service_path=service_path, service_port=port, credentials=credentials, scopes=scopes, ssl_credentials=ssl_credentials) self.subscriber_stub = config.create_stub( pubsub_pb2.SubscriberStub, channel=channel, service_path=service_path, service_port=port, credentials=credentials, scopes=scopes, ssl_credentials=ssl_credentials) self._create_subscription = api_callable.create_api_call( self.subscriber_stub.CreateSubscription, settings=defaults['create_subscription']) self._get_subscription = api_callable.create_api_call( self.subscriber_stub.GetSubscription, settings=defaults['get_subscription']) self._update_subscription = api_callable.create_api_call( self.subscriber_stub.UpdateSubscription, settings=defaults['update_subscription']) self._list_subscriptions = api_callable.create_api_call( self.subscriber_stub.ListSubscriptions, settings=defaults['list_subscriptions']) self._delete_subscription = api_callable.create_api_call( self.subscriber_stub.DeleteSubscription, settings=defaults['delete_subscription']) self._modify_ack_deadline = api_callable.create_api_call( self.subscriber_stub.ModifyAckDeadline, settings=defaults['modify_ack_deadline']) self._acknowledge = api_callable.create_api_call( self.subscriber_stub.Acknowledge, settings=defaults['acknowledge']) self._pull = api_callable.create_api_call(self.subscriber_stub.Pull, settings=defaults['pull']) self._streaming_pull = api_callable.create_api_call( self.subscriber_stub.StreamingPull, settings=defaults['streaming_pull']) self._modify_push_config = api_callable.create_api_call( self.subscriber_stub.ModifyPushConfig, settings=defaults['modify_push_config']) self._list_snapshots = api_callable.create_api_call( self.subscriber_stub.ListSnapshots, settings=defaults['list_snapshots']) self._create_snapshot = api_callable.create_api_call( self.subscriber_stub.CreateSnapshot, settings=defaults['create_snapshot']) self._delete_snapshot = api_callable.create_api_call( self.subscriber_stub.DeleteSnapshot, settings=defaults['delete_snapshot']) self._seek = api_callable.create_api_call(self.subscriber_stub.Seek, settings=defaults['seek']) self._set_iam_policy = api_callable.create_api_call( self.iam_policy_stub.SetIamPolicy, settings=defaults['set_iam_policy']) self._get_iam_policy = api_callable.create_api_call( self.iam_policy_stub.GetIamPolicy, settings=defaults['get_iam_policy']) self._test_iam_permissions = api_callable.create_api_call( self.iam_policy_stub.TestIamPermissions, settings=defaults['test_iam_permissions'])
def __init__(self, channel=None, credentials=None, ssl_credentials=None, scopes=None, client_config=None, lib_name=None, lib_version='', metrics_headers=()): """Constructor. Args: channel (~grpc.Channel): A ``Channel`` instance through which to make calls. credentials (~google.auth.credentials.Credentials): The authorization credentials to attach to requests. These credentials identify this application to the service. ssl_credentials (~grpc.ChannelCredentials): A ``ChannelCredentials`` instance for use with an SSL-enabled channel. scopes (Sequence[str]): A list of OAuth2 scopes to attach to requests. client_config (dict): A dictionary for call options for each method. See :func:`google.gax.construct_settings` for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points. lib_name (str): The API library software used for calling the service. (Unless you are writing an API client itself, leave this as default.) lib_version (str): The API library software version used for calling the service. (Unless you are writing an API client itself, leave this as default.) metrics_headers (dict): A dictionary of values for tracking client library metrics. Ultimately serializes to a string (e.g. 'foo/1.2.3 bar/3.14.1'). This argument should be considered private. """ # Unless the calling application specifically requested # OAuth scopes, request everything. if scopes is None: scopes = self._ALL_SCOPES # Initialize an empty client config, if none is set. if client_config is None: client_config = {} # Initialize metrics_headers as an ordered dictionary # (cuts down on cardinality of the resulting string slightly). metrics_headers = collections.OrderedDict(metrics_headers) metrics_headers['gl-python'] = platform.python_version() # The library may or may not be set, depending on what is # calling this client. Newer client libraries set the library name # and version. if lib_name: metrics_headers[lib_name] = lib_version # Finally, track the GAPIC package version. metrics_headers['gapic'] = pkg_resources.get_distribution( 'google-cloud-videointelligence', ).version # Load the configuration defaults. defaults = api_callable.construct_settings( 'google.cloud.videointelligence.v1beta1.VideoIntelligenceService', video_intelligence_service_client_config.config, client_config, config.STATUS_CODE_NAMES, metrics_headers=metrics_headers, ) self.video_intelligence_service_stub = config.create_stub( video_intelligence_pb2.VideoIntelligenceServiceStub, channel=channel, service_path=self.SERVICE_ADDRESS, service_port=self.DEFAULT_SERVICE_PORT, credentials=credentials, scopes=scopes, ssl_credentials=ssl_credentials) self.operations_client = operations_client.OperationsClient( service_path=self.SERVICE_ADDRESS, channel=channel, credentials=credentials, ssl_credentials=ssl_credentials, scopes=scopes, client_config=client_config, metrics_headers=metrics_headers, ) self._annotate_video = api_callable.create_api_call( self.video_intelligence_service_stub.AnnotateVideo, settings=defaults['annotate_video'])
def test_call_api_call(self): settings = _CallSettings() my_callable = api_callable.create_api_call(lambda _req, _timeout: 42, settings) self.assertEqual(my_callable(None), 42)
def test_call_override(self): settings = _CallSettings(timeout=10) my_callable = api_callable.create_api_call( lambda _req, timeout: timeout, settings) self.assertEqual(my_callable(None, CallOptions(timeout=20)), 20)
def test_bundling_page_streaming_error(self): settings = CallSettings( page_descriptor=object(), bundle_descriptor=object(), bundler=object()) with self.assertRaises(ValueError): api_callable.create_api_call(lambda _req, _timeout: 42, settings)
def __init__(self, service_path=SERVICE_ADDRESS, port=DEFAULT_SERVICE_PORT, channel=None, credentials=None, ssl_credentials=None, scopes=None, client_config=None, app_name='gax', app_version=_GAX_VERSION): """Constructor. Args: service_path (string): The domain name of the API remote host. port (int): The port on which to connect to the remote host. channel (:class:`grpc.Channel`): A ``Channel`` instance through which to make calls. credentials (object): The authorization credentials to attach to requests. These credentials identify this application to the service. ssl_credentials (:class:`grpc.ChannelCredentials`): A ``ChannelCredentials`` instance for use with an SSL-enabled channel. scopes (list[string]): A list of OAuth2 scopes to attach to requests. client_config (dict): A dictionary for call options for each method. See :func:`google.gax.construct_settings` for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points. app_name (string): The codename of the calling service. app_version (string): The version of the calling service. Returns: A SpeechClient object. """ if scopes is None: scopes = self._ALL_SCOPES if client_config is None: client_config = {} goog_api_client = '{}/{} {} gax/{} python/{}'.format( app_name, app_version, self._CODE_GEN_NAME_VERSION, self._GAX_VERSION, platform.python_version()) metadata = [('x-goog-api-client', goog_api_client)] default_client_config = json.loads( pkg_resources.resource_string( __name__, 'speech_client_config.json').decode()) defaults = api_callable.construct_settings( 'google.cloud.speech.v1beta1.Speech', default_client_config, client_config, config.STATUS_CODE_NAMES, kwargs={'metadata': metadata}) self.speech_stub = config.create_stub( cloud_speech_pb2.SpeechStub, channel=channel, service_path=service_path, service_port=port, credentials=credentials, scopes=scopes, ssl_credentials=ssl_credentials) self._sync_recognize = api_callable.create_api_call( self.speech_stub.SyncRecognize, settings=defaults['sync_recognize']) self._async_recognize = api_callable.create_api_call( self.speech_stub.AsyncRecognize, settings=defaults['async_recognize']) self._streaming_recognize = api_callable.create_api_call( self.speech_stub.StreamingRecognize, settings=defaults['streaming_recognize'])
def __init__(self, service_path=SERVICE_ADDRESS, port=DEFAULT_SERVICE_PORT, channel=None, metadata_transformer=None, ssl_creds=None, scopes=None, client_config=None, timeout=_DEFAULT_TIMEOUT, app_name='gax', app_version=_GAX_VERSION): """Constructor. Args: service_path (string): The domain name of the API remote host. port (int): The port on which to connect to the remote host. channel (:class:`grpc.beta.implementations.Channel`): A ``Channel`` object through which to make calls. ssl_creds (:class:`grpc.beta.implementations.ClientCredentials`): A `ClientCredentials` for use with an SSL-enabled channel. client_config (dict): A dictionary for call options for each method. See :func:`google.gax.construct_settings` for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points. metadata_transformer (Callable[[], list]): A function that creates the metadata for requests. timeout (int): The default timeout, in seconds, for calls made through this client app_name (string): The codename of the calling service. app_version (string): The version of the calling service. Returns: A ConfigServiceV2Api object. """ if scopes is None: scopes = self._ALL_SCOPES if client_config is None: client_config = {} goog_api_client = '{}/{} {} gax/{} python/{}'.format( app_name, app_version, self._CODE_GEN_NAME_VERSION, self._GAX_VERSION, platform.python_version()) metadata = [('x-goog-api-client', goog_api_client)] default_client_config = json.loads( pkg_resources.resource_string( __name__, 'config_service_v2_client_config.json')) defaults = api_callable.construct_settings( 'google.logging.v2.ConfigServiceV2', default_client_config, client_config, config.STATUS_CODE_NAMES, timeout, kwargs={'metadata': metadata}, page_descriptors=self._PAGE_DESCRIPTORS) self.stub = config.create_stub( logging_config_pb2.beta_create_ConfigServiceV2_stub, service_path, port, ssl_creds=ssl_creds, channel=channel, metadata_transformer=metadata_transformer, scopes=scopes) self._list_sinks = api_callable.create_api_call( self.stub.ListSinks, settings=defaults['list_sinks']) self._get_sink = api_callable.create_api_call( self.stub.GetSink, settings=defaults['get_sink']) self._create_sink = api_callable.create_api_call( self.stub.CreateSink, settings=defaults['create_sink']) self._update_sink = api_callable.create_api_call( self.stub.UpdateSink, settings=defaults['update_sink']) self._delete_sink = api_callable.create_api_call( self.stub.DeleteSink, settings=defaults['delete_sink'])
def __init__(self, service_path=SERVICE_ADDRESS, port=DEFAULT_SERVICE_PORT, channel=None, credentials=None, ssl_credentials=None, scopes=None, client_config=None, app_name='gax', app_version=_GAX_VERSION): """Constructor. Args: service_path (string): The domain name of the API remote host. port (int): The port on which to connect to the remote host. channel (:class:`grpc.Channel`): A ``Channel`` instance through which to make calls. credentials (object): The authorization credentials to attach to requests. These credentials identify this application to the service. ssl_credentials (:class:`grpc.ChannelCredentials`): A ``ChannelCredentials`` instance for use with an SSL-enabled channel. scopes (list[string]): A list of OAuth2 scopes to attach to requests. client_config (dict): A dictionary for call options for each method. See :func:`google.gax.construct_settings` for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points. app_name (string): The codename of the calling service. app_version (string): The version of the calling service. Returns: A DatastoreClient object. """ if scopes is None: scopes = self._ALL_SCOPES if client_config is None: client_config = {} goog_api_client = '{}/{} {} gax/{} python/{}'.format( app_name, app_version, self._CODE_GEN_NAME_VERSION, self._GAX_VERSION, platform.python_version()) metadata = [('x-goog-api-client', goog_api_client)] default_client_config = json.loads( pkg_resources.resource_string( __name__, 'datastore_client_config.json').decode()) defaults = api_callable.construct_settings( 'google.datastore.v1.Datastore', default_client_config, client_config, config.STATUS_CODE_NAMES, kwargs={'metadata': metadata}) self.datastore_stub = config.create_stub( datastore_pb2.DatastoreStub, channel=channel, service_path=service_path, service_port=port, credentials=credentials, scopes=scopes, ssl_credentials=ssl_credentials) self._lookup = api_callable.create_api_call( self.datastore_stub.Lookup, settings=defaults['lookup']) self._run_query = api_callable.create_api_call( self.datastore_stub.RunQuery, settings=defaults['run_query']) self._begin_transaction = api_callable.create_api_call( self.datastore_stub.BeginTransaction, settings=defaults['begin_transaction']) self._commit = api_callable.create_api_call( self.datastore_stub.Commit, settings=defaults['commit']) self._rollback = api_callable.create_api_call( self.datastore_stub.Rollback, settings=defaults['rollback']) self._allocate_ids = api_callable.create_api_call( self.datastore_stub.AllocateIds, settings=defaults['allocate_ids'])
def __init__(self, channel=None, credentials=None, ssl_credentials=None, scopes=None, client_config=None, lib_name=None, lib_version='', metrics_headers=()): """Constructor. Args: channel (~grpc.Channel): A ``Channel`` instance through which to make calls. credentials (~google.auth.credentials.Credentials): The authorization credentials to attach to requests. These credentials identify this application to the service. ssl_credentials (~grpc.ChannelCredentials): A ``ChannelCredentials`` instance for use with an SSL-enabled channel. scopes (Sequence[str]): A list of OAuth2 scopes to attach to requests. client_config (dict): A dictionary for call options for each method. See :func:`google.gax.construct_settings` for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points. lib_name (str): The API library software used for calling the service. (Unless you are writing an API client itself, leave this as default.) lib_version (str): The API library software version used for calling the service. (Unless you are writing an API client itself, leave this as default.) metrics_headers (dict): A dictionary of values for tracking client library metrics. Ultimately serializes to a string (e.g. 'foo/1.2.3 bar/3.14.1'). This argument should be considered private. """ # Unless the calling application specifically requested # OAuth scopes, request everything. if scopes is None: scopes = self._ALL_SCOPES # Initialize an empty client config, if none is set. if client_config is None: client_config = {} # Initialize metrics_headers as an ordered dictionary # (cuts down on cardinality of the resulting string slightly). metrics_headers = collections.OrderedDict(metrics_headers) metrics_headers['gl-python'] = platform.python_version() # The library may or may not be set, depending on what is # calling this client. Newer client libraries set the library name # and version. if lib_name: metrics_headers[lib_name] = lib_version # Finally, track the GAPIC package version. metrics_headers['gapic'] = pkg_resources.get_distribution( 'google-cloud-firestore', ).version # Load the configuration defaults. defaults = api_callable.construct_settings( 'google.firestore.v1beta1.Firestore', firestore_client_config.config, client_config, config.STATUS_CODE_NAMES, metrics_headers=metrics_headers, page_descriptors=self._PAGE_DESCRIPTORS, ) self.firestore_stub = config.create_stub( firestore_pb2.FirestoreStub, channel=channel, service_path=self.SERVICE_ADDRESS, service_port=self.DEFAULT_SERVICE_PORT, credentials=credentials, scopes=scopes, ssl_credentials=ssl_credentials) self._get_document = api_callable.create_api_call( self.firestore_stub.GetDocument, settings=defaults['get_document']) self._list_documents = api_callable.create_api_call( self.firestore_stub.ListDocuments, settings=defaults['list_documents']) self._create_document = api_callable.create_api_call( self.firestore_stub.CreateDocument, settings=defaults['create_document']) self._update_document = api_callable.create_api_call( self.firestore_stub.UpdateDocument, settings=defaults['update_document']) self._delete_document = api_callable.create_api_call( self.firestore_stub.DeleteDocument, settings=defaults['delete_document']) self._batch_get_documents = api_callable.create_api_call( self.firestore_stub.BatchGetDocuments, settings=defaults['batch_get_documents']) self._begin_transaction = api_callable.create_api_call( self.firestore_stub.BeginTransaction, settings=defaults['begin_transaction']) self._commit = api_callable.create_api_call( self.firestore_stub.Commit, settings=defaults['commit']) self._rollback = api_callable.create_api_call( self.firestore_stub.Rollback, settings=defaults['rollback']) self._run_query = api_callable.create_api_call( self.firestore_stub.RunQuery, settings=defaults['run_query']) self._write = api_callable.create_api_call(self.firestore_stub.Write, settings=defaults['write']) self._listen = api_callable.create_api_call( self.firestore_stub.Listen, settings=defaults['listen']) self._list_collection_ids = api_callable.create_api_call( self.firestore_stub.ListCollectionIds, settings=defaults['list_collection_ids'])
def test_page_streaming(self): # A mock grpc function that page streams a list of consecutive # integers, returning `page_size` integers with each call and using # the next integer to return as the page token, until `pages_to_stream` # pages have been returned. # pylint:disable=too-many-locals page_size = 3 pages_to_stream = 5 # pylint: disable=abstract-method, too-few-public-methods class PageStreamingRequest(object): def __init__(self, page_token=0): self.page_token = page_token class PageStreamingResponse(object): def __init__(self, nums=(), next_page_token=0): self.nums = nums self.next_page_token = next_page_token fake_grpc_func_descriptor = PageDescriptor('page_token', 'next_page_token', 'nums') def grpc_return_value(request, *dummy_args, **dummy_kwargs): start = int(request.page_token) if start > 0 and start < page_size * pages_to_stream: return PageStreamingResponse(nums=list( range(start, start + page_size)), next_page_token=start + page_size) elif start >= page_size * pages_to_stream: return PageStreamingResponse() else: return PageStreamingResponse(nums=list(range(page_size)), next_page_token=page_size) with mock.patch('grpc.UnaryUnaryMultiCallable') as mock_grpc: mock_grpc.side_effect = grpc_return_value settings = _CallSettings(page_descriptor=fake_grpc_func_descriptor, timeout=0) my_callable = api_callable.create_api_call(mock_grpc, settings=settings) self.assertEqual(list(my_callable(PageStreamingRequest())), list(range(page_size * pages_to_stream))) unflattened_option = CallOptions(page_token=INITIAL_PAGE) # Expect a list of pages_to_stream pages, each of size page_size, # plus one empty page expected = [ list(range(page_size * n, page_size * (n + 1))) for n in range(pages_to_stream) ] + [()] self.assertEqual( list(my_callable(PageStreamingRequest(), unflattened_option)), expected) pages_already_read = 2 explicit_page_token_option = CallOptions( page_token=str(page_size * pages_already_read)) # Expect a list of pages_to_stream pages, each of size page_size, # plus one empty page, minus the pages_already_read expected = [ list(range(page_size * n, page_size * (n + 1))) for n in range(pages_already_read, pages_to_stream) ] expected += [()] self.assertEqual( list( my_callable(PageStreamingRequest(), explicit_page_token_option)), expected)
def __init__(self, service_path=SERVICE_ADDRESS, port=DEFAULT_SERVICE_PORT, channel=None, metadata_transformer=None, ssl_creds=None, scopes=None, client_config=None, app_name='gax', app_version=_GAX_VERSION): """Constructor. Args: service_path (string): The domain name of the API remote host. port (int): The port on which to connect to the remote host. channel (:class:`grpc.Channel`): A ``Channel`` instance through which to make calls. ssl_creds (:class:`grpc.ChannelCredentials`): A ``ChannelCredentials`` instance for use with an SSL-enabled channel. client_config (dict): A dictionary for call options for each method. See :func:`google.gax.construct_settings` for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points. metadata_transformer (Callable[[], list]): A function that creates the metadata for requests. app_name (string): The codename of the calling service. app_version (string): The version of the calling service. Returns: A PublisherApi object. """ if scopes is None: scopes = self._ALL_SCOPES if client_config is None: client_config = {} goog_api_client = '{}/{} {} gax/{} python/{}'.format( app_name, app_version, self._CODE_GEN_NAME_VERSION, self._GAX_VERSION, platform.python_version()) metadata = [('x-goog-api-client', goog_api_client)] default_client_config = json.loads( pkg_resources.resource_string( __name__, 'publisher_client_config.json').decode()) defaults = api_callable.construct_settings( 'google.pubsub.v1.Publisher', default_client_config, client_config, config.STATUS_CODE_NAMES, kwargs={'metadata': metadata}, bundle_descriptors=self._BUNDLE_DESCRIPTORS, page_descriptors=self._PAGE_DESCRIPTORS) self.stub = config.create_stub( pubsub_pb2.PublisherStub, service_path, port, ssl_creds=ssl_creds, channel=channel, metadata_transformer=metadata_transformer, scopes=scopes) self._create_topic = api_callable.create_api_call( self.stub.CreateTopic, settings=defaults['create_topic']) self._publish = api_callable.create_api_call( self.stub.Publish, settings=defaults['publish']) self._get_topic = api_callable.create_api_call( self.stub.GetTopic, settings=defaults['get_topic']) self._list_topics = api_callable.create_api_call( self.stub.ListTopics, settings=defaults['list_topics']) self._list_topic_subscriptions = api_callable.create_api_call( self.stub.ListTopicSubscriptions, settings=defaults['list_topic_subscriptions']) self._delete_topic = api_callable.create_api_call( self.stub.DeleteTopic, settings=defaults['delete_topic'])
def test_bundling_page_streaming_error(self): settings = _CallSettings(page_descriptor=object(), bundle_descriptor=object(), bundler=object()) with self.assertRaises(ValueError): api_callable.create_api_call(lambda _req, _timeout: 42, settings)
def __init__(self, service_path=SERVICE_ADDRESS, port=DEFAULT_SERVICE_PORT, channel=None, credentials=None, ssl_credentials=None, scopes=None, client_config=None, app_name=None, app_version='', lib_name=None, lib_version='', metrics_headers=()): """Constructor. Args: service_path (string): The domain name of the API remote host. port (int): The port on which to connect to the remote host. channel (:class:`grpc.Channel`): A ``Channel`` instance through which to make calls. credentials (object): The authorization credentials to attach to requests. These credentials identify this application to the service. ssl_credentials (:class:`grpc.ChannelCredentials`): A ``ChannelCredentials`` instance for use with an SSL-enabled channel. scopes (list[string]): A list of OAuth2 scopes to attach to requests. client_config (dict): A dictionary for call options for each method. See :func:`google.gax.construct_settings` for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points. app_name (string): The name of the application calling the service. Recommended for analytics purposes. app_version (string): The version of the application calling the service. Recommended for analytics purposes. lib_name (string): The API library software used for calling the service. (Unless you are writing an API client itself, leave this as default.) lib_version (string): The API library software version used for calling the service. (Unless you are writing an API client itself, leave this as default.) metrics_headers (dict): A dictionary of values for tracking client library metrics. Ultimately serializes to a string (e.g. 'foo/1.2.3 bar/3.14.1'). This argument should be considered private. Returns: A SubscriberClient object. """ # Unless the calling application specifically requested # OAuth scopes, request everything. if scopes is None: scopes = self._ALL_SCOPES # Initialize an empty client config, if none is set. if client_config is None: client_config = {} # Initialize metrics_headers as an ordered dictionary # (cuts down on cardinality of the resulting string slightly). metrics_headers = collections.OrderedDict(metrics_headers) metrics_headers['gl-python'] = platform.python_version() # The library may or may not be set, depending on what is # calling this client. Newer client libraries set the library name # and version. if lib_name: metrics_headers[lib_name] = lib_version # Finally, track the GAPIC package version. metrics_headers['gapic'] = pkg_resources.get_distribution( 'gapic-google-cloud-pubsub-v1', ).version # Load the configuration defaults. default_client_config = json.loads( pkg_resources.resource_string( __name__, 'subscriber_client_config.json').decode()) defaults = api_callable.construct_settings( 'google.pubsub.v1.Subscriber', default_client_config, client_config, config.STATUS_CODE_NAMES, metrics_headers=metrics_headers, page_descriptors=self._PAGE_DESCRIPTORS, ) self.iam_policy_stub = config.create_stub( iam_policy_pb2.IAMPolicyStub, channel=channel, service_path=service_path, service_port=port, credentials=credentials, scopes=scopes, ssl_credentials=ssl_credentials) self.subscriber_stub = config.create_stub( pubsub_pb2.SubscriberStub, channel=channel, service_path=service_path, service_port=port, credentials=credentials, scopes=scopes, ssl_credentials=ssl_credentials) self._create_subscription = api_callable.create_api_call( self.subscriber_stub.CreateSubscription, settings=defaults['create_subscription']) self._get_subscription = api_callable.create_api_call( self.subscriber_stub.GetSubscription, settings=defaults['get_subscription']) self._update_subscription = api_callable.create_api_call( self.subscriber_stub.UpdateSubscription, settings=defaults['update_subscription']) self._list_subscriptions = api_callable.create_api_call( self.subscriber_stub.ListSubscriptions, settings=defaults['list_subscriptions']) self._delete_subscription = api_callable.create_api_call( self.subscriber_stub.DeleteSubscription, settings=defaults['delete_subscription']) self._modify_ack_deadline = api_callable.create_api_call( self.subscriber_stub.ModifyAckDeadline, settings=defaults['modify_ack_deadline']) self._acknowledge = api_callable.create_api_call( self.subscriber_stub.Acknowledge, settings=defaults['acknowledge']) self._pull = api_callable.create_api_call( self.subscriber_stub.Pull, settings=defaults['pull']) self._streaming_pull = api_callable.create_api_call( self.subscriber_stub.StreamingPull, settings=defaults['streaming_pull']) self._modify_push_config = api_callable.create_api_call( self.subscriber_stub.ModifyPushConfig, settings=defaults['modify_push_config']) self._list_snapshots = api_callable.create_api_call( self.subscriber_stub.ListSnapshots, settings=defaults['list_snapshots']) self._create_snapshot = api_callable.create_api_call( self.subscriber_stub.CreateSnapshot, settings=defaults['create_snapshot']) self._delete_snapshot = api_callable.create_api_call( self.subscriber_stub.DeleteSnapshot, settings=defaults['delete_snapshot']) self._seek = api_callable.create_api_call( self.subscriber_stub.Seek, settings=defaults['seek']) self._set_iam_policy = api_callable.create_api_call( self.iam_policy_stub.SetIamPolicy, settings=defaults['set_iam_policy']) self._get_iam_policy = api_callable.create_api_call( self.iam_policy_stub.GetIamPolicy, settings=defaults['get_iam_policy']) self._test_iam_permissions = api_callable.create_api_call( self.iam_policy_stub.TestIamPermissions, settings=defaults['test_iam_permissions'])