zai_rs/knowledge/
delete.rs

1use crate::client::http::HttpClient;
2
3/// Knowledge delete request (DELETE /llm-application/open/knowledge/{id})
4pub struct KnowledgeDeleteRequest {
5    /// Bearer API key
6    pub key: String,
7    url: String,
8    _body: (),
9}
10
11impl KnowledgeDeleteRequest {
12    /// Build a delete request with target id
13    pub fn new(key: String, id: impl AsRef<str>) -> Self {
14        let url = format!(
15            "https://open.bigmodel.cn/api/llm-application/open/knowledge/{}",
16            id.as_ref()
17        );
18        Self {
19            key,
20            url,
21            _body: (),
22        }
23    }
24
25    /// Perform HTTP DELETE with error handling compatible with {"error":{...}}
26    pub fn delete(
27        &self,
28    ) -> impl std::future::Future<Output = anyhow::Result<reqwest::Response>> + Send {
29        let url = self.url.clone();
30        let key = self.key.clone();
31        async move {
32            let resp = reqwest::Client::new()
33                .delete(url)
34                .bearer_auth(key)
35                .send()
36                .await?;
37
38            let status = resp.status();
39            if status.is_success() {
40                return Ok(resp);
41            }
42            let text = resp.text().await.unwrap_or_default();
43            #[derive(serde::Deserialize)]
44            struct ErrEnv {
45                error: ErrObj,
46            }
47            #[derive(serde::Deserialize)]
48            struct ErrObj {
49                code: serde_json::Value,
50                message: String,
51            }
52            if let Ok(parsed) = serde_json::from_str::<ErrEnv>(&text) {
53                return Err(anyhow::anyhow!(
54                    "HTTP {} {} | code={} | message={}",
55                    status.as_u16(),
56                    status.canonical_reason().unwrap_or(""),
57                    parsed.error.code,
58                    parsed.error.message
59                ));
60            }
61            Err(anyhow::anyhow!(
62                "HTTP {} {} | body={}",
63                status.as_u16(),
64                status.canonical_reason().unwrap_or(""),
65                text
66            ))
67        }
68    }
69
70    /// Send delete request and parse typed response
71    pub async fn send(&self) -> anyhow::Result<KnowledgeDeleteResponse> {
72        let resp = self.delete().await?;
73        let parsed = resp.json::<KnowledgeDeleteResponse>().await?;
74        Ok(parsed)
75    }
76}
77
78impl HttpClient for KnowledgeDeleteRequest {
79    type Body = ();
80    type ApiUrl = String;
81    type ApiKey = String;
82
83    fn api_url(&self) -> &Self::ApiUrl {
84        &self.url
85    }
86    fn api_key(&self) -> &Self::ApiKey {
87        &self.key
88    }
89    fn body(&self) -> &Self::Body {
90        &self._body
91    }
92}
93
94/// Delete response envelope without data
95#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, validator::Validate)]
96pub struct KnowledgeDeleteResponse {
97    #[serde(skip_serializing_if = "Option::is_none")]
98    pub code: Option<i64>,
99    #[serde(skip_serializing_if = "Option::is_none")]
100    pub message: Option<String>,
101    #[serde(skip_serializing_if = "Option::is_none")]
102    pub timestamp: Option<u64>,
103}