def test_lookup_single_key_empty_response_w_transaction(self): from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" transaction = b"TRANSACTION" key_pb = _make_key_pb(project) rsp_pb = datastore_pb2.LookupResponse() read_options = datastore_pb2.ReadOptions(transaction=transaction) # Create mock HTTP and client with response. http = _make_requests_session( [_make_response(content=rsp_pb._pb.SerializeToString())]) client_info = _make_client_info() client = mock.Mock( _http=http, _base_url="test.invalid", _client_info=client_info, spec=["_http", "_base_url", "_client_info"], ) # Make request. ds_api = self._make_one(client) response = ds_api.lookup(project, [key_pb], read_options=read_options) # Check the result and verify the callers. self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "lookup") self.assertEqual(len(response.found), 0) self.assertEqual(len(response.missing), 0) self.assertEqual(len(response.deferred), 0) request = _verify_protobuf_call(http, uri, datastore_pb2.LookupRequest()) self.assertEqual(list(request.keys), [key_pb._pb]) self.assertEqual(request.read_options, read_options._pb)
def lookup(self, project_id, keys, read_options=None): """Perform a ``lookup`` request. :type project_id: str :param project_id: The project to connect to. This is usually your project name in the cloud console. :type keys: List[.entity_pb2.Key] :param keys: The keys to retrieve from the datastore. :type read_options: :class:`.datastore_pb2.ReadOptions` :param read_options: (Optional) The options for this lookup. Contains either the transaction for the read or ``STRONG`` or ``EVENTUAL`` read consistency. :rtype: :class:`.datastore_pb2.LookupResponse` :returns: The returned protobuf response object. """ request_pb = _datastore_pb2.LookupRequest( project_id=project_id, read_options=read_options, keys=keys ) return _rpc( self.client._http, project_id, "lookup", self.client._base_url, self.client._client_info, request_pb, _datastore_pb2.LookupResponse, )
def test_lookup_multiple_keys_w_missing(self): from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" key_pb1 = _make_key_pb(project) key_pb2 = _make_key_pb(project, id_=2345) rsp_pb = datastore_pb2.LookupResponse() er_1 = rsp_pb._pb.missing.add() er_1.entity.key.CopyFrom(key_pb1._pb) er_2 = rsp_pb._pb.missing.add() er_2.entity.key.CopyFrom(key_pb2._pb) read_options = datastore_pb2.ReadOptions() # Create mock HTTP and client with response. http = _make_requests_session( [_make_response(content=rsp_pb._pb.SerializeToString())]) client_info = _make_client_info() client = mock.Mock( _http=http, _base_url="test.invalid", _client_info=client_info, spec=["_http", "_base_url", "_client_info"], ) # Make request. ds_api = self._make_one(client) response = ds_api.lookup(project, [key_pb1, key_pb2], read_options=read_options) # Check the result and verify the callers. self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "lookup") self.assertEqual(len(response.found), 0) self.assertEqual(len(response.deferred), 0) missing_keys = [result.entity.key for result in response.missing] self.assertEqual(missing_keys, [key_pb1._pb, key_pb2._pb]) request = _verify_protobuf_call(http, uri, datastore_pb2.LookupRequest()) self.assertEqual(list(request.keys), [key_pb1._pb, key_pb2._pb]) self.assertEqual(request.read_options, read_options._pb)
def test_lookup_single_key_nonempty_response(self): from google.cloud.datastore_v1.types import datastore as datastore_pb2 from google.cloud.datastore_v1.types import entity as entity_pb2 project = "PROJECT" key_pb = _make_key_pb(project) rsp_pb = datastore_pb2.LookupResponse() entity = entity_pb2.Entity() entity.key._pb.CopyFrom(key_pb._pb) rsp_pb._pb.found.add(entity=entity._pb) read_options = datastore_pb2.ReadOptions() # Create mock HTTP and client with response. http = _make_requests_session( [_make_response(content=rsp_pb._pb.SerializeToString())]) client_info = _make_client_info() client = mock.Mock( _http=http, _base_url="test.invalid", _client_info=client_info, spec=["_http", "_base_url", "_client_info"], ) # Make request. ds_api = self._make_one(client) response = ds_api.lookup(project, [key_pb], read_options=read_options) # Check the result and verify the callers. self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "lookup") self.assertEqual(len(response.found), 1) self.assertEqual(len(response.missing), 0) self.assertEqual(len(response.deferred), 0) found = response.found[0].entity self.assertEqual(found.key.path[0].kind, "Kind") self.assertEqual(found.key.path[0].id, 1234) request = _verify_protobuf_call(http, uri, datastore_pb2.LookupRequest()) self.assertEqual(list(request.keys), [key_pb._pb]) self.assertEqual(request.read_options, read_options._pb)
async def lookup( self, request: datastore.LookupRequest = None, *, project_id: str = None, read_options: datastore.ReadOptions = None, keys: Sequence[entity.Key] = None, retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), ) -> datastore.LookupResponse: r"""Looks up entities by key. Args: request (:class:`~.datastore.LookupRequest`): The request object. The request for [Datastore.Lookup][google.datastore.v1.Datastore.Lookup]. project_id (:class:`str`): Required. The ID of the project against which to make the request. This corresponds to the ``project_id`` field on the ``request`` instance; if ``request`` is provided, this should not be set. read_options (:class:`~.datastore.ReadOptions`): The options for this lookup request. This corresponds to the ``read_options`` field on the ``request`` instance; if ``request`` is provided, this should not be set. keys (:class:`Sequence[~.entity.Key]`): Required. Keys of entities to look up. This corresponds to the ``keys`` field on the ``request`` instance; if ``request`` is provided, this should not be set. retry (google.api_core.retry.Retry): Designation of what errors, if any, should be retried. timeout (float): The timeout for this request. metadata (Sequence[Tuple[str, str]]): Strings which should be sent along with the request as metadata. Returns: ~.datastore.LookupResponse: The response for [Datastore.Lookup][google.datastore.v1.Datastore.Lookup]. """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. has_flattened_params = any([project_id, read_options, keys]) if request is not None and has_flattened_params: raise ValueError("If the `request` argument is set, then none of " "the individual field arguments should be set.") request = datastore.LookupRequest(request) # If we have keyword arguments corresponding to fields on the # request, apply these. if project_id is not None: request.project_id = project_id if read_options is not None: request.read_options = read_options if keys: request.keys.extend(keys) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. rpc = gapic_v1.method_async.wrap_method( self._client._transport.lookup, default_retry=retries.Retry( initial=0.1, maximum=60.0, multiplier=1.3, predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), ), default_timeout=60.0, client_info=DEFAULT_CLIENT_INFO, ) # Send the request. response = await rpc( request, retry=retry, timeout=timeout, metadata=metadata, ) # Done; return the response. return response
def _lookup_multiple_helper( self, found=0, missing=0, deferred=0, retry=None, timeout=None, ): from google.cloud.datastore_v1.types import datastore as datastore_pb2 from google.cloud.datastore_v1.types import entity as entity_pb2 project = "PROJECT" key_pb1 = _make_key_pb(project) key_pb2 = _make_key_pb(project, id_=2345) keys = [key_pb1, key_pb2] read_options = datastore_pb2.ReadOptions() rsp_pb = datastore_pb2.LookupResponse() found_keys = [] for i_found in range(found): key = keys[i_found] found_keys.append(key._pb) entity = entity_pb2.Entity() entity.key._pb.CopyFrom(key._pb) rsp_pb._pb.found.add(entity=entity._pb) missing_keys = [] for i_missing in range(missing): key = keys[i_missing] missing_keys.append(key._pb) entity = entity_pb2.Entity() entity.key._pb.CopyFrom(key._pb) rsp_pb._pb.missing.add(entity=entity._pb) deferred_keys = [] for i_deferred in range(deferred): key = keys[i_deferred] deferred_keys.append(key._pb) rsp_pb._pb.deferred.append(key._pb) http = _make_requests_session( [_make_response(content=rsp_pb._pb.SerializeToString())]) client_info = _make_client_info() client = mock.Mock( _http=http, _base_url="test.invalid", _client_info=client_info, spec=["_http", "_base_url", "_client_info"], ) ds_api = self._make_one(client) request = { "project_id": project, "keys": keys, "read_options": read_options, } kwargs = _make_retry_timeout_kwargs(retry, timeout, http) response = ds_api.lookup(request=request, **kwargs) self.assertEqual(response, rsp_pb._pb) self.assertEqual([found.entity.key for found in response.found], found_keys) self.assertEqual([missing.entity.key for missing in response.missing], missing_keys) self.assertEqual(list(response.deferred), deferred_keys) uri = _build_expected_url(client._base_url, project, "lookup") request = _verify_protobuf_call( http, uri, datastore_pb2.LookupRequest(), retry=retry, timeout=timeout, ) self.assertEqual(list(request.keys), [key_pb1._pb, key_pb2._pb]) self.assertEqual(request.read_options, read_options._pb)
def _lookup_single_helper( self, read_consistency=None, transaction=None, empty=True, retry=None, timeout=None, ): from google.cloud.datastore_v1.types import datastore as datastore_pb2 from google.cloud.datastore_v1.types import entity as entity_pb2 project = "PROJECT" key_pb = _make_key_pb(project) options_kw = {} if read_consistency is not None: options_kw["read_consistency"] = read_consistency if transaction is not None: options_kw["transaction"] = transaction read_options = datastore_pb2.ReadOptions(**options_kw) rsp_pb = datastore_pb2.LookupResponse() if not empty: entity = entity_pb2.Entity() entity.key._pb.CopyFrom(key_pb._pb) rsp_pb._pb.found.add(entity=entity._pb) http = _make_requests_session( [_make_response(content=rsp_pb._pb.SerializeToString())]) client_info = _make_client_info() client = mock.Mock( _http=http, _base_url="test.invalid", _client_info=client_info, spec=["_http", "_base_url", "_client_info"], ) ds_api = self._make_one(client) request = { "project_id": project, "keys": [key_pb], "read_options": read_options, } kwargs = _make_retry_timeout_kwargs(retry, timeout, http) response = ds_api.lookup(request=request, **kwargs) self.assertEqual(response, rsp_pb._pb) if empty: self.assertEqual(len(response.found), 0) else: self.assertEqual(len(response.found), 1) self.assertEqual(len(response.missing), 0) self.assertEqual(len(response.deferred), 0) uri = _build_expected_url(client._base_url, project, "lookup") request = _verify_protobuf_call( http, uri, datastore_pb2.LookupRequest(), retry=retry, timeout=timeout, ) if retry is not None: retry.assert_called_once_with(http.request) self.assertEqual(list(request.keys), [key_pb._pb]) self.assertEqual(request.read_options, read_options._pb)
def lookup( self, request: datastore.LookupRequest = None, *, project_id: str = None, read_options: datastore.ReadOptions = None, keys: Sequence[entity.Key] = None, retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), ) -> datastore.LookupResponse: r"""Looks up entities by key. Args: request (:class:`~.datastore.LookupRequest`): The request object. The request for [Datastore.Lookup][google.datastore.v1.Datastore.Lookup]. project_id (:class:`str`): Required. The ID of the project against which to make the request. This corresponds to the ``project_id`` field on the ``request`` instance; if ``request`` is provided, this should not be set. read_options (:class:`~.datastore.ReadOptions`): The options for this lookup request. This corresponds to the ``read_options`` field on the ``request`` instance; if ``request`` is provided, this should not be set. keys (:class:`Sequence[~.entity.Key]`): Required. Keys of entities to look up. This corresponds to the ``keys`` field on the ``request`` instance; if ``request`` is provided, this should not be set. retry (google.api_core.retry.Retry): Designation of what errors, if any, should be retried. timeout (float): The timeout for this request. metadata (Sequence[Tuple[str, str]]): Strings which should be sent along with the request as metadata. Returns: ~.datastore.LookupResponse: The response for [Datastore.Lookup][google.datastore.v1.Datastore.Lookup]. """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. has_flattened_params = any([project_id, read_options, keys]) if request is not None and has_flattened_params: raise ValueError("If the `request` argument is set, then none of " "the individual field arguments should be set.") # Minor optimization to avoid making a copy if the user passes # in a datastore.LookupRequest. # There's no risk of modifying the input as we've already verified # there are no flattened fields. if not isinstance(request, datastore.LookupRequest): request = datastore.LookupRequest(request) # If we have keyword arguments corresponding to fields on the # request, apply these. if project_id is not None: request.project_id = project_id if read_options is not None: request.read_options = read_options if keys: request.keys.extend(keys) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. rpc = self._transport._wrapped_methods[self._transport.lookup] # Send the request. response = rpc( request, retry=retry, timeout=timeout, metadata=metadata, ) # Done; return the response. return response