1use serde::{Deserialize, Serialize};
2use url::Url;
3use validator::Validate;
4
5use crate::client::http::HttpClient;
6
7use super::types::BatchItem;
8
9#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
11pub struct BatchesListQuery {
12 #[serde(skip_serializing_if = "Option::is_none")]
14 pub after: Option<String>,
15
16 #[serde(skip_serializing_if = "Option::is_none")]
18 #[validate(range(min = 1, max = 100))]
19 pub limit: Option<u32>,
20}
21
22impl BatchesListQuery {
23 pub fn new() -> Self {
25 Self {
26 after: None,
27 limit: None,
28 }
29 }
30
31 pub fn with_after(mut self, after: impl Into<String>) -> Self {
33 self.after = Some(after.into());
34 self
35 }
36
37 pub fn with_limit(mut self, limit: u32) -> Self {
39 self.limit = Some(limit);
40 self
41 }
42}
43
44pub struct BatchesListRequest {
46 pub key: String,
48 url: String,
50 _body: (),
52}
53
54impl BatchesListRequest {
55 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 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 pub fn with_query(mut self, q: BatchesListQuery) -> Self {
82 self.rebuild_url(&q);
83 self
84 }
85
86 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#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
112pub struct BatchesListResponse {
113 #[serde(skip_serializing_if = "Option::is_none")]
115 pub object: Option<ListObject>,
116
117 #[serde(skip_serializing_if = "Option::is_none")]
119 pub data: Option<Vec<BatchItem>>,
120
121 #[serde(skip_serializing_if = "Option::is_none")]
123 pub first_id: Option<String>,
124
125 #[serde(skip_serializing_if = "Option::is_none")]
127 pub last_id: Option<String>,
128
129 #[serde(skip_serializing_if = "Option::is_none")]
131 pub has_more: Option<bool>,
132}
133
134#[derive(Debug, Clone, Serialize, Deserialize)]
136#[serde(rename_all = "lowercase")]
137pub enum ListObject {
138 List,
140}