Skip to content

Commit

Permalink
chore: add auth client docs snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
anitarua committed Jan 3, 2025
1 parent 5f7600f commit 31112d5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
4 changes: 2 additions & 2 deletions example/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ path = "src/docs_examples/docs_examples.rs"

[dependencies]
futures = "0.3.30"
momento = { version = "0.44.0" }
momento = { version = "0.47.0" }
tokio = { version = "1.37.0", features = ["full"] }
uuid = { version = "1.8.0", features = ["v4"] }
anyhow = "1"
45 changes: 45 additions & 0 deletions example/rust/src/docs_examples/docs_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use momento::cache::{
SetIfPresentResponse, SetIfPresentAndNotEqualResponse, SortedSetFetchResponse, SortedSetOrder, UpdateTtlResponse,
};
use momento::topics::TopicClient;
use momento::auth::{AuthClient, CacheSelector, DisposableTokenScopes, ExpiresIn, GenerateDisposableTokenRequest};
use momento::{CacheClient, CredentialProvider, MomentoError, PreviewStorageClient};
use std::collections::HashMap;
use std::convert::TryInto;
Expand Down Expand Up @@ -729,6 +730,41 @@ pub async fn example_API_Storage_Delete(storage_client: &PreviewStorageClient, s
Ok(())
}

#[allow(non_snake_case)]
pub fn example_API_InstantiateAuthClient() -> Result<(), MomentoError> {
let _auth_client = AuthClient::builder()
.credential_provider(CredentialProvider::from_env_var("MOMENTO_API_KEY")?)
.build()?;
Ok(())
}

#[allow(non_snake_case)]
pub async fn example_API_GenerateDisposableToken(auth_client: &AuthClient) -> Result<(), MomentoError> {
// Basic example
let expiry = ExpiresIn::minutes(30);
let scope = DisposableTokenScopes::cache_key_read_only(CacheSelector::AllCaches, "key");
let response = auth_client
.generate_disposable_token(scope, expiry)
.await?;
let token = response.clone().auth_token();
println!(
"Generated disposable token ending with '{}' that expires at epoch {}",
&token[token.len() - 10 .. token.len() - 1], response.expires_at()
);

// Generate a token with optional token ID that can be used with Momento Topics
let expiry = ExpiresIn::minutes(30);
let scope = DisposableTokenScopes::cache_key_read_only(CacheSelector::AllCaches, "key");
let request = GenerateDisposableTokenRequest::new(scope, expiry).token_id("my-token-id".to_string());
let response = auth_client.send_request(request).await?;
let token = response.clone().auth_token();
println!(
"Generated disposable token ending with '{}' that expires at epoch {}",
&token[token.len() - 10 .. token.len() - 1], response.expires_at()
);
Ok(())
}

#[tokio::main]
pub async fn main() -> Result<(), MomentoError> {
example_API_CredentialProviderFromString();
Expand Down Expand Up @@ -841,5 +877,14 @@ pub async fn main() -> Result<(), MomentoError> {

example_API_Storage_DeleteStore(&storage_client, &store_name).await?;

example_API_InstantiateAuthClient()?;

let auth_client = AuthClient::builder()
.credential_provider(CredentialProvider::from_env_var(
"MOMENTO_API_KEY".to_string())?)
.build()?;

example_API_GenerateDisposableToken(&auth_client).await?;

Ok(())
}

0 comments on commit 31112d5

Please sign in to comment.