zai_rs/batches/
list.rs

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