Skip to main content

zai_rs/batches/
list.rs

1use serde::{Deserialize, Serialize};
2use url::Url;
3use validator::Validate;
4
5use super::types::BatchItem;
6use crate::{ZaiResult, client::http::HttpClient};
7
8/// Query parameters for listing batch processing tasks
9#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
10pub struct BatchesListQuery {
11    /// Pagination cursor: return results after this ID
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub after: Option<String>,
14
15    /// Page size 1..=100 (server default 20)
16    #[serde(skip_serializing_if = "Option::is_none")]
17    #[validate(range(min = 1, max = 100))]
18    pub limit: Option<u32>,
19}
20
21impl Default for BatchesListQuery {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27impl BatchesListQuery {
28    /// Create an empty query (no filters)
29    pub fn new() -> Self {
30        Self {
31            after: None,
32            limit: None,
33        }
34    }
35
36    /// Set the pagination cursor
37    pub fn with_after(mut self, after: impl Into<String>) -> Self {
38        self.after = Some(after.into());
39        self
40    }
41
42    /// Set page size (1..=100)
43    pub fn with_limit(mut self, limit: u32) -> Self {
44        self.limit = Some(limit);
45        self
46    }
47}
48
49/// Batches list request (GET /paas/v4/batches)
50pub struct BatchesListRequest {
51    /// Bearer API key
52    pub key: String,
53    /// Fully built request URL (with query string)
54    url: String,
55    /// No body for GET
56    _body: (),
57}
58
59impl BatchesListRequest {
60    /// Create a request targeting the batches list endpoint
61    pub fn new(key: String) -> Self {
62        let url = "https://open.bigmodel.cn/api/paas/v4/batches".to_string();
63        Self {
64            key,
65            url,
66            _body: (),
67        }
68    }
69
70    /// Rebuild URL with query parameters
71    fn rebuild_url(&mut self, q: &BatchesListQuery) {
72        let mut url = Url::parse("https://open.bigmodel.cn/api/paas/v4/batches").unwrap();
73        {
74            let mut pairs = url.query_pairs_mut();
75            if let Some(after) = q.after.as_ref() {
76                pairs.append_pair("after", after);
77            }
78            if let Some(limit) = q.limit.as_ref() {
79                pairs.append_pair("limit", &limit.to_string());
80            }
81        }
82        self.url = url.to_string();
83    }
84
85    /// Attach a query to this request
86    pub fn with_query(mut self, q: BatchesListQuery) -> Self {
87        self.rebuild_url(&q);
88        self
89    }
90
91    /// Send the request and parse typed response.
92    pub async fn send(&self) -> ZaiResult<BatchesListResponse> {
93        let resp: reqwest::Response = self.get().await?;
94
95        let parsed = resp.json::<BatchesListResponse>().await?;
96
97        Ok(parsed)
98    }
99}
100
101impl HttpClient for BatchesListRequest {
102    type Body = ();
103    type ApiUrl = String;
104    type ApiKey = String;
105
106    fn api_url(&self) -> &Self::ApiUrl {
107        &self.url
108    }
109    fn api_key(&self) -> &Self::ApiKey {
110        &self.key
111    }
112    fn body(&self) -> &Self::Body {
113        &self._body
114    }
115}
116
117/// Response for listing batch processing tasks
118#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
119pub struct BatchesListResponse {
120    /// Response type ("list")
121    #[serde(skip_serializing_if = "Option::is_none")]
122    pub object: Option<ListObject>,
123
124    /// Batch task entries
125    #[serde(skip_serializing_if = "Option::is_none")]
126    pub data: Option<Vec<BatchItem>>,
127
128    /// First ID in this page
129    #[serde(skip_serializing_if = "Option::is_none")]
130    pub first_id: Option<String>,
131
132    /// Last ID in this page
133    #[serde(skip_serializing_if = "Option::is_none")]
134    pub last_id: Option<String>,
135
136    /// Whether more data is available
137    #[serde(skip_serializing_if = "Option::is_none")]
138    pub has_more: Option<bool>,
139}
140
141/// Object type for list responses
142#[derive(Debug, Clone, Serialize, Deserialize)]
143#[serde(rename_all = "lowercase")]
144pub enum ListObject {
145    /// List marker
146    List,
147}