use serde::{Deserialize, Serialize};
use url::Url;
use validator::Validate;
use super::types::BatchItem;
use crate::{ZaiResult, client::http::HttpClient};
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct BatchesListQuery {
#[serde(skip_serializing_if = "Option::is_none")]
pub after: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[validate(range(min = 1, max = 100))]
pub limit: Option<u32>,
}
impl Default for BatchesListQuery {
fn default() -> Self {
Self::new()
}
}
impl BatchesListQuery {
pub fn new() -> Self {
Self {
after: None,
limit: None,
}
}
pub fn with_after(mut self, after: impl Into<String>) -> Self {
self.after = Some(after.into());
self
}
pub fn with_limit(mut self, limit: u32) -> Self {
self.limit = Some(limit);
self
}
}
pub struct BatchesListRequest {
pub key: String,
url: String,
_body: (),
}
impl BatchesListRequest {
pub fn new(key: String) -> Self {
let url = "https://open.bigmodel.cn/api/paas/v4/batches".to_string();
Self {
key,
url,
_body: (),
}
}
fn rebuild_url(&mut self, q: &BatchesListQuery) {
let mut url = Url::parse("https://open.bigmodel.cn/api/paas/v4/batches").unwrap();
{
let mut pairs = url.query_pairs_mut();
if let Some(after) = q.after.as_ref() {
pairs.append_pair("after", after);
}
if let Some(limit) = q.limit.as_ref() {
pairs.append_pair("limit", &limit.to_string());
}
}
self.url = url.to_string();
}
pub fn with_query(mut self, q: BatchesListQuery) -> Self {
self.rebuild_url(&q);
self
}
pub async fn send(&self) -> ZaiResult<BatchesListResponse> {
let resp: reqwest::Response = self.get().await?;
let parsed = resp.json::<BatchesListResponse>().await?;
Ok(parsed)
}
}
impl HttpClient for BatchesListRequest {
type Body = ();
type ApiUrl = String;
type ApiKey = String;
fn api_url(&self) -> &Self::ApiUrl {
&self.url
}
fn api_key(&self) -> &Self::ApiKey {
&self.key
}
fn body(&self) -> &Self::Body {
&self._body
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct BatchesListResponse {
#[serde(skip_serializing_if = "Option::is_none")]
pub object: Option<ListObject>,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Vec<BatchItem>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub first_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub last_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub has_more: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ListObject {
List,
}