Skip to content

Commit 223be86

Browse files
authored
chore: add dev docs snippets and cheat sheet for storage client (#389)
* chore: add dev docs snippets for storage client * show example of getting value using match vs try_into * add storage client cheat sheet
1 parent e64afce commit 223be86

File tree

3 files changed

+120
-3
lines changed

3 files changed

+120
-3
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use momento::{
2+
storage, CredentialProvider, MomentoResult, PreviewStorageClient,
3+
};
4+
5+
#[tokio::main]
6+
async fn main() -> MomentoResult<()> {
7+
let storage_client = PreviewStorageClient::builder()
8+
.configuration(storage::configurations::Laptop::latest())
9+
.credential_provider(
10+
CredentialProvider::from_env_var("MOMENTO_API_KEY".to_string())
11+
.expect("API key should be valid"),
12+
)
13+
.build()?;
14+
15+
// ...
16+
17+
Ok(())
18+
}

example/rust/src/docs_examples/docs_examples.rs

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use momento::cache::{
2525
SetIfPresentResponse, SetIfPresentAndNotEqualResponse, SortedSetFetchResponse, SortedSetOrder, UpdateTtlResponse,
2626
};
2727
use momento::topics::TopicClient;
28-
use momento::{CacheClient, CredentialProvider, MomentoError};
28+
use momento::{CacheClient, CredentialProvider, MomentoError, PreviewStorageClient};
2929
use std::collections::HashMap;
3030
use std::convert::TryInto;
3131
use std::time::Duration;
@@ -36,6 +36,11 @@ pub fn example_API_CredentialProviderFromString() {
3636
let _credential_provider = CredentialProvider::from_string("my-api-key".to_string());
3737
}
3838

39+
#[allow(non_snake_case)]
40+
pub fn example_API_CredentialProviderFromEnvVar() {
41+
let _credential_provider = CredentialProvider::from_env_var("MOMENTO_API_KEY".to_string());
42+
}
43+
3944
#[allow(non_snake_case)]
4045
pub fn example_API_ConfigurationLaptop() {
4146
let _config = momento::cache::configurations::Laptop::latest();
@@ -651,9 +656,83 @@ pub async fn example_responsetypes_dictionary_with_try_into(cache_client: &Cache
651656
Ok(())
652657
}
653658

659+
#[allow(non_snake_case)]
660+
pub fn example_API_Storage_InstantiateClient() -> Result<(), MomentoError> {
661+
let _storage_client = PreviewStorageClient::builder()
662+
.configuration(momento::storage::configurations::Laptop::latest())
663+
.credential_provider(CredentialProvider::from_env_var(
664+
"MOMENTO_API_KEY".to_string(),
665+
)?)
666+
.build()?;
667+
Ok(())
668+
}
669+
670+
#[allow(non_snake_case)]
671+
pub async fn example_API_Storage_CreateStore(storage_client: &PreviewStorageClient, store_name: &String) -> Result<(), MomentoError> {
672+
let response = storage_client.create_store(store_name).await?;
673+
match response {
674+
momento::storage::CreateStoreResponse::Created => println!("Store {} created", store_name),
675+
momento::storage::CreateStoreResponse::AlreadyExists => println!("Store {} already exists", store_name),
676+
}
677+
Ok(())
678+
}
679+
680+
#[allow(non_snake_case)]
681+
pub async fn example_API_Storage_ListStores(storage_client: &PreviewStorageClient) -> Result<(), MomentoError> {
682+
let response = storage_client.list_stores().await?;
683+
println!("Stores: {:#?}", response.stores);
684+
Ok(())
685+
}
686+
687+
#[allow(non_snake_case)]
688+
pub async fn example_API_Storage_DeleteStore(storage_client: &PreviewStorageClient, store_name: &String) -> Result<(), MomentoError> {
689+
storage_client.delete_store(store_name).await?;
690+
println!("Store {} deleted", store_name);
691+
Ok(())
692+
}
693+
694+
#[allow(non_snake_case)]
695+
pub async fn example_API_Storage_Put(storage_client: &PreviewStorageClient, store_name: &String) -> Result<(), MomentoError> {
696+
storage_client.put(store_name, "key", "value").await?;
697+
println!("Put key and value in store {}", store_name);
698+
Ok(())
699+
}
700+
701+
#[allow(non_snake_case)]
702+
pub async fn example_API_Storage_Get(storage_client: &PreviewStorageClient, store_name: &String) -> Result<(), MomentoError> {
703+
let response = storage_client.get(store_name, "key").await?;
704+
match response {
705+
momento::storage::GetResponse::NotFound => println!("Key not found in {}", store_name),
706+
momento::storage::GetResponse::Found { value } => {
707+
// A Found response indicates the value was found in the store.
708+
709+
// Use `match` to get the value if you don't know the type beforehand:
710+
match value.clone() {
711+
momento::storage::StorageValue::String(value) => println!("Got string value {}", value),
712+
momento::storage::StorageValue::Bytes(value) => println!("Got bytes value {:?}", value),
713+
momento::storage::StorageValue::Integer(value) => println!("Got integer value {}", value),
714+
momento::storage::StorageValue::Double(value) => println!("Got double value {}", value),
715+
}
716+
717+
// If you know the type you're expecting, you can `try_into()` it directly:
718+
let found_value: String = value.try_into()?;
719+
println!("Got value {}", found_value);
720+
}
721+
}
722+
Ok(())
723+
}
724+
725+
#[allow(non_snake_case)]
726+
pub async fn example_API_Storage_Delete(storage_client: &PreviewStorageClient, store_name: &String) -> Result<(), MomentoError> {
727+
storage_client.delete(store_name, "key").await?;
728+
println!("Deleted key from store {}", store_name);
729+
Ok(())
730+
}
731+
654732
#[tokio::main]
655733
pub async fn main() -> Result<(), MomentoError> {
656734
example_API_CredentialProviderFromString();
735+
example_API_CredentialProviderFromEnvVar();
657736
example_API_ConfigurationLaptop();
658737
example_API_ConfigurationInRegionDefaultLatest();
659738
example_API_ConfigurationInRegionLowLatency();
@@ -742,5 +821,25 @@ pub async fn main() -> Result<(), MomentoError> {
742821
.await;
743822

744823
example_API_DeleteCache(&cache_client, &cache_name).await?;
824+
825+
example_API_Storage_InstantiateClient()?;
826+
827+
let storage_client = PreviewStorageClient::builder()
828+
.configuration(momento::storage::configurations::Laptop::latest())
829+
.credential_provider(CredentialProvider::from_env_var(
830+
"MOMENTO_API_KEY".to_string(),
831+
)?)
832+
.build()?;
833+
let store_name = format!("{}-{}", "docs-examples", Uuid::new_v4());
834+
835+
example_API_Storage_CreateStore(&storage_client, &store_name).await?;
836+
example_API_Storage_ListStores(&storage_client).await?;
837+
838+
example_API_Storage_Put(&storage_client, &store_name).await?;
839+
example_API_Storage_Get(&storage_client, &store_name).await?;
840+
example_API_Storage_Delete(&storage_client, &store_name).await?;
841+
842+
example_API_Storage_DeleteStore(&storage_client, &store_name).await?;
843+
745844
Ok(())
746845
}

sdk/Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)