diff --git a/example/rust/src/docs_examples/cheat_sheet_storage_client.rs b/example/rust/src/docs_examples/cheat_sheet_storage_client.rs new file mode 100644 index 00000000..037adf73 --- /dev/null +++ b/example/rust/src/docs_examples/cheat_sheet_storage_client.rs @@ -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(()) +} \ No newline at end of file diff --git a/example/rust/src/docs_examples/docs_examples.rs b/example/rust/src/docs_examples/docs_examples.rs index e704fa30..e62c283f 100644 --- a/example/rust/src/docs_examples/docs_examples.rs +++ b/example/rust/src/docs_examples/docs_examples.rs @@ -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; @@ -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(); @@ -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(); @@ -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(()) } diff --git a/sdk/Cargo.lock b/sdk/Cargo.lock index 6749acd4..096cc39b 100644 --- a/sdk/Cargo.lock +++ b/sdk/Cargo.lock @@ -643,7 +643,7 @@ dependencies = [ [[package]] name = "momento" -version = "0.41.3" +version = "0.42.0" dependencies = [ "anyhow", "base64", @@ -679,7 +679,7 @@ dependencies = [ [[package]] name = "momento-test-util" -version = "0.41.3" +version = "0.42.0" dependencies = [ "anyhow", "momento",