Skip to content

Commit

Permalink
feat(onboarding): rust sdk snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
Tymek committed Sep 24, 2024
1 parent bfe3af6 commit 5166ce8
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions frontend/src/component/onboarding/snippets/rust.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,52 @@
1\. Install the SDK
```sh
cargo add unleash-client
cargo add unleash-api-client --features async-std,reqwest,surf
cargo add serde --features derive
cargo add serde reqwest --features json
cargo add serde tokio --features full
cargo add serde anyhow cfg cfg-if enum-map@~2.0.0 surf
```

2\. Initialize Unleash
```rust
let client = client::ClientBuilder::default()
.interval(500)
.into_client::<UserFeatures, reqwest::Client>(
"<YOUR_API_URL>",
"unleash-onboarding-rust",
"unleash-onboarding-instance",
"<YOUR_API_TOKEN>",
)?;
client.register().await?;
use enum_map::Enum;
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::time::Duration;
use tokio::time::sleep;
use unleash_api_client::client::ClientBuilder;
use unleash_api_client::Client;

#[derive(Debug, Deserialize, Serialize, Enum, Clone)]
enum Flags {
#[serde(rename = "<YOUR_FLAG>")]
TestFlag,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let client: Client<Flags, reqwest::Client> = ClientBuilder::default()
.interval(5000) // Polling & metrics interval - default 15000 (ms)
.into_client(
"<YOUR_API_URL>",
"unleash-onboarding-rust",
"unleash-onboarding-instance",
Some("<YOUR_API_TOKEN>".to_owned()),
)?;
client.register().await?;

let (_, _) = tokio::join!(client.poll_for_updates(), async {
sleep(Duration::from_millis(1000)).await;

let is_enabled = client.is_enabled(Flags::TestFlag, None, true);
println!("\nIs flag enabled: {}\n", is_enabled);

sleep(Duration::from_millis(5000)).await;

client.stop_poll().await;
Ok::<(), Box<dyn Error + Send + Sync>>(())
});

Ok(())
}
```

0 comments on commit 5166ce8

Please sign in to comment.