Skip to content

Latest commit

 

History

History

state_store

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Example - Save and get state

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

Pre-requisites

Install Dapr python-SDK

pip3 install dapr dapr-ext-grpc

Run the example

To run this example, the following code can be utilized:

dapr run -- python3 state_store.py

The output should be as follows:

== APP == State store has successfully saved value_1 with key_1 as key

== APP == Cannot save due to bad etag. ErrorCode=StatusCode.ABORTED

== APP == State store has successfully saved value_2 with key_2 as key

== APP == State store has successfully saved value_3 with key_3 as key

== APP == Cannot save bulk due to bad etags. ErrorCode=StatusCode.ABORTED

== APP == Got value=b'value_1' eTag=1

== APP == Got items with etags: [(b'value_1_updated', '2'), (b'value_2', '2')]

== APP == Got values after transaction delete: [b'', b'']

== APP == Got value after delete: b''

Error Handling

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