Skip to main content

zai_rs/knowledge/
retrieve.rs

1use std::sync::Arc;
2
3use super::types::KnowledgeDetailResponse;
4use crate::{
5    ZaiResult,
6    client::{
7        endpoints::{ApiBase, EndpointConfig, join_url, paths},
8        http::{HttpClient, HttpClientConfig, parse_typed_response},
9    },
10};
11
12/// Knowledge detail request (GET /llm-application/open/knowledge/{id})
13pub struct KnowledgeRetrieveRequest {
14    /// Bearer API key
15    pub key: String,
16    url: String,
17    endpoint_config: EndpointConfig,
18    api_base: ApiBase,
19    id: String,
20    http_config: Arc<HttpClientConfig>,
21    _body: (),
22}
23
24impl KnowledgeRetrieveRequest {
25    /// Build a retrieve request with id
26    pub fn new(key: String, id: impl AsRef<str>) -> Self {
27        let id = id.as_ref().to_string();
28        let endpoint_config = EndpointConfig::default();
29        let api_base = ApiBase::LlmApplication;
30        let url = endpoint_config.url(&api_base, &join_url(paths::KNOWLEDGE, &id));
31        Self {
32            key,
33            url,
34            endpoint_config,
35            api_base,
36            id,
37            http_config: Arc::new(HttpClientConfig::default()),
38            _body: (),
39        }
40    }
41
42    fn rebuild_url(&mut self) {
43        self.url = self
44            .endpoint_config
45            .url(&self.api_base, &join_url(paths::KNOWLEDGE, &self.id));
46    }
47
48    pub fn with_base_url(mut self, base: impl Into<String>) -> Self {
49        self.api_base = ApiBase::Custom(base.into());
50        self.rebuild_url();
51        self
52    }
53
54    pub fn with_endpoint_config(mut self, endpoint_config: EndpointConfig) -> Self {
55        self.endpoint_config = endpoint_config;
56        self.rebuild_url();
57        self
58    }
59
60    pub fn with_http_config(mut self, config: HttpClientConfig) -> Self {
61        self.http_config = Arc::new(config);
62        self
63    }
64
65    /// Send and parse typed response
66    pub async fn send(&self) -> ZaiResult<KnowledgeDetailResponse> {
67        let resp = self.get().await?;
68        let parsed = parse_typed_response::<KnowledgeDetailResponse>(resp).await?;
69        Ok(parsed)
70    }
71}
72
73impl HttpClient for KnowledgeRetrieveRequest {
74    type Body = ();
75    type ApiUrl = String;
76    type ApiKey = String;
77
78    fn api_url(&self) -> &Self::ApiUrl {
79        &self.url
80    }
81    fn api_key(&self) -> &Self::ApiKey {
82        &self.key
83    }
84    fn body(&self) -> &Self::Body {
85        &self._body
86    }
87
88    fn http_config(&self) -> Arc<HttpClientConfig> {
89        Arc::clone(&self.http_config)
90    }
91}
92
93/// Alias for symmetry with other modules
94pub type KnowledgeRetrieveResponse = KnowledgeDetailResponse;