1use serde::{Deserialize, Serialize};
2use url::Url;
3use validator::Validate;
4
5use super::types::BatchItem;
6use crate::{ZaiResult, client::http::HttpClient};
7
8#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
10pub struct BatchesListQuery {
11 #[serde(skip_serializing_if = "Option::is_none")]
13 pub after: Option<String>,
14
15 #[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 pub fn new() -> Self {
30 Self {
31 after: None,
32 limit: None,
33 }
34 }
35
36 pub fn with_after(mut self, after: impl Into<String>) -> Self {
38 self.after = Some(after.into());
39 self
40 }
41
42 pub fn with_limit(mut self, limit: u32) -> Self {
44 self.limit = Some(limit);
45 self
46 }
47}
48
49pub struct BatchesListRequest {
51 pub key: String,
53 url: String,
55 _body: (),
57}
58
59impl BatchesListRequest {
60 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 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 pub fn with_query(mut self, q: BatchesListQuery) -> Self {
87 self.rebuild_url(&q);
88 self
89 }
90
91 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#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
119pub struct BatchesListResponse {
120 #[serde(skip_serializing_if = "Option::is_none")]
122 pub object: Option<ListObject>,
123
124 #[serde(skip_serializing_if = "Option::is_none")]
126 pub data: Option<Vec<BatchItem>>,
127
128 #[serde(skip_serializing_if = "Option::is_none")]
130 pub first_id: Option<String>,
131
132 #[serde(skip_serializing_if = "Option::is_none")]
134 pub last_id: Option<String>,
135
136 #[serde(skip_serializing_if = "Option::is_none")]
138 pub has_more: Option<bool>,
139}
140
141#[derive(Debug, Clone, Serialize, Deserialize)]
143#[serde(rename_all = "lowercase")]
144pub enum ListObject {
145 List,
147}