Skip to main content

zai_rs/file/
delete.rs

1use crate::client::http::HttpClient;
2
3/// File delete request (DELETE /paas/v4/files/{file_id})
4pub struct FileDeleteRequest {
5    pub key: String,
6    url: String,
7    _body: (),
8}
9
10impl FileDeleteRequest {
11    pub fn new(key: String, file_id: impl Into<String>) -> Self {
12        let url = format!(
13            "https://open.bigmodel.cn/api/paas/v4/files/{}",
14            file_id.into()
15        );
16        Self {
17            key,
18            url,
19            _body: (),
20        }
21    }
22
23    pub fn delete(
24        &self,
25    ) -> impl std::future::Future<Output = crate::ZaiResult<reqwest::Response>> + Send {
26        let url = self.url.clone();
27        let key = self.key.clone();
28        async move {
29            let resp = reqwest::Client::new()
30                .delete(url)
31                .bearer_auth(key)
32                .send()
33                .await?;
34
35            let status = resp.status();
36            if status.is_success() {
37                return Ok(resp);
38            }
39            let text = resp.text().await.unwrap_or_default();
40            #[derive(serde::Deserialize)]
41            struct ErrEnv {
42                error: ErrObj,
43            }
44            #[derive(serde::Deserialize)]
45            struct ErrObj {
46                _code: serde_json::Value,
47                message: String,
48            }
49
50            if let Ok(parsed) = serde_json::from_str::<ErrEnv>(&text) {
51                Err(crate::client::error::ZaiError::from_api_response(
52                    status.as_u16(),
53                    0,
54                    parsed.error.message,
55                ))
56            } else {
57                Err(crate::client::error::ZaiError::from_api_response(
58                    status.as_u16(),
59                    0,
60                    text,
61                ))
62            }
63        }
64    }
65
66    /// Send delete request and parse typed response.
67    pub async fn send(&self) -> crate::ZaiResult<super::response::FileDeleteResponse> {
68        let resp = self.delete().await?;
69        let parsed = resp.json::<super::response::FileDeleteResponse>().await?;
70        Ok(parsed)
71    }
72}
73
74impl HttpClient for FileDeleteRequest {
75    type Body = ();
76    type ApiUrl = String;
77    type ApiKey = String;
78
79    fn api_url(&self) -> &Self::ApiUrl {
80        &self.url
81    }
82    fn api_key(&self) -> &Self::ApiKey {
83        &self.key
84    }
85    fn body(&self) -> &Self::Body {
86        &self._body
87    }
88}