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, request_pb, _datastore_pb2.LookupResponse, )
def _datastore_lookup(keys, read_options, retries=None, timeout=None): """Issue a Lookup call to Datastore using gRPC. Args: keys (Iterable[entity_pb2.Key]): The entity keys to look up. read_options (Union[datastore_pb2.ReadOptions, NoneType]): Options for the request. retries (int): Number of times to potentially retry the call. If :data:`None` is passed, will use :data:`_retry._DEFAULT_RETRIES`. If :data:`0` is passed, the call is attempted only once. timeout (float): Timeout, in seconds, to pass to gRPC call. If :data:`None` is passed, will use :data:`_DEFAULT_TIMEOUT`. Returns: tasklets.Future: Future object for eventual result of lookup. """ client = context_module.get_context().client request = datastore_pb2.LookupRequest( project_id=client.project, keys=[key for key in keys], read_options=read_options, ) return make_call("Lookup", request, retries=retries, timeout=timeout)
def test_lookup_multiple_keys_w_deferred(self): from google.cloud.datastore_v1.proto import datastore_pb2 project = 'PROJECT' key_pb1 = _make_key_pb(project) key_pb2 = _make_key_pb(project, id_=2345) rsp_pb = datastore_pb2.LookupResponse() rsp_pb.deferred.add().CopyFrom(key_pb1) rsp_pb.deferred.add().CopyFrom(key_pb2) read_options = datastore_pb2.ReadOptions() # Create mock HTTP and client with response. http = _make_requests_session( [_make_response(content=rsp_pb.SerializeToString())]) client = mock.Mock( _http=http, _base_url='test.invalid', spec=['_http', '_base_url']) # 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) uri = _build_expected_url(client._base_url, project, 'lookup') self.assertEqual(len(response.found), 0) self.assertEqual(len(response.missing), 0) self.assertEqual(list(response.deferred), [key_pb1, key_pb2]) request = _verify_protobuf_call( http, uri, datastore_pb2.LookupRequest()) self.assertEqual(list(request.keys), [key_pb1, key_pb2]) self.assertEqual(request.read_options, read_options)
def test_lookup_multiple_keys_empty_response(self): from google.cloud.datastore_v1.proto import datastore_pb2 project = "PROJECT" key_pb1 = _make_key_pb(project) key_pb2 = _make_key_pb(project, id_=2345) rsp_pb = datastore_pb2.LookupResponse() read_options = datastore_pb2.ReadOptions() # Create mock HTTP and client with response. http = _make_requests_session( [_make_response(content=rsp_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) 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_pb1, key_pb2]) self.assertEqual(request.read_options, read_options)
def lookup(self, project_id, keys, read_options=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None): """ Looks up entities by key. Example: >>> from google.cloud import datastore_v1 >>> >>> client = datastore_v1.DatastoreClient() >>> >>> project_id = '' >>> keys = [] >>> >>> response = client.lookup(project_id, keys) Args: project_id (str): The ID of the project against which to make the request. keys (list[Union[dict, ~google.cloud.datastore_v1.types.Key]]): Keys of entities to look up. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.datastore_v1.types.Key` read_options (Union[dict, ~google.cloud.datastore_v1.types.ReadOptions]): The options for this lookup request. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.datastore_v1.types.ReadOptions` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.datastore_v1.types.LookupResponse` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ if metadata is None: metadata = [] metadata = list(metadata) request = datastore_pb2.LookupRequest( project_id=project_id, keys=keys, read_options=read_options, ) return self._lookup( request, retry=retry, timeout=timeout, metadata=metadata)
def test_connect(self): # when zipped grpc needs to load a resource that it tries to get from disk # that resource must be unzipped along with the native code library credentials = grpc.ssl_channel_credentials() channel = grpc.secure_channel('datastore.googleapis.com', credentials) datastore_stub = datastore_pb2_grpc.DatastoreStub(channel) request = datastore_pb2.LookupRequest() with self.assertRaisesRegexp(grpc.RpcError, 'missing required authentication') as context: datastore_stub.Lookup(request) self.assertEqual(context.exception.code(), grpc.StatusCode.UNAUTHENTICATED)
def _datastore_lookup(keys): """Issue a Lookup call to Datastore using gRPC. Args: keys (Iterable[datastore_v1.proto.entity_pb2.Key]): The entity keys to look up. Returns: :class:`grpc.Future`: Future object for eventual result of lookup. """ client = _runstate.current().client request = datastore_pb2.LookupRequest( project_id=client.project, keys=[key for key in keys] ) api = stub() return api.Lookup.future(request)
def test_lookup_single_key_nonempty_response(self): from google.cloud.datastore_v1.proto import datastore_pb2 from google.cloud.datastore_v1.proto import entity_pb2 project = "PROJECT" key_pb = _make_key_pb(project) rsp_pb = datastore_pb2.LookupResponse() entity = entity_pb2.Entity() entity.key.CopyFrom(key_pb) rsp_pb.found.add(entity=entity) read_options = datastore_pb2.ReadOptions() # Create mock HTTP and client with response. http = _make_requests_session( [_make_response(content=rsp_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) 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]) self.assertEqual(request.read_options, read_options)
def _datastore_lookup(keys, read_options): """Issue a Lookup call to Datastore using gRPC. Args: keys (Iterable[entity_pb2.Key]): The entity keys to look up. read_options (Union[datastore_pb2.ReadOptions, NoneType]): Options for the request. Returns: :class:`grpc.Future`: Future object for eventual result of lookup. """ client = _runstate.current().client request = datastore_pb2.LookupRequest( project_id=client.project, keys=[key for key in keys], read_options=read_options, ) api = stub() return api.Lookup.future(request)
def test_lookup(self): # Setup Expected Response expected_response = {} expected_response = datastore_pb2.LookupResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = datastore_v1.DatastoreClient(channel=channel) # Setup Request project_id = 'projectId-1969970175' keys = [] response = client.lookup(project_id, keys) assert expected_response == response assert len(channel.requests) == 1 expected_request = datastore_pb2.LookupRequest(project_id=project_id, keys=keys) actual_request = channel.requests[0][1] assert expected_request == actual_request
def lookup( self, project_id, keys, read_options=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Looks up entities by key. Example: >>> from google.cloud import datastore_v1 >>> >>> client = datastore_v1.DatastoreClient() >>> >>> # TODO: Initialize `project_id`: >>> project_id = '' >>> >>> # TODO: Initialize `keys`: >>> keys = [] >>> >>> response = client.lookup(project_id, keys) Args: project_id (str): The ID of the project against which to make the request. keys (list[Union[dict, ~google.cloud.datastore_v1.types.Key]]): Keys of entities to look up. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.datastore_v1.types.Key` read_options (Union[dict, ~google.cloud.datastore_v1.types.ReadOptions]): The options for this lookup request. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.datastore_v1.types.ReadOptions` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.datastore_v1.types.LookupResponse` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "lookup" not in self._inner_api_calls: self._inner_api_calls[ "lookup" ] = google.api_core.gapic_v1.method.wrap_method( self.transport.lookup, default_retry=self._method_configs["Lookup"].retry, default_timeout=self._method_configs["Lookup"].timeout, client_info=self._client_info, ) request = datastore_pb2.LookupRequest( project_id=project_id, keys=keys, read_options=read_options ) if metadata is None: metadata = [] metadata = list(metadata) try: routing_header = [("project_id", project_id)] except AttributeError: pass else: routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( routing_header ) metadata.append(routing_metadata) return self._inner_api_calls["lookup"]( request, retry=retry, timeout=timeout, metadata=metadata )