def reserve_ids(self,
                    project_id,
                    keys,
                    database_id=None,
                    retry=google.api_core.gapic_v1.method.DEFAULT,
                    timeout=google.api_core.gapic_v1.method.DEFAULT,
                    metadata=None):
        """
        Prevents the supplied keys' IDs from being auto-allocated by Cloud
        Datastore.

        Example:
            >>> from google.cloud import datastore_v1
            >>>
            >>> client = datastore_v1.DatastoreClient()
            >>>
            >>> project_id = ''
            >>> keys = []
            >>>
            >>> response = client.reserve_ids(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]]): A list of keys with complete key paths whose numeric IDs should not be
                auto-allocated.
                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.datastore_v1.types.Key`
            database_id (str): If not empty, the ID of the database against which to make the request.
            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.ReserveIdsResponse` 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.ReserveIdsRequest(
            project_id=project_id,
            keys=keys,
            database_id=database_id,
        )
        return self._reserve_ids(
            request, retry=retry, timeout=timeout, metadata=metadata)
Example #2
0
    def test_reserve_ids(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = datastore_pb2.ReserveIdsResponse(
            **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.reserve_ids(project_id, keys)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = datastore_pb2.ReserveIdsRequest(
            project_id=project_id, keys=keys)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def reserve_ids(
        self,
        project_id,
        keys,
        database_id=None,
        retry=google.api_core.gapic_v1.method.DEFAULT,
        timeout=google.api_core.gapic_v1.method.DEFAULT,
        metadata=None,
    ):
        """
        Prevents the supplied keys' IDs from being auto-allocated by Cloud
        Datastore.

        Example:
            >>> from google.cloud import datastore_v1
            >>>
            >>> client = datastore_v1.DatastoreClient()
            >>>
            >>> # TODO: Initialize `project_id`:
            >>> project_id = ''
            >>>
            >>> # TODO: Initialize `keys`:
            >>> keys = []
            >>>
            >>> response = client.reserve_ids(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]]): A list of keys with complete key paths whose numeric IDs should not be
                auto-allocated.

                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.datastore_v1.types.Key`
            database_id (str): If not empty, the ID of the database against which to make the request.
            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.ReserveIdsResponse` 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 "reserve_ids" not in self._inner_api_calls:
            self._inner_api_calls[
                "reserve_ids"
            ] = google.api_core.gapic_v1.method.wrap_method(
                self.transport.reserve_ids,
                default_retry=self._method_configs["ReserveIds"].retry,
                default_timeout=self._method_configs["ReserveIds"].timeout,
                client_info=self._client_info,
            )

        request = datastore_pb2.ReserveIdsRequest(
            project_id=project_id, keys=keys, database_id=database_id
        )
        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["reserve_ids"](
            request, retry=retry, timeout=timeout, metadata=metadata
        )