From 31112d500c4b45786de485d36c5303a17e15b14e Mon Sep 17 00:00:00 2001 From: anitarua Date: Fri, 3 Jan 2025 12:50:12 -0800 Subject: [PATCH] chore: add auth client docs snippets --- example/rust/Cargo.lock | 4 +- example/rust/Cargo.toml | 2 +- .../rust/src/docs_examples/docs_examples.rs | 45 +++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/example/rust/Cargo.lock b/example/rust/Cargo.lock index fc7eee80..6e7519b9 100644 --- a/example/rust/Cargo.lock +++ b/example/rust/Cargo.lock @@ -535,9 +535,9 @@ dependencies = [ [[package]] name = "momento" -version = "0.44.0" +version = "0.47.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70b17859dc0a6c5c0f227f98054723ec32a07f00d6851663ddd9a4d1f00d87b1" +checksum = "fc7de71fc41987b56bda2a3f97f31e701431d63ad29a01ba5fe0b4f78cb01d01" dependencies = [ "base64", "derive_more", diff --git a/example/rust/Cargo.toml b/example/rust/Cargo.toml index eba00359..69071c27 100644 --- a/example/rust/Cargo.toml +++ b/example/rust/Cargo.toml @@ -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" diff --git a/example/rust/src/docs_examples/docs_examples.rs b/example/rust/src/docs_examples/docs_examples.rs index e62c283f..878c4aeb 100644 --- a/example/rust/src/docs_examples/docs_examples.rs +++ b/example/rust/src/docs_examples/docs_examples.rs @@ -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; @@ -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(); @@ -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(()) }