This example demonstrates the Statestore APIs in Dapr. It demonstrates the following APIs:
- save state: Save single or mutiple states to statestore
- get state: Get a single state from statestore
- bulk get: Get multiple states(Bulk get) from statestore
- transaction: Execute a transaction on supported statestores
- delete state: Delete specified key from statestore
- etags: Use of etag and error handling for etag mismatches
It creates a client using DaprClient and calls all the state API methods available as example.
It uses the default configuration from Dapr init in self-hosted mode.
Note: Make sure to use the latest proto bindings
pip3 install dapr dapr-ext-grpcTo run this example, the following code can be utilized:
dapr run --resources-path components/ -- python3 state_store.pyThe output should be as follows:
State store has successfully saved value_1 with key_1 as key
Cannot save due to bad etag. ErrorCode=StatusCode.ABORTED
State store has successfully saved value_2 with key_2 as key
State store has successfully saved value_3 with key_3 as key
Cannot save bulk due to bad etags. ErrorCode=StatusCode.ABORTED
Got value=b'value_1' eTag=1
Got items with etags: [(b'value_1_updated', '2'), (b'value_2', '2')]
Transaction with outbox pattern executed successfully!
Got value after outbox pattern: b'val1'
Got values after transaction delete: [b'', b'']
Got value after delete: b''
The Dapr python-sdk will pass through errors that it receives from the Dapr runtime. In the case of an etag mismatch, the Dapr runtime will return StatusCode.ABORTED