Skip to content

Commit

Permalink
chore: add dev docs snippets and cheat sheet for storage client (#389)
Browse files Browse the repository at this point in the history
* chore: add dev docs snippets for storage client

* show example of getting value using match vs try_into

* add storage client cheat sheet
  • Loading branch information
anitarua authored Jul 17, 2024
1 parent e64afce commit 223be86
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 3 deletions.
18 changes: 18 additions & 0 deletions example/rust/src/docs_examples/cheat_sheet_storage_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use momento::{
storage, CredentialProvider, MomentoResult, PreviewStorageClient,
};

#[tokio::main]
async fn main() -> MomentoResult<()> {
let storage_client = PreviewStorageClient::builder()
.configuration(storage::configurations::Laptop::latest())
.credential_provider(
CredentialProvider::from_env_var("MOMENTO_API_KEY".to_string())
.expect("API key should be valid"),
)
.build()?;

// ...

Ok(())
}
101 changes: 100 additions & 1 deletion example/rust/src/docs_examples/docs_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use momento::cache::{
SetIfPresentResponse, SetIfPresentAndNotEqualResponse, SortedSetFetchResponse, SortedSetOrder, UpdateTtlResponse,
};
use momento::topics::TopicClient;
use momento::{CacheClient, CredentialProvider, MomentoError};
use momento::{CacheClient, CredentialProvider, MomentoError, PreviewStorageClient};
use std::collections::HashMap;
use std::convert::TryInto;
use std::time::Duration;
Expand All @@ -36,6 +36,11 @@ pub fn example_API_CredentialProviderFromString() {
let _credential_provider = CredentialProvider::from_string("my-api-key".to_string());
}

#[allow(non_snake_case)]
pub fn example_API_CredentialProviderFromEnvVar() {
let _credential_provider = CredentialProvider::from_env_var("MOMENTO_API_KEY".to_string());
}

#[allow(non_snake_case)]
pub fn example_API_ConfigurationLaptop() {
let _config = momento::cache::configurations::Laptop::latest();
Expand Down Expand Up @@ -651,9 +656,83 @@ pub async fn example_responsetypes_dictionary_with_try_into(cache_client: &Cache
Ok(())
}

#[allow(non_snake_case)]
pub fn example_API_Storage_InstantiateClient() -> Result<(), MomentoError> {
let _storage_client = PreviewStorageClient::builder()
.configuration(momento::storage::configurations::Laptop::latest())
.credential_provider(CredentialProvider::from_env_var(
"MOMENTO_API_KEY".to_string(),
)?)
.build()?;
Ok(())
}

#[allow(non_snake_case)]
pub async fn example_API_Storage_CreateStore(storage_client: &PreviewStorageClient, store_name: &String) -> Result<(), MomentoError> {
let response = storage_client.create_store(store_name).await?;
match response {
momento::storage::CreateStoreResponse::Created => println!("Store {} created", store_name),
momento::storage::CreateStoreResponse::AlreadyExists => println!("Store {} already exists", store_name),
}
Ok(())
}

#[allow(non_snake_case)]
pub async fn example_API_Storage_ListStores(storage_client: &PreviewStorageClient) -> Result<(), MomentoError> {
let response = storage_client.list_stores().await?;
println!("Stores: {:#?}", response.stores);
Ok(())
}

#[allow(non_snake_case)]
pub async fn example_API_Storage_DeleteStore(storage_client: &PreviewStorageClient, store_name: &String) -> Result<(), MomentoError> {
storage_client.delete_store(store_name).await?;
println!("Store {} deleted", store_name);
Ok(())
}

#[allow(non_snake_case)]
pub async fn example_API_Storage_Put(storage_client: &PreviewStorageClient, store_name: &String) -> Result<(), MomentoError> {
storage_client.put(store_name, "key", "value").await?;
println!("Put key and value in store {}", store_name);
Ok(())
}

#[allow(non_snake_case)]
pub async fn example_API_Storage_Get(storage_client: &PreviewStorageClient, store_name: &String) -> Result<(), MomentoError> {
let response = storage_client.get(store_name, "key").await?;
match response {
momento::storage::GetResponse::NotFound => println!("Key not found in {}", store_name),
momento::storage::GetResponse::Found { value } => {
// A Found response indicates the value was found in the store.

// Use `match` to get the value if you don't know the type beforehand:
match value.clone() {
momento::storage::StorageValue::String(value) => println!("Got string value {}", value),
momento::storage::StorageValue::Bytes(value) => println!("Got bytes value {:?}", value),
momento::storage::StorageValue::Integer(value) => println!("Got integer value {}", value),
momento::storage::StorageValue::Double(value) => println!("Got double value {}", value),
}

// If you know the type you're expecting, you can `try_into()` it directly:
let found_value: String = value.try_into()?;
println!("Got value {}", found_value);
}
}
Ok(())
}

#[allow(non_snake_case)]
pub async fn example_API_Storage_Delete(storage_client: &PreviewStorageClient, store_name: &String) -> Result<(), MomentoError> {
storage_client.delete(store_name, "key").await?;
println!("Deleted key from store {}", store_name);
Ok(())
}

#[tokio::main]
pub async fn main() -> Result<(), MomentoError> {
example_API_CredentialProviderFromString();
example_API_CredentialProviderFromEnvVar();
example_API_ConfigurationLaptop();
example_API_ConfigurationInRegionDefaultLatest();
example_API_ConfigurationInRegionLowLatency();
Expand Down Expand Up @@ -742,5 +821,25 @@ pub async fn main() -> Result<(), MomentoError> {
.await;

example_API_DeleteCache(&cache_client, &cache_name).await?;

example_API_Storage_InstantiateClient()?;

let storage_client = PreviewStorageClient::builder()
.configuration(momento::storage::configurations::Laptop::latest())
.credential_provider(CredentialProvider::from_env_var(
"MOMENTO_API_KEY".to_string(),
)?)
.build()?;
let store_name = format!("{}-{}", "docs-examples", Uuid::new_v4());

example_API_Storage_CreateStore(&storage_client, &store_name).await?;
example_API_Storage_ListStores(&storage_client).await?;

example_API_Storage_Put(&storage_client, &store_name).await?;
example_API_Storage_Get(&storage_client, &store_name).await?;
example_API_Storage_Delete(&storage_client, &store_name).await?;

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

Ok(())
}
4 changes: 2 additions & 2 deletions sdk/Cargo.lock

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

0 comments on commit 223be86

Please sign in to comment.