use super::types::DocumentListResponse;
use crate::ZaiResult;
use crate::client::ZaiClient;
#[derive(Debug, Clone, serde::Serialize, validator::Validate, Default)]
#[allow(clippy::new_without_default)]
pub struct DocumentListQuery {
#[validate(length(min = 1))]
pub knowledge_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[validate(range(min = 1))]
pub page: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
#[validate(range(min = 1))]
pub size: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
#[validate(length(min = 1))]
pub word: Option<String>,
}
impl DocumentListQuery {
pub fn new(knowledge_id: impl Into<String>) -> Self {
Self {
knowledge_id: knowledge_id.into(),
page: Some(1),
size: Some(10),
word: None,
}
}
pub fn with_page(mut self, page: u32) -> Self {
self.page = Some(page);
self
}
pub fn with_size(mut self, size: u32) -> Self {
self.size = Some(size);
self
}
pub fn with_word(mut self, word: impl Into<String>) -> Self {
self.word = Some(word.into());
self
}
fn pairs(&self) -> Vec<(&'static str, String)> {
let mut params: Vec<(&'static str, String)> = Vec::new();
params.push(("knowledge_id", self.knowledge_id.clone()));
if let Some(page) = self.page.as_ref() {
params.push(("page", page.to_string()));
}
if let Some(size) = self.size.as_ref() {
params.push(("size", size.to_string()));
}
if let Some(word) = self.word.as_ref() {
params.push(("word", word.clone()));
}
params
}
}
#[allow(clippy::new_without_default)]
pub struct DocumentListRequest {
query: Option<DocumentListQuery>,
}
impl DocumentListRequest {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self { query: None }
}
pub fn with_query(mut self, q: DocumentListQuery) -> Self {
self.query = Some(q);
self
}
pub async fn send_via(&self, client: &ZaiClient) -> ZaiResult<DocumentListResponse> {
let q = self
.query
.as_ref()
.ok_or_else(|| crate::ZaiError::ApiError {
code: crate::client::error::codes::SDK_VALIDATION,
message: "document list requires a knowledge_id; call with_query first".to_string(),
})?;
let params = q.pairs();
let route = crate::client::routes::DOCUMENTS_LIST;
let url = client.endpoints().resolve_route_with_query(
route,
&[],
¶ms
.iter()
.map(|(k, v)| (*k, v.as_str()))
.collect::<Vec<_>>(),
)?;
client
.send_empty::<DocumentListResponse>(route.method(), url)
.await
}
pub async fn send_via_with_query(
mut self,
client: &ZaiClient,
q: &DocumentListQuery,
) -> ZaiResult<DocumentListResponse> {
use validator::Validate;
q.validate()?;
self.query = Some(q.clone());
self.send_via(client).await
}
}