Quickstart: State Management
This quickstart showcases how to enable stateful data persistence using the State API.
Prerequisites
Before you proceed, ensure you have the following prereqs installed.
- Python
- .NET
- JavaScript
- Java
Log in to Catalyst
Authenticate to Diagrid Catalyst using the following command:
diagrid login
Confirm your organization and user details are correct:
diagrid whoami
Clone quickstart code
Clone the quickstart code from the Diagrid Labs GitHub repository and navigate to the appropriate directory:
- Python
- .NET
- JavaScript
- Java
diagrid dev quickstart --type state --language python
diagrid dev quickstart --type state --language csharp
diagrid dev quickstart --type state --language javascript
diagrid dev quickstart --type state --language java
After the command executes successfully, the CLI will output the contents of your newly created project including an App ID called order-app
and a Diagrid KV Store called kvstore
.
Run quickstart application
To run the quickstart locally, use the diagrid dev run
command. This command uses the dapr multi-app run file in the root of the code directory to:
- Provision a Catalyst project: If the project does not exist, the command will create a new project
- Create resources: Creates the necessary resources on Catalyst such as App IDs, Components, and Diagrid Pub/Sub or KV Store managed services
- Launch the quickstart applications: Locates the code directories using the values specified for
workDir
and launches each application using the app command inputs - Inject environment variables: Configures the Dapr client to talk to Catalyst using the
order-app
App ID API token
For additional details on the Catalyst local development experience, read Develop Locally with Catalyst APIs.
- Python
- .NET
- JavaScript
- Java
Install python dependencies in a virtual environment.
This quickstart uses the venv module to create the virtual environment. Please feel free to adapt if you prefer to use conda, pipenv, or another alternative.
python -m venv venv
- MacOS
- Linux
- Windows
If you are using MacOS, start the virtual environment with the following command:
source venv/bin/activate
Don't forget to close your virtual environment with deactivate
when done with the quickstart
If you are using Linux, start the virtual environment with the following command:
source venv/bin/activate
Don't forget to close your virtual environment with deactivate
when done with the quickstart
If you are using PowerShell on Windows, start the virtual environment with the following command:
venv/Scripts/Activate.ps1
If you are using the Command Prompt on Windows, start the virtual environment with the following command:
venv/Scripts/activate.bat
If running in the Windows Command Prompt, don't forget to close your virtual environment with venv/Scripts/deactivate.bat
when done with the quickstart
Install the python app requirements:
pip install -r requirements.txt
Install .NET dependencies.
dotnet restore
Install node dependencies.
npm install
Install maven dependencies.
mvn clean install
Run the application using the following command:
- Python
- .NET
- JavaScript
- Java
diagrid dev run -f dev-python-state.yaml --project dev-python-state
diagrid dev run -f dev-csharp-state.yaml --project dev-csharp-state
diagrid dev run -f dev-js-state.yaml --project dev-js-state
diagrid dev run -f dev-java-state.yaml --project dev-java-state
Call the State API
With the quickstart application running, use the curl commands below or take advantage of the test.rest
file in the root folder of the repo powered by the VS Code REST Client extension to trigger requests from the app to the Dapr State API hosted by Catalyst.
Store state
Open a new terminal and execute the following curl command to save a state item to the Diagrid KV store:
curl -i -X POST http://localhost:5001/order -H "Content-Type: application/json" -d '{"orderId":1}'
Retrieve state
Confirm the state item was saved successfully by retrieving the state:
curl -i -X GET http://localhost:5001/order/1
You can also view the stored item using the Diagrid KV Store data explorer:
Delete state
Delete the state item using the below command:
curl -i -X DELETE http://localhost:5001/order/1
After running the above commands, the local app logs should indicate an order was saved, retrieved and deleted. You can confirm this by viewing the Catalyst API logs.
To stop the application use CTL-C
or run the following:
diagrid dev stop
Clean up resources
If you are not going to continue to use this application, you can delete the resources using the following commands:
diagrid appid delete order-app
If you want to delete the entire Catalyst project, including the managed infrastructure resources, run the diagrid project delete
command.