def test_rollback_ok(self): from google.cloud.datastore_v1.proto import datastore_pb2 project = "PROJECT" transaction = b"xact" rsp_pb = datastore_pb2.RollbackResponse() # 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.rollback(project, transaction) # Check the result and verify the callers. self.assertEqual(response, rsp_pb) uri = _build_expected_url(client._base_url, project, "rollback") request = _verify_protobuf_call(http, uri, datastore_pb2.RollbackRequest()) self.assertEqual(request.transaction, transaction)
def rollback(self, project_id, transaction): """Perform a ``rollback`` request. :type project_id: str :param project_id: The project to connect to. This is usually your project name in the cloud console. :type transaction: bytes :param transaction: The transaction ID to rollback. :rtype: :class:`.datastore_pb2.RollbackResponse` :returns: The returned protobuf response object. """ request_pb = _datastore_pb2.RollbackRequest(project_id=project_id, transaction=transaction) # Response is empty (i.e. no fields) but we return it anyway. return _rpc( self.client._http, project_id, "rollback", self.client._base_url, self.client._client_info, request_pb, _datastore_pb2.RollbackResponse, )
def rollback(self, project_id, transaction, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None): """ Rolls back a transaction. Example: >>> from google.cloud import datastore_v1 >>> >>> client = datastore_v1.DatastoreClient() >>> >>> project_id = '' >>> transaction = b'' >>> >>> response = client.rollback(project_id, transaction) Args: project_id (str): The ID of the project against which to make the request. transaction (bytes): The transaction identifier, returned by a call to ``Datastore.BeginTransaction``. 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.RollbackResponse` 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.RollbackRequest( project_id=project_id, transaction=transaction, ) return self._rollback( request, retry=retry, timeout=timeout, metadata=metadata)
def _datastore_rollback(transaction, retries=None): """Calls Rollback in Datastore. Args: transaction (bytes): Transaction id. 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. Returns: tasklets.Tasklet: Future for :class:`google.cloud.datastore_v1.datastore_pb2.RollbackResponse` """ client = context_module.get_context().client request = datastore_pb2.RollbackRequest(project_id=client.project, transaction=transaction) return make_call("Rollback", request, retries=retries)
def test_rollback(self): # Setup Expected Response expected_response = {} expected_response = datastore_pb2.RollbackResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = datastore_v1.DatastoreClient(channel=channel) # Setup Request project_id = 'projectId-1969970175' transaction = b'-34' response = client.rollback(project_id, transaction) assert expected_response == response assert len(channel.requests) == 1 expected_request = datastore_pb2.RollbackRequest( project_id=project_id, transaction=transaction) actual_request = channel.requests[0][1] assert expected_request == actual_request
def rollback( self, project_id, transaction, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Rolls back a transaction. Example: >>> from google.cloud import datastore_v1 >>> >>> client = datastore_v1.DatastoreClient() >>> >>> # TODO: Initialize `project_id`: >>> project_id = '' >>> >>> # TODO: Initialize `transaction`: >>> transaction = b'' >>> >>> response = client.rollback(project_id, transaction) Args: project_id (str): The ID of the project against which to make the request. transaction (bytes): The transaction identifier, returned by a call to ``Datastore.BeginTransaction``. 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.RollbackResponse` 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 "rollback" not in self._inner_api_calls: self._inner_api_calls[ "rollback" ] = google.api_core.gapic_v1.method.wrap_method( self.transport.rollback, default_retry=self._method_configs["Rollback"].retry, default_timeout=self._method_configs["Rollback"].timeout, client_info=self._client_info, ) request = datastore_pb2.RollbackRequest( project_id=project_id, transaction=transaction ) 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["rollback"]( request, retry=retry, timeout=timeout, metadata=metadata )