Skip to main content

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    pub fn delete(
26        &self,
27    ) -> impl std::future::Future<Output = crate::ZaiResult<reqwest::Response>> + Send {
28        let url = self.url.clone();
29        let key = self.key.clone();
30        async move {
31            let resp = reqwest::Client::new()
32                .delete(url)
33                .bearer_auth(key)
34                .send()
35                .await?;
36
37            let status = resp.status();
38            if status.is_success() {
39                return Ok(resp);
40            }
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
53            if let Ok(parsed) = serde_json::from_str::<ErrEnv>(&text) {
54                return Err(crate::client::error::ZaiError::from_api_response(
55                    status.as_u16(),
56                    0,
57                    parsed.error.message,
58                ));
59            }
60
61            Err(crate::client::error::ZaiError::from_api_response(
62                status.as_u16(),
63                0,
64                text,
65            ))
66        }
67    }
68
69    /// Send delete request and parse typed response
70    pub async fn send(&self) -> crate::ZaiResult<DocumentDeleteResponse> {
71        let resp = self.delete().await?;
72
73        let parsed = resp.json::<DocumentDeleteResponse>().await?;
74
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}