1use super::request::{FileListPurpose, FileListQuery};
2use crate::{ZaiResult, client::ZaiClient};
3
4pub struct FileListRequest {
9 query: FileListQuery,
10}
11
12impl FileListRequest {
13 pub fn new(purpose: FileListPurpose) -> Self {
15 Self {
16 query: FileListQuery::new(purpose),
17 }
18 }
19
20 pub fn with_query(mut self, q: FileListQuery) -> Self {
22 self.query = q;
23 self
24 }
25
26 pub async fn send_via(
28 &self,
29 client: &ZaiClient,
30 ) -> ZaiResult<super::response::FileListResponse> {
31 use validator::Validate;
32
33 self.query.validate()?;
34 if self
35 .query
36 .after
37 .as_deref()
38 .is_some_and(|after| after.trim().is_empty())
39 {
40 return Err(crate::client::validation::invalid(
41 "after cannot be blank when provided",
42 ));
43 }
44 let mut params: Vec<(&str, String)> = Vec::new();
45 if let Some(after) = self.query.after.as_ref() {
46 params.push(("after", after.clone()));
47 }
48 params.push(("purpose", self.query.purpose.as_str().to_string()));
49 if let Some(order) = self.query.order.as_ref() {
50 params.push(("order", order.as_str().to_string()));
51 }
52 if let Some(limit) = self.query.limit.as_ref() {
53 params.push(("limit", limit.to_string()));
54 }
55 let borrowed: Vec<(&str, &str)> = params.iter().map(|(k, v)| (*k, v.as_str())).collect();
56 let route = crate::client::routes::FILES_LIST;
57 let url = client
58 .endpoints()
59 .resolve_route_with_query(route, &[], &borrowed)?;
60 client
61 .send_empty::<super::response::FileListResponse>(route.method(), url)
62 .await
63 }
64}