Client

The Dapr client package allows you to interact with other Dapr applications from a Rust application.

Note

The Dapr Rust-SDK is currently in Alpha. Work is underway to bring it to a stable release and will likely involve breaking changes.

Prerequisites

Import the client package

Add Dapr to your cargo.toml

  1. [dependencies]
  2. # Other dependencies
  3. dapr = "0.13.0"

You can either reference dapr::Client or bind the full path to a new name as follows:

  1. use dapr::Client as DaprClient

Instantiating the Dapr client

  1. const addr: String = "https://127.0.0.1";
  2. const port: String = "50001";
  3. let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr,
  4. port).await?;

Building blocks

The Rust SDK allows you to interface with the Dapr building blocks.

Service Invocation

To invoke a specific method on another service running with Dapr sidecar, the Dapr client Go SDK provides two options:

Invoke a service

  1. let response = client
  2. .invoke_service("service-to-invoke", "method-to-invoke", Some(data))
  3. .await
  4. .unwrap();

For a full guide on service invocation, visit How-To: Invoke a service.

State Management

The Dapr Client provides access to these state management methods: save_state, get_state, delete_state that can be used like so:

  1. let store_name = "store-name";
  2. let state_key = "state-key";
  3. let states = vec![(state_key, ("state-value").as_bytes().to_vec())];
  4. // save state with the key "state-key" and value "state-value"
  5. client.save_state(store_name, states).await?;
  6. // get state for key "state-key"
  7. let response = client.get_state(store_name, state_key, None).await.unwrap();
  8. // delete state for key "state-key"
  9. client.delete_state(store_name, state_key, None).await?;

Note: The save_state method currently performs a ‘bulk’ save but this will be refactored

For a full guide on state management, visit How-To: Save & get state.

Publish Messages

To publish data onto a topic, the Dapr Go client provides a simple method:

  1. let pubsub_name = "pubsub-name".to_string();
  2. let pubsub_topic = "topic-name".to_string();
  3. let pubsub_content_type = "text/plain".to_string();
  4. let data = "content".to_string().into_bytes();
  5. client
  6. .publish_event(pubsub_name, pubsub_topic, pubsub_content_type, data, None)
  7. .await?;

For a full guide on pub/sub, visit How-To: Publish & subscribe.

Rust SDK Examples