Skip to main content

zai_rs/knowledge/
document_delete.rs

1use std::sync::Arc;
2
3use crate::client::{
4    endpoints::{ApiBase, EndpointConfig, join_url, paths},
5    http::{HttpClient, HttpClientConfig, parse_typed_response},
6};
7
8/// Document delete request (DELETE /llm-application/open/document/{id})
9pub struct DocumentDeleteRequest {
10    /// Bearer API key
11    pub key: String,
12    url: String,
13    endpoint_config: EndpointConfig,
14    api_base: ApiBase,
15    id: String,
16    http_config: Arc<HttpClientConfig>,
17    _body: (),
18}
19
20impl DocumentDeleteRequest {
21    /// Build a delete request with target document id
22    pub fn new(key: String, id: impl AsRef<str>) -> Self {
23        let id = id.as_ref().to_string();
24        let endpoint_config = EndpointConfig::default();
25        let api_base = ApiBase::LlmApplication;
26        let url = endpoint_config.url(&api_base, &join_url(paths::DOCUMENT, &id));
27        Self {
28            key,
29            url,
30            endpoint_config,
31            api_base,
32            id,
33            http_config: Arc::new(HttpClientConfig::default()),
34            _body: (),
35        }
36    }
37
38    fn rebuild_url(&mut self) {
39        self.url = self
40            .endpoint_config
41            .url(&self.api_base, &join_url(paths::DOCUMENT, &self.id));
42    }
43
44    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
45        self.api_base = ApiBase::Custom(base_url.into());
46        self.rebuild_url();
47        self
48    }
49
50    pub fn with_endpoint_config(mut self, endpoint_config: EndpointConfig) -> Self {
51        self.endpoint_config = endpoint_config;
52        self.rebuild_url();
53        self
54    }
55
56    pub fn with_http_config(mut self, config: HttpClientConfig) -> Self {
57        self.http_config = Arc::new(config);
58        self
59    }
60
61    pub fn delete(
62        &self,
63    ) -> impl std::future::Future<Output = crate::ZaiResult<reqwest::Response>> + Send {
64        HttpClient::delete(self)
65    }
66
67    /// Send delete request and parse typed response
68    pub async fn send(&self) -> crate::ZaiResult<DocumentDeleteResponse> {
69        let resp = self.delete().await?;
70
71        parse_typed_response::<DocumentDeleteResponse>(resp).await
72    }
73}
74
75impl HttpClient for DocumentDeleteRequest {
76    type Body = (); // unused
77    type ApiUrl = String;
78    type ApiKey = String;
79
80    fn api_url(&self) -> &Self::ApiUrl {
81        &self.url
82    }
83    fn api_key(&self) -> &Self::ApiKey {
84        &self.key
85    }
86    fn body(&self) -> &Self::Body {
87        &self._body
88    }
89
90    fn http_config(&self) -> Arc<HttpClientConfig> {
91        self.http_config.clone()
92    }
93}
94
95/// Delete response envelope without data
96#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, validator::Validate)]
97pub struct DocumentDeleteResponse {
98    #[serde(skip_serializing_if = "Option::is_none")]
99    pub code: Option<i64>,
100    #[serde(skip_serializing_if = "Option::is_none")]
101    pub message: Option<String>,
102    #[serde(skip_serializing_if = "Option::is_none")]
103    pub timestamp: Option<u64>,
104}