shadow_drive_sdk/client/list_objects.rs
1use serde_json::{json, Value};
2use solana_sdk::{pubkey::Pubkey, signer::Signer};
3
4use crate::{
5 constants::SHDW_DRIVE_ENDPOINT,
6 error::Error,
7 models::{ListObjectsResponse, ShadowDriveResult},
8};
9
10use super::ShadowDriveClient;
11
12impl<T> ShadowDriveClient<T>
13where
14 T: Signer,
15{
16 /// Gets a list of all files associated with a storage account.
17 /// The output contains all of the file names as strings.
18 /// * `storage_account_key` - The public key of the [`StorageAccount`](crate::models::StorageAccount) that owns the files.
19 ///
20 /// # Example
21 ///
22 /// ```
23 /// # use shadow_drive_rust::{ShadowDriveClient, derived_addresses::storage_account};
24 /// # use solana_client::rpc_client::RpcClient;
25 /// # use solana_sdk::{
26 /// # pubkey::Pubkey,
27 /// # signature::Keypair,
28 /// # signer::{keypair::read_keypair_file, Signer},
29 /// # };
30 /// #
31 /// # let keypair = read_keypair_file(KEYPAIR_PATH).expect("failed to load keypair at path");
32 /// # let user_pubkey = keypair.pubkey();
33 /// # let rpc_client = RpcClient::new("https://ssc-dao.genesysgo.net");
34 /// # let shdw_drive_client = ShadowDriveClient::new(keypair, rpc_client);
35 /// # let (storage_account_key, _) = storage_account(&user_pubkey, 0);
36 /// #
37 /// let files = shdw_drive_client
38 /// .list_objects(&storage_account_key)
39 /// .await?;
40 /// ```
41 pub async fn list_objects(
42 &self,
43 storage_account_key: &Pubkey,
44 ) -> ShadowDriveResult<Vec<String>> {
45 let response = self
46 .http_client
47 .post(format!("{}/list-objects", SHDW_DRIVE_ENDPOINT))
48 .json(&json!({
49 "storageAccount": storage_account_key.to_string()
50 }))
51 .send()
52 .await?;
53
54 if !response.status().is_success() {
55 return Err(Error::ShadowDriveServerError {
56 status: response.status().as_u16(),
57 message: response.json::<Value>().await?,
58 });
59 }
60 response
61 .json::<ListObjectsResponse>()
62 .await
63 .map(|response| response.keys)
64 .map_err(Error::from)
65 }
66}