covert_sdk/
kv.rs

1use std::sync::Arc;
2
3use covert_types::methods::kv::CreateSecretResponse;
4pub use covert_types::methods::kv::{
5    CreateSecretParams, HardDeleteSecretParams, HardDeleteSecretResponse, ReadConfigResponse,
6    ReadSecretResponse, RecoverSecretParams, RecoverSecretResponse, SetConfigParams,
7    SetConfigResponse, SoftDeleteSecretParams, SoftDeleteSecretResponse,
8};
9
10use crate::{base::BaseClient, utils::get_mount_path};
11
12pub struct Client {
13    config: Arc<BaseClient>,
14}
15
16impl Client {
17    pub(crate) fn new(config: Arc<BaseClient>) -> Self {
18        Self { config }
19    }
20
21    pub async fn create(
22        &self,
23        mount: &str,
24        key: &str,
25        params: &CreateSecretParams,
26    ) -> Result<CreateSecretResponse, String> {
27        let path = get_mount_path(mount, &format!("data/{key}"));
28        self.config.post(path, params).await
29    }
30
31    pub async fn read(
32        &self,
33        mount: &str,
34        key: &str,
35        version: Option<u32>,
36    ) -> Result<ReadSecretResponse, String> {
37        let mut path = get_mount_path(mount, &format!("data/{key}"));
38        if let Some(version) = version {
39            path = format!("{path}?version={version}");
40        }
41        self.config.get(path).await
42    }
43
44    pub async fn set_config(
45        &self,
46        mount: &str,
47        params: &SetConfigParams,
48    ) -> Result<SetConfigResponse, String> {
49        let path = get_mount_path(mount, "config");
50        self.config.post(path, params).await
51    }
52
53    pub async fn read_config(&self, mount: &str) -> Result<ReadConfigResponse, String> {
54        let path = get_mount_path(mount, "config");
55        self.config.get(path).await
56    }
57
58    pub async fn delete(
59        &self,
60        mount: &str,
61        key: &str,
62        params: &SoftDeleteSecretParams,
63    ) -> Result<SoftDeleteSecretResponse, String> {
64        let path = get_mount_path(mount, &format!("delete/{key}"));
65        self.config.post(path, params).await
66    }
67
68    pub async fn recover(
69        &self,
70        mount: &str,
71        key: &str,
72        params: &RecoverSecretParams,
73    ) -> Result<RecoverSecretResponse, String> {
74        let path = get_mount_path(mount, &format!("undelete/{key}"));
75        self.config.post(path, params).await
76    }
77
78    pub async fn hard_delete(
79        &self,
80        mount: &str,
81        key: &str,
82        params: &HardDeleteSecretParams,
83    ) -> Result<HardDeleteSecretResponse, String> {
84        let path = get_mount_path(mount, &format!("destroy/{key}"));
85        self.config.post(path, params).await
86    }
87}