Skip to main content

zai_rs/knowledge/
list.rs

1use std::sync::Arc;
2
3use super::types::KnowledgeListResponse;
4use crate::{
5    ZaiResult,
6    client::{
7        endpoints::{ApiBase, EndpointConfig, build_query, paths},
8        http::{HttpClient, HttpClientConfig, parse_typed_response},
9    },
10};
11
12/// Query parameters for knowledge list API
13#[derive(Debug, Clone, Default, serde::Serialize, validator::Validate)]
14pub struct KnowledgeListQuery {
15    /// Page index starting from 1 (default 1)
16    #[serde(skip_serializing_if = "Option::is_none")]
17    #[validate(range(min = 1))]
18    pub page: Option<u32>,
19    /// Page size (default 10)
20    #[serde(skip_serializing_if = "Option::is_none")]
21    #[validate(range(min = 1))]
22    pub size: Option<u32>,
23}
24
25impl KnowledgeListQuery {
26    pub fn new() -> Self {
27        Self {
28            page: Some(1),
29            size: Some(10),
30        }
31    }
32    pub fn with_page(mut self, page: u32) -> Self {
33        self.page = Some(page);
34        self
35    }
36    pub fn with_size(mut self, size: u32) -> Self {
37        self.size = Some(size);
38        self
39    }
40}
41
42/// Knowledge list request (GET /llm-application/open/knowledge)
43pub struct KnowledgeListRequest {
44    /// Bearer API key
45    pub key: String,
46    url: String,
47    endpoint_config: EndpointConfig,
48    api_base: ApiBase,
49    http_config: Arc<HttpClientConfig>,
50    query: KnowledgeListQuery,
51    _body: (),
52}
53
54impl KnowledgeListRequest {
55    pub fn new(key: String) -> Self {
56        let endpoint_config = EndpointConfig::default();
57        let api_base = ApiBase::LlmApplication;
58        let url = endpoint_config.url(&api_base, paths::KNOWLEDGE);
59        Self {
60            key,
61            url,
62            endpoint_config,
63            api_base,
64            http_config: Arc::new(HttpClientConfig::default()),
65            query: KnowledgeListQuery::new(),
66            _body: (),
67        }
68    }
69
70    fn rebuild_url(&mut self) {
71        let endpoint = self.endpoint_config.url(&self.api_base, paths::KNOWLEDGE);
72        let mut params: Vec<(&str, String)> = Vec::new();
73        if let Some(page) = self.query.page.as_ref() {
74            params.push(("page", page.to_string()));
75        }
76        if let Some(size) = self.query.size.as_ref() {
77            params.push(("size", size.to_string()));
78        }
79        self.url = build_query(&endpoint, params);
80    }
81
82    pub fn with_base_url(mut self, base: impl Into<String>) -> Self {
83        self.api_base = ApiBase::Custom(base.into());
84        self.rebuild_url();
85        self
86    }
87
88    pub fn with_endpoint_config(mut self, endpoint_config: EndpointConfig) -> Self {
89        self.endpoint_config = endpoint_config;
90        self.rebuild_url();
91        self
92    }
93
94    pub fn with_http_config(mut self, config: HttpClientConfig) -> Self {
95        self.http_config = Arc::new(config);
96        self
97    }
98
99    /// Apply query by rebuilding internal URL
100    pub fn with_query(mut self, q: KnowledgeListQuery) -> Self {
101        self.query = q;
102        self.rebuild_url();
103        self
104    }
105
106    /// Send request and parse typed response
107    pub async fn send(&self) -> ZaiResult<KnowledgeListResponse> {
108        let resp = self.get().await?;
109        let parsed = parse_typed_response::<KnowledgeListResponse>(resp).await?;
110        Ok(parsed)
111    }
112
113    /// Validate query, rebuild URL then send
114    pub async fn send_with_query(
115        mut self,
116        q: &KnowledgeListQuery,
117    ) -> ZaiResult<KnowledgeListResponse> {
118        use validator::Validate;
119        q.validate()?;
120        self.query = q.clone();
121        self.rebuild_url();
122        self.send().await
123    }
124}
125
126impl HttpClient for KnowledgeListRequest {
127    type Body = ();
128    type ApiUrl = String;
129    type ApiKey = String;
130
131    fn api_url(&self) -> &Self::ApiUrl {
132        &self.url
133    }
134    fn api_key(&self) -> &Self::ApiKey {
135        &self.key
136    }
137    fn body(&self) -> &Self::Body {
138        &self._body
139    }
140
141    fn http_config(&self) -> Arc<HttpClientConfig> {
142        Arc::clone(&self.http_config)
143    }
144}