示例#1
0
    def delete_state(
        self,
        store_name: str,
        key: str,
        etag: Optional[str] = None,
        options: Optional[StateOptions] = None,
        state_metadata: Optional[Dict[str, str]] = dict(),
        metadata: Optional[MetadataTuple] = ()
    ) -> DaprResponse:
        """Deletes key-value pairs from a statestore

        This deletes a value from the statestore with a given key and state store name.
        Options for request can be passed with the options field and custom
        metadata can be passed with metadata field.

        The example deletes states from a statestore:
            from dapr import DaprClient
            with DaprClient() as d:
                resp = d.delete_state(
                    store_name='state_store',
                    key='key1',
                    etag='etag',
                    state_metadata={"header1": "value1"},
                    metadata=(
                        ('header1', 'value1')
                    )
                )

        Args:
            store_name (str): the state store name to delete from
            key (str): the key of the key-value pair to delete
            etag (str, optional): the etag to delete with
            options (StateOptions, optional): custom options
                for concurrency and consistency
            state_metadata (Dict[str, str], optional): custom metadata for state request
            metadata (tuple, optional): custom metadata

        Returns:
            :class:`DaprResponse` gRPC metadata returned from callee
        """

        if not store_name or len(store_name) == 0 or len(
                store_name.strip()) == 0:
            raise ValueError("State store name cannot be empty")

        if options is None:
            state_options = None
        else:
            state_options = options.get_proto()

        etag_object = common_v1.Etag(value=etag) if etag is not None else None
        req = api_v1.DeleteStateRequest(store_name=store_name,
                                        key=key,
                                        etag=etag_object,
                                        options=state_options,
                                        metadata=state_metadata)
        _, call = self._stub.DeleteState.with_call(req, metadata=metadata)
        return DaprResponse(headers=call.initial_metadata())
示例#2
0
import os

from dapr.proto import api_v1, api_service_v1, common_v1
from google.protobuf.any_pb2 import Any

# Get port from environment variable.
port = os.getenv('DAPR_GRPC_PORT', '50001')
daprUri = 'localhost:' + port
channel = grpc.insecure_channel(daprUri)

client = api_service_v1.DaprStub(channel)
client.PublishEvent(api_v1.PublishEventRequest(topic='sith', data='lala'.encode('utf-8')))
print('Published!')

key = 'mykey'
storeName = 'statestore'
req = common_v1.StateItem(key=key, value='my state'.encode('utf-8'))
state = api_v1.SaveStateRequest(store_name=storeName, states=[req])

client.SaveState(state)
print('Saved!')

resp = client.GetState(api_v1.GetStateRequest(store_name=storeName, key=key))
print('Got!')
print(resp)

resp = client.DeleteState(api_v1.DeleteStateRequest(store_name=storeName, key=key))
print('Deleted!')

channel.close()