zai_rs/file/
list.rs

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