@@ -25,7 +25,7 @@ use momento::cache::{
2525 SetIfPresentResponse , SetIfPresentAndNotEqualResponse , SortedSetFetchResponse , SortedSetOrder , UpdateTtlResponse ,
2626} ;
2727use momento:: topics:: TopicClient ;
28- use momento:: { CacheClient , CredentialProvider , MomentoError } ;
28+ use momento:: { CacheClient , CredentialProvider , MomentoError , PreviewStorageClient } ;
2929use std:: collections:: HashMap ;
3030use std:: convert:: TryInto ;
3131use 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) ]
4045pub 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]
655733pub 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}
0 commit comments