def save_state( self, store_name: str, key: str, value: Union[bytes, str], etag: Optional[str] = None, options: Optional[StateOptions] = None, metadata: Optional[MetadataTuple] = () ) -> DaprResponse: """Saves key-value pairs to a statestore This saves a value to 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 saves states to a statestore: from dapr import DaprClient with DaprClient() as d: resp = d.save_state( store_name='state_store' states=[{'key': 'key1', 'value': 'value1'}], etag='etag', metadata=( ('header1', 'value1') ), ) Args: store_name (str): the state store name to save to key (str): the key to be saved value (bytes or str): the value to be saved etag (str, optional): the etag to save with options (StateOptions, optional): custom options for concurrency and consistency metadata (tuple, optional): custom metadata Returns: :class:`DaprResponse` gRPC metadata returned from callee """ if not isinstance(value, (bytes, str)): raise ValueError(f'invalid type for data {type(value)}') req_value = value if 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() state = common_v1.StateItem(key=key, value=to_bytes(req_value), etag=etag, options=state_options) req = api_v1.SaveStateRequest(store_name=store_name, states=[state]) response, call = self._stub.SaveState.with_call(req, metadata=metadata) return DaprResponse(headers=call.initial_metadata())
def save_bulk_state( self, store_name: str, states: List[StateItem], metadata: Optional[MetadataTuple] = () ) -> DaprResponse: """Saves state items to a statestore This saves a given state item into the statestore specified by store_name. The example saves states to a statestore: from dapr import DaprClient with DaprClient() as d: resp = d.save_bulk_state( store_name='state_store', states=[StateItem(key='key1', value='value1'), StateItem(key='key2', value='value2', etag='etag'),], ) Args: store_name (str): the state store name to save to states (List[StateItem]): list of states to save metadata (tuple, optional): gRPC custom metadata Returns: :class:`DaprResponse` gRPC metadata returned from callee Raises: ValueError: states is empty ValueError: store_name is empty """ if metadata is not None: warn( 'metadata argument is deprecated. Dapr already intercepts API token headers ' 'and this is not needed.', DeprecationWarning, stacklevel=2) if not states or len(states) == 0: raise ValueError("States to be saved cannot be empty") if not store_name or len(store_name) == 0 or len( store_name.strip()) == 0: raise ValueError("State store name cannot be empty") req_states = [ common_v1.StateItem( key=i.key, value=to_bytes(i.value), etag=common_v1.Etag( value=i.etag) if i.etag is not None else None, options=i.options, metadata=i.metadata) for i in states ] req = api_v1.SaveStateRequest(store_name=store_name, states=req_states) _, call = self._stub.SaveState.with_call(req, metadata=metadata) return DaprResponse(headers=call.initial_metadata())
def save_bulk_state( self, store_name: str, states: List[StateItem], metadata: Optional[MetadataTuple] = () ) -> DaprResponse: """Saves state items to a statestore This saves a given state item into the statestore specified by store_name. The example saves states to a statestore: from dapr import DaprClient with DaprClient() as d: resp = d.save_bulk_state( store_name='state_store' states=[StateItem(key='key1', value='value1'), StateItem(key='key2', value='value2', etag='etag'),], metadata=( ('header1', 'value1') ), ) Args: store_name (str): the state store name to save to states (List[StateItem]): list of states to save metadata (tuple, optional): custom metadata Returns: :class:`DaprResponse` gRPC metadata returned from callee Raises: ValueError: states is empty ValueError: store_name is empty """ if not states or len(states) == 0: raise ValueError("States to be saved cannot be empty") if not store_name or len(store_name) == 0 or len( store_name.strip()) == 0: raise ValueError("State store name cannot be empty") req_states = [ common_v1.StateItem(key=i.key, value=to_bytes(i.value), etag=i.etag, options=i.options, metadata=i.metadata) for i in states ] req = api_v1.SaveStateRequest(store_name=store_name, states=req_states) _, call = self._stub.SaveState.with_call(req, metadata=metadata) return DaprResponse(headers=call.initial_metadata())
def save_state( self, store_name: str, key: str, value: Union[bytes, str], etag: Optional[str] = None, options: Optional[StateOptions] = None, state_metadata: Optional[Dict[str, str]] = dict(), metadata: Optional[MetadataTuple] = () ) -> DaprResponse: """Saves key-value pairs to a statestore This saves a value to 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 saves states to a statestore: from dapr import DaprClient with DaprClient() as d: resp = d.save_state( store_name='state_store', key='key1', value='value1', etag='etag', state_metadata={"metakey": "metavalue"}, ) Args: store_name (str): the state store name to save to key (str): the key to be saved value (bytes or str): the value to be saved etag (str, optional): the etag to save with options (StateOptions, optional): custom options for concurrency and consistency state_metadata (Dict[str, str], optional): Dapr metadata for state request metadata (tuple, optional, DEPRECATED): gRPC custom metadata Returns: :class:`DaprResponse` gRPC metadata returned from callee Raises: ValueError: value is not bytes or str ValueError: store_name is empty """ if metadata is not None: warn( 'metadata argument is deprecated. Dapr already intercepts API token headers ' 'and this is not needed.', DeprecationWarning, stacklevel=2) if not isinstance(value, (bytes, str)): raise ValueError(f'invalid type for data {type(value)}') req_value = value 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() state = common_v1.StateItem( key=key, value=to_bytes(req_value), etag=common_v1.Etag(value=etag) if etag is not None else None, options=state_options, metadata=state_metadata) req = api_v1.SaveStateRequest(store_name=store_name, states=[state]) _, call = self._stub.SaveState.with_call(req, metadata=metadata) return DaprResponse(headers=call.initial_metadata())
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()