zai_rs/knowledge/
document_upload_url.rs1use serde::{Deserialize, Serialize};
2use validator::Validate;
3
4use super::types::UploadUrlResponse;
5use crate::ZaiResult;
6use crate::client::ZaiClient;
7
8#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
10pub struct UploadUrlDetail {
11 #[validate(url)]
13 pub url: String,
14 #[serde(skip_serializing_if = "Option::is_none")]
16 pub knowledge_type: Option<i64>,
17 #[serde(skip_serializing_if = "Option::is_none")]
19 pub custom_separator: Option<Vec<String>>,
20 #[serde(skip_serializing_if = "Option::is_none")]
22 #[validate(range(min = 1))]
23 pub sentence_size: Option<u32>,
24 #[serde(skip_serializing_if = "Option::is_none")]
26 #[validate(url)]
27 pub callback_url: Option<String>,
28 #[serde(skip_serializing_if = "Option::is_none")]
30 pub callback_header: Option<std::collections::BTreeMap<String, String>>,
31}
32
33impl UploadUrlDetail {
34 pub fn new(url: impl Into<String>) -> Self {
36 Self {
37 url: url.into(),
38 knowledge_type: None,
39 custom_separator: None,
40 sentence_size: None,
41 callback_url: None,
42 callback_header: None,
43 }
44 }
45 pub fn with_knowledge_type(mut self, t: i64) -> Self {
47 self.knowledge_type = Some(t);
48 self
49 }
50 pub fn with_custom_separator(mut self, seps: Vec<String>) -> Self {
52 self.custom_separator = Some(seps);
53 self
54 }
55 pub fn with_sentence_size(mut self, size: u32) -> Self {
57 self.sentence_size = Some(size);
58 self
59 }
60 pub fn with_callback_url(mut self, url: impl Into<String>) -> Self {
62 self.callback_url = Some(url.into());
63 self
64 }
65 pub fn with_callback_header(
67 mut self,
68 headers: std::collections::BTreeMap<String, String>,
69 ) -> Self {
70 self.callback_header = Some(headers);
71 self
72 }
73}
74
75#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
77pub struct UploadUrlBody {
78 #[validate(length(min = 1))]
80 pub upload_detail: Vec<UploadUrlDetail>,
81 #[validate(length(min = 1))]
83 pub knowledge_id: String,
84}
85
86impl UploadUrlBody {
87 pub fn new(knowledge_id: impl Into<String>) -> Self {
89 Self {
90 upload_detail: Vec::new(),
91 knowledge_id: knowledge_id.into(),
92 }
93 }
94 pub fn add_detail(mut self, detail: UploadUrlDetail) -> Self {
96 self.upload_detail.push(detail);
97 self
98 }
99 pub fn add_url(mut self, url: impl Into<String>) -> Self {
101 self.upload_detail.push(UploadUrlDetail::new(url));
102 self
103 }
104}
105
106pub struct DocumentUploadUrlRequest {
111 body: UploadUrlBody,
112}
113
114impl DocumentUploadUrlRequest {
115 pub fn new(body: UploadUrlBody) -> Self {
117 Self { body }
118 }
119
120 pub fn body_mut(&mut self) -> &mut UploadUrlBody {
122 &mut self.body
123 }
124
125 pub async fn send_via(&self, client: &ZaiClient) -> ZaiResult<UploadUrlResponse> {
127 self.body.validate()?;
128 let route = crate::client::routes::DOCUMENTS_UPLOAD_URL;
129 let url = client.endpoints().resolve_route(route, &[])?;
130 client
131 .send_json::<_, UploadUrlResponse>(route.method(), url, &self.body)
132 .await
133 }
134}