zai_rs/knowledge/
document_list.rs

1use url::Url;
2
3use super::types::DocumentListResponse;
4use crate::client::http::HttpClient;
5
6/// Query parameters for listing documents under a knowledge base
7#[derive(Debug, Clone, serde::Serialize, validator::Validate)]
8pub struct DocumentListQuery {
9    /// Knowledge base id (required)
10    #[validate(length(min = 1))]
11    pub knowledge_id: String,
12    /// Page index (default 1)
13    #[serde(skip_serializing_if = "Option::is_none")]
14    #[validate(range(min = 1))]
15    pub page: Option<u32>,
16    /// Page size (default 10)
17    #[serde(skip_serializing_if = "Option::is_none")]
18    #[validate(range(min = 1))]
19    pub size: Option<u32>,
20    /// Document name filter
21    #[serde(skip_serializing_if = "Option::is_none")]
22    #[validate(length(min = 1))]
23    pub word: Option<String>,
24}
25
26impl DocumentListQuery {
27    pub fn new(knowledge_id: impl Into<String>) -> Self {
28        Self {
29            knowledge_id: knowledge_id.into(),
30            page: Some(1),
31            size: Some(10),
32            word: None,
33        }
34    }
35    pub fn with_page(mut self, page: u32) -> Self {
36        self.page = Some(page);
37        self
38    }
39    pub fn with_size(mut self, size: u32) -> Self {
40        self.size = Some(size);
41        self
42    }
43    pub fn with_word(mut self, word: impl Into<String>) -> Self {
44        self.word = Some(word.into());
45        self
46    }
47}
48
49/// Document list request (GET /llm-application/open/document)
50pub struct DocumentListRequest {
51    /// Bearer API key
52    pub key: String,
53    url: String,
54    _body: (),
55}
56
57impl DocumentListRequest {
58    pub fn new(key: String) -> Self {
59        let url = "https://open.bigmodel.cn/api/llm-application/open/document".to_string();
60        Self {
61            key,
62            url,
63            _body: (),
64        }
65    }
66
67    fn rebuild_url(&mut self, q: &DocumentListQuery) {
68        let mut url =
69            Url::parse("https://open.bigmodel.cn/api/llm-application/open/document").unwrap();
70        {
71            let mut pairs = url.query_pairs_mut();
72            pairs.append_pair("knowledge_id", &q.knowledge_id);
73            if let Some(page) = q.page.as_ref() {
74                pairs.append_pair("page", &page.to_string());
75            }
76            if let Some(size) = q.size.as_ref() {
77                pairs.append_pair("size", &size.to_string());
78            }
79            if let Some(word) = q.word.as_ref() {
80                pairs.append_pair("word", word);
81            }
82        }
83        self.url = url.to_string();
84    }
85
86    /// Apply query by rebuilding internal URL
87    pub fn with_query(mut self, q: DocumentListQuery) -> Self {
88        self.rebuild_url(&q);
89        self
90    }
91
92    /// Validate query, rebuild URL, then send
93    pub async fn send_with_query(
94        mut self,
95        q: &DocumentListQuery,
96    ) -> anyhow::Result<DocumentListResponse> {
97        use validator::Validate;
98        q.validate()?;
99        self.rebuild_url(q);
100        self.send().await
101    }
102
103    /// Send and parse typed response
104    pub async fn send(&self) -> anyhow::Result<DocumentListResponse> {
105        let resp = self.get().await?;
106        let parsed = resp.json::<DocumentListResponse>().await?;
107        Ok(parsed)
108    }
109}
110
111impl HttpClient for DocumentListRequest {
112    type Body = ();
113    type ApiUrl = String;
114    type ApiKey = String;
115
116    fn api_url(&self) -> &Self::ApiUrl {
117        &self.url
118    }
119    fn api_key(&self) -> &Self::ApiKey {
120        &self.key
121    }
122    fn body(&self) -> &Self::Body {
123        &self._body
124    }
125}