use std::sync::Arc;
use serde::{Deserialize, Serialize};
use validator::Validate;
use super::types::BatchItem;
use crate::{
ZaiResult,
client::{
endpoints::{ApiBase, EndpointConfig, build_query, paths},
http::{HttpClient, HttpClientConfig, parse_typed_response},
},
};
#[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,
endpoint_config: EndpointConfig,
api_base: ApiBase,
http_config: Arc<HttpClientConfig>,
query: BatchesListQuery,
_body: (),
}
impl BatchesListRequest {
pub fn new(key: String) -> Self {
let endpoint_config = EndpointConfig::default();
let api_base = ApiBase::PaasV4;
let url = endpoint_config.url(&api_base, paths::BATCHES);
Self {
key,
url,
endpoint_config,
api_base,
http_config: Arc::new(HttpClientConfig::default()),
query: BatchesListQuery::new(),
_body: (),
}
}
fn rebuild_url(&mut self) {
let endpoint = self.endpoint_config.url(&self.api_base, paths::BATCHES);
let mut params: Vec<(&str, String)> = Vec::new();
if let Some(after) = self.query.after.as_ref() {
params.push(("after", after.clone()));
}
if let Some(limit) = self.query.limit.as_ref() {
params.push(("limit", limit.to_string()));
}
self.url = build_query(&endpoint, params);
}
pub fn with_base_url(mut self, base: impl Into<String>) -> Self {
self.api_base = ApiBase::Custom(base.into());
self.rebuild_url();
self
}
pub fn with_endpoint_config(mut self, endpoint_config: EndpointConfig) -> Self {
self.endpoint_config = endpoint_config;
self.rebuild_url();
self
}
pub fn with_http_config(mut self, config: HttpClientConfig) -> Self {
self.http_config = Arc::new(config);
self
}
pub fn with_query(mut self, q: BatchesListQuery) -> Self {
self.query = q;
self.rebuild_url();
self
}
pub async fn send(&self) -> ZaiResult<BatchesListResponse> {
let resp: reqwest::Response = self.get().await?;
let parsed = parse_typed_response::<BatchesListResponse>(resp).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
}
fn http_config(&self) -> Arc<HttpClientConfig> {
Arc::clone(&self.http_config)
}
}
#[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,
}