open_lark/service/cloud_docs/wiki/v2/space_node/
copy.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 CopySpaceNodeRequest {
20 #[serde(skip)]
21 api_request: ApiRequest,
22 #[serde(skip)]
24 space_id: String,
25 #[serde(skip)]
27 node_token: String,
28 #[serde(skip_serializing_if = "Option::is_none")]
30 target_parent_token: Option<String>,
31 #[serde(skip_serializing_if = "Option::is_none")]
33 target_space_id: Option<String>,
34 #[serde(skip_serializing_if = "Option::is_none")]
36 title: Option<String>,
37}
38
39impl CopySpaceNodeRequest {
40 pub fn builder() -> CopySpaceNodeRequestBuilder {
41 CopySpaceNodeRequestBuilder::default()
42 }
43
44 pub fn new(space_id: impl ToString, node_token: impl ToString) -> Self {
45 Self {
46 space_id: space_id.to_string(),
47 node_token: node_token.to_string(),
48 ..Default::default()
49 }
50 }
51}
52
53#[derive(Default)]
54pub struct CopySpaceNodeRequestBuilder {
55 request: CopySpaceNodeRequest,
56}
57
58impl CopySpaceNodeRequestBuilder {
59 pub fn space_id(mut self, space_id: impl ToString) -> Self {
61 self.request.space_id = space_id.to_string();
62 self
63 }
64
65 pub fn node_token(mut self, node_token: impl ToString) -> Self {
67 self.request.node_token = node_token.to_string();
68 self
69 }
70
71 pub fn target_parent_token(mut self, target_parent_token: impl ToString) -> Self {
73 self.request.target_parent_token = Some(target_parent_token.to_string());
74 self
75 }
76
77 pub fn move_to_root(mut self) -> Self {
79 self.request.target_parent_token = None;
80 self
81 }
82
83 pub fn target_space_id(mut self, target_space_id: impl ToString) -> Self {
85 self.request.target_space_id = Some(target_space_id.to_string());
86 self
87 }
88
89 pub fn move_to_current_space(mut self) -> Self {
91 self.request.target_space_id = None;
92 self
93 }
94
95 pub fn title(mut self, title: impl ToString) -> Self {
97 self.request.title = Some(title.to_string());
98 self
99 }
100
101 pub fn keep_original_title(mut self) -> Self {
103 self.request.title = None;
104 self
105 }
106
107 pub fn build(mut self) -> CopySpaceNodeRequest {
108 self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
109 self.request
110 }
111}
112
113impl_executable_builder_owned!(
114 CopySpaceNodeRequestBuilder,
115 crate::service::cloud_docs::wiki::v2::space_node::SpaceNodeService,
116 CopySpaceNodeRequest,
117 CopySpaceNodeResponse,
118 copy
119);
120
121#[derive(Debug, Deserialize)]
123pub struct CopiedNode {
124 pub space_id: String,
126 pub node_token: String,
128 pub obj_type: String,
130 pub parent_node_token: Option<String>,
132 pub node_type: Option<String>,
134 pub obj_token: Option<String>,
136 pub title: Option<String>,
138}
139
140#[derive(Debug, Deserialize)]
142pub struct CopySpaceNodeResponse {
143 pub node: CopiedNode,
145}
146
147impl ApiResponseTrait for CopySpaceNodeResponse {
148 fn data_format() -> ResponseFormat {
149 ResponseFormat::Data
150 }
151}
152
153pub async fn copy_space_node(
155 request: CopySpaceNodeRequest,
156 config: &Config,
157 option: Option<RequestOption>,
158) -> SDKResult<BaseResponse<CopySpaceNodeResponse>> {
159 let mut api_req = request.api_request;
160 api_req.http_method = Method::POST;
161 api_req.api_path = format!(
162 "/open-apis/wiki/v2/spaces/{}/nodes/{}/copy",
163 request.space_id, request.node_token
164 );
165 api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
166
167 let api_resp = Transport::request(api_req, config, option).await?;
168 Ok(api_resp)
169}
170
171#[cfg(test)]
172mod tests {
173 use super::*;
174
175 #[test]
176 fn test_copy_space_node_request_builder() {
177 let request = CopySpaceNodeRequest::builder()
178 .space_id("spcxxxxxx")
179 .node_token("wikcnxxxxxx")
180 .target_parent_token("wikcnyyyyyyy")
181 .target_space_id("spcyyyyyy")
182 .title("复制的文档")
183 .build();
184
185 assert_eq!(request.space_id, "spcxxxxxx");
186 assert_eq!(request.node_token, "wikcnxxxxxx");
187 assert_eq!(
188 request.target_parent_token,
189 Some("wikcnyyyyyyy".to_string())
190 );
191 assert_eq!(request.target_space_id, Some("spcyyyyyy".to_string()));
192 assert_eq!(request.title, Some("复制的文档".to_string()));
193 }
194
195 #[test]
196 fn test_copy_to_root_current_space() {
197 let request = CopySpaceNodeRequest::builder()
198 .space_id("spcxxxxxx")
199 .node_token("wikcnxxxxxx")
200 .move_to_root()
201 .move_to_current_space()
202 .keep_original_title()
203 .build();
204
205 assert_eq!(request.space_id, "spcxxxxxx");
206 assert_eq!(request.node_token, "wikcnxxxxxx");
207 assert_eq!(request.target_parent_token, None);
208 assert_eq!(request.target_space_id, None);
209 assert_eq!(request.title, None);
210 }
211}