1use crate::client::http::HttpClient;
2
3pub 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(
25 &self,
26 ) -> impl std::future::Future<Output = anyhow::Result<reqwest::Response>> + Send {
27 let url = self.url.clone();
28 let key = self.key.clone();
29 async move {
30 let resp = reqwest::Client::new()
31 .delete(url)
32 .bearer_auth(key)
33 .send()
34 .await?;
35
36 let status = resp.status();
37 if status.is_success() {
38 return Ok(resp);
39 }
40 let text = resp.text().await.unwrap_or_default();
41 #[derive(serde::Deserialize)]
42 struct ErrEnv {
43 error: ErrObj,
44 }
45 #[derive(serde::Deserialize)]
46 struct ErrObj {
47 code: serde_json::Value,
48 message: String,
49 }
50 if let Ok(parsed) = serde_json::from_str::<ErrEnv>(&text) {
51 return Err(anyhow::anyhow!(
52 "HTTP {} {} | code={} | message={}",
53 status.as_u16(),
54 status.canonical_reason().unwrap_or(""),
55 parsed.error.code,
56 parsed.error.message
57 ));
58 } else {
59 return Err(anyhow::anyhow!(
60 "HTTP {} {} | body={}",
61 status.as_u16(),
62 status.canonical_reason().unwrap_or(""),
63 text
64 ));
65 }
66 }
67 }
68
69 pub async fn send(&self) -> anyhow::Result<super::response::FileDeleteResponse> {
71 let resp = self.delete().await?;
72 let parsed = resp.json::<super::response::FileDeleteResponse>().await?;
73 Ok(parsed)
74 }
75}
76
77impl HttpClient for FileDeleteRequest {
78 type Body = ();
79 type ApiUrl = String;
80 type ApiKey = String;
81
82 fn api_url(&self) -> &Self::ApiUrl {
83 &self.url
84 }
85 fn api_key(&self) -> &Self::ApiKey {
86 &self.key
87 }
88 fn body(&self) -> &Self::Body {
89 &self._body
90 }
91}