mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-09 00:18:00 +01:00
feat(onboarding): Rust SDK snippet (#8239)
A bit different then other examples - only way to run current state of Rust SDK is to have some kind of an opinionated setup around it.
This commit is contained in:
parent
a95c8d183f
commit
cdb1297246
@ -1,17 +1,52 @@
|
|||||||
1\. Install the SDK
|
1\. Install the SDK
|
||||||
```sh
|
```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
|
2\. Initialize Unleash
|
||||||
```rust
|
```rust
|
||||||
let client = client::ClientBuilder::default()
|
use enum_map::Enum;
|
||||||
.interval(500)
|
use serde::{Deserialize, Serialize};
|
||||||
.into_client::<UserFeatures, reqwest::Client>(
|
use std::error::Error;
|
||||||
"<YOUR_API_URL>",
|
use std::time::Duration;
|
||||||
"unleash-onboarding-rust",
|
use tokio::time::sleep;
|
||||||
"unleash-onboarding-instance",
|
use unleash_api_client::client::ClientBuilder;
|
||||||
"<YOUR_API_TOKEN>",
|
use unleash_api_client::Client;
|
||||||
)?;
|
|
||||||
client.register().await?;
|
#[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(())
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user