zai_rs/knowledge/
document_delete.rs

1use crate::client::http::HttpClient;
2
3/// Document delete request (DELETE /llm-application/open/document/{id})
4pub struct DocumentDeleteRequest {
5    /// Bearer API key
6    pub key: String,
7    url: String,
8    _body: (),
9}
10
11impl DocumentDeleteRequest {
12    /// Build a delete request with target document 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/document/{}",
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
43            let text = resp.text().await.unwrap_or_default();
44            #[derive(serde::Deserialize)]
45            struct ErrEnv {
46                error: ErrObj,
47            }
48            #[derive(serde::Deserialize)]
49            struct ErrObj {
50                code: serde_json::Value,
51                message: String,
52            }
53            if let Ok(parsed) = serde_json::from_str::<ErrEnv>(&text) {
54                return Err(anyhow::anyhow!(
55                    "HTTP {} {} | code={} | message={}",
56                    status.as_u16(),
57                    status.canonical_reason().unwrap_or(""),
58                    parsed.error.code,
59                    parsed.error.message
60                ));
61            }
62            Err(anyhow::anyhow!(
63                "HTTP {} {} | body={}",
64                status.as_u16(),
65                status.canonical_reason().unwrap_or(""),
66                text
67            ))
68        }
69    }
70
71    /// Send delete request and parse typed response
72    pub async fn send(&self) -> anyhow::Result<DocumentDeleteResponse> {
73        let resp = self.delete().await?;
74        let parsed = resp.json::<DocumentDeleteResponse>().await?;
75        Ok(parsed)
76    }
77}
78
79impl HttpClient for DocumentDeleteRequest {
80    type Body = (); // unused
81    type ApiUrl = String;
82    type ApiKey = String;
83
84    fn api_url(&self) -> &Self::ApiUrl {
85        &self.url
86    }
87    fn api_key(&self) -> &Self::ApiKey {
88        &self.key
89    }
90    fn body(&self) -> &Self::Body {
91        &self._body
92    }
93}
94
95/// Delete response envelope without data
96#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, validator::Validate)]
97pub struct DocumentDeleteResponse {
98    #[serde(skip_serializing_if = "Option::is_none")]
99    pub code: Option<i64>,
100    #[serde(skip_serializing_if = "Option::is_none")]
101    pub message: Option<String>,
102    #[serde(skip_serializing_if = "Option::is_none")]
103    pub timestamp: Option<u64>,
104}