Skip to main content

zai_rs/file/
list.rs

1use url::Url;
2
3use super::request::FileListQuery;
4use crate::{ZaiResult, client::http::HttpClient};
5
6/// Files list request (GET /paas/v4/files)
7///
8/// Builds query parameters from `FileListQuery` and performs an authenticated
9/// GET.
10pub struct FileListRequest {
11    pub key: String,
12    url: String,
13    _body: (),
14}
15
16impl FileListRequest {
17    pub fn new(key: String) -> Self {
18        let url = "https://open.bigmodel.cn/api/paas/v4/files".to_string();
19        Self {
20            key,
21            url,
22            _body: (),
23        }
24    }
25
26    fn rebuild_url(&mut self, q: &FileListQuery) {
27        let mut url = Url::parse("https://open.bigmodel.cn/api/paas/v4/files").unwrap();
28        {
29            let mut pairs = url.query_pairs_mut();
30            if let Some(after) = q.after.as_ref() {
31                pairs.append_pair("after", after);
32            }
33            if let Some(purpose) = q.purpose.as_ref() {
34                pairs.append_pair("purpose", purpose.as_str());
35            }
36            if let Some(order) = q.order.as_ref() {
37                pairs.append_pair("order", order.as_str());
38            }
39            if let Some(limit) = q.limit.as_ref() {
40                pairs.append_pair("limit", &limit.to_string());
41            }
42        }
43        self.url = url.to_string();
44    }
45
46    pub fn with_query(mut self, q: FileListQuery) -> Self {
47        self.rebuild_url(&q);
48        self
49    }
50    /// Send request and parse typed response.
51    pub async fn send(&self) -> ZaiResult<super::response::FileListResponse> {
52        let resp = self.get().await?;
53        let parsed = resp.json::<super::response::FileListResponse>().await?;
54        Ok(parsed)
55    }
56
57    /// Validate query, rebuild URL and send in one call.
58    pub async fn send_with_query(
59        mut self,
60        q: &super::request::FileListQuery,
61    ) -> ZaiResult<super::response::FileListResponse> {
62        use validator::Validate;
63        q.validate()?;
64        self.rebuild_url(q);
65        self.send().await
66    }
67}
68
69impl HttpClient for FileListRequest {
70    type Body = ();
71    type ApiUrl = String;
72    type ApiKey = String;
73
74    fn api_url(&self) -> &Self::ApiUrl {
75        &self.url
76    }
77    fn api_key(&self) -> &Self::ApiKey {
78        &self.key
79    }
80    fn body(&self) -> &Self::Body {
81        &self._body
82    }
83}