open_lark/service/cloud_docs/wiki/v2/space_node/
create.rs1use reqwest::Method;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5 core::{
6 api_req::ApiRequest,
7 api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
8 config::Config,
9 constants::AccessTokenType,
10 http::Transport,
11 req_option::RequestOption,
12 SDKResult,
13 },
14 impl_executable_builder_owned,
15};
16
17#[derive(Debug, Serialize, Default)]
19pub struct CreateSpaceNodeRequest {
20 #[serde(skip)]
21 api_request: ApiRequest,
22 #[serde(skip)]
24 space_id: String,
25 obj_type: String,
27 #[serde(skip_serializing_if = "Option::is_none")]
29 parent_node_token: Option<String>,
30 #[serde(skip_serializing_if = "Option::is_none")]
32 node_type: Option<String>,
33 #[serde(skip_serializing_if = "Option::is_none")]
35 title: Option<String>,
36}
37
38impl CreateSpaceNodeRequest {
39 pub fn builder() -> CreateSpaceNodeRequestBuilder {
40 CreateSpaceNodeRequestBuilder::default()
41 }
42
43 pub fn new(space_id: impl ToString, obj_type: impl ToString) -> Self {
44 Self {
45 space_id: space_id.to_string(),
46 obj_type: obj_type.to_string(),
47 ..Default::default()
48 }
49 }
50}
51
52#[derive(Default)]
53pub struct CreateSpaceNodeRequestBuilder {
54 request: CreateSpaceNodeRequest,
55}
56
57impl CreateSpaceNodeRequestBuilder {
58 pub fn space_id(mut self, space_id: impl ToString) -> Self {
60 self.request.space_id = space_id.to_string();
61 self
62 }
63
64 pub fn obj_type(mut self, obj_type: impl ToString) -> Self {
66 self.request.obj_type = obj_type.to_string();
67 self
68 }
69
70 pub fn with_doc_type(mut self) -> Self {
72 self.request.obj_type = "doc".to_string();
73 self
74 }
75
76 pub fn with_sheet_type(mut self) -> Self {
78 self.request.obj_type = "sheet".to_string();
79 self
80 }
81
82 pub fn with_mindnote_type(mut self) -> Self {
84 self.request.obj_type = "mindnote".to_string();
85 self
86 }
87
88 pub fn with_bitable_type(mut self) -> Self {
90 self.request.obj_type = "bitable".to_string();
91 self
92 }
93
94 pub fn parent_node_token(mut self, parent_node_token: impl ToString) -> Self {
96 self.request.parent_node_token = Some(parent_node_token.to_string());
97 self
98 }
99
100 pub fn node_type(mut self, node_type: impl ToString) -> Self {
102 self.request.node_type = Some(node_type.to_string());
103 self
104 }
105
106 pub fn with_origin_node(mut self) -> Self {
108 self.request.node_type = Some("origin".to_string());
109 self
110 }
111
112 pub fn with_shortcut_node(mut self) -> Self {
114 self.request.node_type = Some("shortcut".to_string());
115 self
116 }
117
118 pub fn title(mut self, title: impl ToString) -> Self {
120 self.request.title = Some(title.to_string());
121 self
122 }
123
124 pub fn build(mut self) -> CreateSpaceNodeRequest {
125 self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
126 self.request
127 }
128}
129
130impl_executable_builder_owned!(
131 CreateSpaceNodeRequestBuilder,
132 crate::service::cloud_docs::wiki::v2::space_node::SpaceNodeService,
133 CreateSpaceNodeRequest,
134 CreateSpaceNodeResponse,
135 create
136);
137
138#[derive(Debug, Deserialize)]
140pub struct CreatedNode {
141 pub space_id: String,
143 pub node_token: String,
145 pub obj_type: String,
147 pub parent_node_token: Option<String>,
149 pub node_type: Option<String>,
151 pub obj_token: Option<String>,
153 pub title: Option<String>,
155}
156
157#[derive(Debug, Deserialize)]
159pub struct CreateSpaceNodeResponse {
160 pub node: CreatedNode,
162}
163
164impl ApiResponseTrait for CreateSpaceNodeResponse {
165 fn data_format() -> ResponseFormat {
166 ResponseFormat::Data
167 }
168}
169
170pub async fn create_space_node(
172 request: CreateSpaceNodeRequest,
173 config: &Config,
174 option: Option<RequestOption>,
175) -> SDKResult<BaseResponse<CreateSpaceNodeResponse>> {
176 let mut api_req = request.api_request;
177 api_req.http_method = Method::POST;
178 api_req.api_path = format!("/open-apis/wiki/v2/spaces/{}/nodes", request.space_id);
179 api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
180
181 let api_resp = Transport::request(api_req, config, option).await?;
182 Ok(api_resp)
183}
184
185#[cfg(test)]
186mod tests {
187 use super::*;
188
189 #[test]
190 fn test_create_space_node_request_builder() {
191 let request = CreateSpaceNodeRequest::builder()
192 .space_id("spcxxxxxx")
193 .with_doc_type()
194 .parent_node_token("wikcnxxxxxx")
195 .with_origin_node()
196 .title("我的文档")
197 .build();
198
199 assert_eq!(request.space_id, "spcxxxxxx");
200 assert_eq!(request.obj_type, "doc");
201 assert_eq!(request.parent_node_token, Some("wikcnxxxxxx".to_string()));
202 assert_eq!(request.node_type, Some("origin".to_string()));
203 assert_eq!(request.title, Some("我的文档".to_string()));
204 }
205}