1use url::Url;
2
3use super::types::KnowledgeListResponse;
4use crate::client::http::HttpClient;
5
6#[derive(Debug, Clone, Default, serde::Serialize, validator::Validate)]
8pub struct KnowledgeListQuery {
9 #[serde(skip_serializing_if = "Option::is_none")]
11 #[validate(range(min = 1))]
12 pub page: Option<u32>,
13 #[serde(skip_serializing_if = "Option::is_none")]
15 #[validate(range(min = 1))]
16 pub size: Option<u32>,
17}
18
19impl KnowledgeListQuery {
20 pub fn new() -> Self {
21 Self {
22 page: Some(1),
23 size: Some(10),
24 }
25 }
26 pub fn with_page(mut self, page: u32) -> Self {
27 self.page = Some(page);
28 self
29 }
30 pub fn with_size(mut self, size: u32) -> Self {
31 self.size = Some(size);
32 self
33 }
34}
35
36pub struct KnowledgeListRequest {
38 pub key: String,
40 url: String,
41 _body: (),
42}
43
44impl KnowledgeListRequest {
45 pub fn new(key: String) -> Self {
46 let url = "https://open.bigmodel.cn/api/llm-application/open/knowledge".to_string();
47 Self {
48 key,
49 url,
50 _body: (),
51 }
52 }
53
54 fn rebuild_url(&mut self, q: &KnowledgeListQuery) {
55 let mut url =
56 Url::parse("https://open.bigmodel.cn/api/llm-application/open/knowledge").unwrap();
57 {
58 let mut pairs = url.query_pairs_mut();
59 if let Some(page) = q.page.as_ref() {
60 pairs.append_pair("page", &page.to_string());
61 }
62 if let Some(size) = q.size.as_ref() {
63 pairs.append_pair("size", &size.to_string());
64 }
65 }
66 self.url = url.to_string();
67 }
68
69 pub fn with_query(mut self, q: KnowledgeListQuery) -> Self {
71 self.rebuild_url(&q);
72 self
73 }
74
75 pub async fn send(&self) -> anyhow::Result<KnowledgeListResponse> {
77 let resp = self.get().await?;
78 let parsed = resp.json::<KnowledgeListResponse>().await?;
79 Ok(parsed)
80 }
81
82 pub async fn send_with_query(
84 mut self,
85 q: &KnowledgeListQuery,
86 ) -> anyhow::Result<KnowledgeListResponse> {
87 use validator::Validate;
88 q.validate()?;
89 self.rebuild_url(q);
90 self.send().await
91 }
92}
93
94impl HttpClient for KnowledgeListRequest {
95 type Body = ();
96 type ApiUrl = String;
97 type ApiKey = String;
98
99 fn api_url(&self) -> &Self::ApiUrl {
100 &self.url
101 }
102 fn api_key(&self) -> &Self::ApiKey {
103 &self.key
104 }
105 fn body(&self) -> &Self::Body {
106 &self._body
107 }
108}