Client

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

Prerequisites

Import the client package

Add Dapr to your cargo.toml

[dependencies]
# Other dependencies
dapr = "0.13.0"

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

use dapr::Client as DaprClient

Instantiating the Dapr client

const addr: String = "https://127.0.0.1";
const port: String = "50001";

let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr,
    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

let response = client
    .invoke_service("service-to-invoke", "method-to-invoke", Some(data))
    .await
    .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:

let store_name = "store-name";
let state_key = "state-key";

let states = vec![(state_key, ("state-value").as_bytes().to_vec())];

// save state with the key "state-key" and value "state-value"
client.save_state(store_name, states).await?;

// get state for key "state-key"
let response = client.get_state(store_name, state_key, None).await.unwrap();

// delete state for key "state-key"
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:

let pubsub_name = "pubsub-name".to_string();
let pubsub_topic = "topic-name".to_string();
let pubsub_content_type = "text/plain".to_string();

let data = "content".to_string().into_bytes();
client
    .publish_event(pubsub_name, pubsub_topic, pubsub_content_type, data, None)
    .await?;

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

Rust SDK Examples