Skip to main content

zai_rs/knowledge/
document_retrieve.rs

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