nacos_api/api/
config.rs

1use crate::{NacosConfig, util};
2use crate::model::DeployConfig;
3use std::error::Error;
4
5const GET_CONFIGS: &str = "/v1/cs/configs";
6const POST_CONFIGS: &str = "/v1/cs/configs";
7const DELETE_CONFIGS: &str = "/v1/cs/configs";
8
9#[derive(Clone)]
10pub struct NacosConfigApi {
11    deploy_config: DeployConfig,
12}
13
14impl NacosConfigApi {
15    pub fn new(config: DeployConfig) -> Self {
16        Self {
17            deploy_config: config,
18        }
19    }
20    pub fn deploy_config(&self) -> &DeployConfig {
21        &self.deploy_config
22    }
23}
24
25impl NacosConfigApi {
26    pub async fn get_configs(&self, nacos: &NacosConfig)
27                             -> Result<String, Box<dyn Error>> {
28        let map = self.deploy_config.init_map();
29        let resp = util::query(&map, |c| c.get(nacos.addr(GET_CONFIGS))).await?;
30        let result = resp.text().await?;
31        Ok(result)
32    }
33
34    pub async fn upload_configs(nacos: &NacosConfig, config: DeployConfig, content: &str, types: Option<String>)
35                                -> Result<(), Box<dyn Error>> {
36        let mut map = config.init_map();
37        map.insert("content".to_string(), content.to_string());
38        if let Some(t) = types { map.insert("type".to_string(), t); }
39        let resp = util::query(&map, |c| c.post(nacos.addr(POST_CONFIGS))).await?;
40        util::resp_assert(resp, "true").await
41    }
42
43    pub async fn delete_configs(nacos: &NacosConfig, config: DeployConfig)
44                                -> Result<(), Box<dyn Error>> {
45        let map = config.init_map();
46        let resp = util::query(&map, |c| c.delete(nacos.addr(DELETE_CONFIGS))).await?;
47        util::resp_assert(resp, "true").await
48    }
49}