open_lark/service/cloud_docs/bitable/v1/app/
copy.rs

1use reqwest::Method;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5    core::{
6        api_req::ApiRequest,
7        api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
8        constants::AccessTokenType,
9        http::Transport,
10        req_option::RequestOption,
11        SDKResult,
12    },
13    impl_executable_builder_owned,
14};
15
16use super::AppService;
17
18impl AppService {
19    /// 复制多维表格
20    pub async fn copy(
21        &self,
22        request: CopyAppRequest,
23        option: Option<RequestOption>,
24    ) -> SDKResult<BaseResponse<CopyAppResponse>> {
25        let mut api_req = request.api_request;
26        api_req.http_method = Method::POST;
27        api_req.api_path = format!("/open-apis/bitable/v1/apps/{}/copy", request.app_token);
28        api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
29        api_req.body = serde_json::to_vec(&CopyAppRequestBody {
30            name: request.name,
31            folder_token: request.folder_token,
32            time_zone: request.time_zone,
33        })?;
34
35        let api_resp = Transport::request(api_req, &self.config, option).await?;
36        Ok(api_resp)
37    }
38}
39
40/// 复制多维表格请求
41#[derive(Debug, Default)]
42pub struct CopyAppRequest {
43    api_request: ApiRequest,
44    /// 待复制的多维表格的 app_token
45    app_token: String,
46    /// 复制的多维表格 App 名字
47    name: Option<String>,
48    /// 复制的多维表格所在文件夹的 token
49    folder_token: Option<String>,
50    /// 时区
51    time_zone: Option<String>,
52}
53
54impl CopyAppRequest {
55    pub fn builder() -> CopyAppRequestBuilder {
56        CopyAppRequestBuilder::default()
57    }
58}
59
60#[derive(Default)]
61pub struct CopyAppRequestBuilder {
62    request: CopyAppRequest,
63}
64
65impl CopyAppRequestBuilder {
66    /// 待复制的多维表格的 app_token
67    pub fn app_token(mut self, app_token: impl ToString) -> Self {
68        self.request.app_token = app_token.to_string();
69        self
70    }
71
72    /// 复制的多维表格 App 名字
73    pub fn name(mut self, name: impl ToString) -> Self {
74        self.request.name = Some(name.to_string());
75        self
76    }
77
78    /// 复制的多维表格所在文件夹的 token
79    pub fn folder_token(mut self, folder_token: impl ToString) -> Self {
80        self.request.folder_token = Some(folder_token.to_string());
81        self
82    }
83
84    /// 时区
85    pub fn time_zone(mut self, time_zone: impl ToString) -> Self {
86        self.request.time_zone = Some(time_zone.to_string());
87        self
88    }
89
90    pub fn build(self) -> CopyAppRequest {
91        self.request
92    }
93}
94
95impl_executable_builder_owned!(
96    CopyAppRequestBuilder,
97    AppService,
98    CopyAppRequest,
99    BaseResponse<CopyAppResponse>,
100    copy
101);
102
103#[derive(Serialize)]
104struct CopyAppRequestBody {
105    #[serde(skip_serializing_if = "Option::is_none")]
106    name: Option<String>,
107    #[serde(skip_serializing_if = "Option::is_none")]
108    folder_token: Option<String>,
109    #[serde(skip_serializing_if = "Option::is_none")]
110    time_zone: Option<String>,
111}
112
113#[derive(Deserialize, Debug)]
114pub struct CopyAppResponse {
115    /// 复制的多维表格的 app 信息
116    pub app: CopyAppResponseData,
117}
118
119#[derive(Deserialize, Debug)]
120pub struct CopyAppResponseData {
121    /// 多维表格的 app_token
122    pub app_token: String,
123    /// 多维表格的名字
124    pub name: String,
125    /// 多维表格的版本号
126    pub revision: i32,
127    /// 多维表格的链接
128    pub url: String,
129}
130
131impl ApiResponseTrait for CopyAppResponse {
132    fn data_format() -> ResponseFormat {
133        ResponseFormat::Data
134    }
135}
136
137#[cfg(test)]
138mod tests {
139    use super::*;
140    use serde_json::json;
141
142    #[test]
143    fn test_copy_app_request() {
144        let request = CopyAppRequest::builder()
145            .app_token("bascnmBA*****yGehy8")
146            .name("复制的多维表格")
147            .folder_token("fldcnmBA*****yGehy8")
148            .time_zone("Asia/Shanghai")
149            .build();
150
151        assert_eq!(request.app_token, "bascnmBA*****yGehy8");
152        assert_eq!(request.name, Some("复制的多维表格".to_string()));
153        assert_eq!(
154            request.folder_token,
155            Some("fldcnmBA*****yGehy8".to_string())
156        );
157        assert_eq!(request.time_zone, Some("Asia/Shanghai".to_string()));
158    }
159
160    #[test]
161    fn test_copy_app_request_body_serialization() {
162        let body = CopyAppRequestBody {
163            name: Some("复制的多维表格".to_string()),
164            folder_token: Some("fldcnmBA*****yGehy8".to_string()),
165            time_zone: Some("Asia/Shanghai".to_string()),
166        };
167
168        let serialized = serde_json::to_value(&body).unwrap();
169        let expected = json!({
170            "name": "复制的多维表格",
171            "folder_token": "fldcnmBA*****yGehy8",
172            "time_zone": "Asia/Shanghai"
173        });
174
175        assert_eq!(serialized, expected);
176    }
177}