open_lark/service/cloud_docs/bitable/v1/app_table/
batch_create.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        endpoints::cloud_docs::*,
10        http::Transport,
11        req_option::RequestOption,
12        SDKResult,
13    },
14    impl_executable_builder_owned,
15};
16
17use super::{AppTableService, TableData};
18
19impl AppTableService {
20    /// 新增多个数据表
21    pub async fn batch_create(
22        &self,
23        request: BatchCreateTablesRequest,
24        option: Option<RequestOption>,
25    ) -> SDKResult<BaseResponse<BatchCreateTablesResponse>> {
26        let mut api_req = request.api_request;
27        api_req.http_method = Method::POST;
28        api_req.api_path =
29            BITABLE_V1_TABLES_BATCH_CREATE.replace("{app_token}", &request.app_token);
30        api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
31        api_req.body = serde_json::to_vec(&BatchCreateTablesRequestBody {
32            tables: request.tables,
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 BatchCreateTablesRequest {
43    api_request: ApiRequest,
44    /// 多维表格的 app_token
45    app_token: String,
46    /// 数据表信息列表
47    tables: Vec<TableData>,
48}
49
50impl BatchCreateTablesRequest {
51    pub fn builder() -> BatchCreateTablesRequestBuilder {
52        BatchCreateTablesRequestBuilder::default()
53    }
54
55    /// 创建批量新增数据表请求
56    pub fn new(app_token: impl ToString, tables: Vec<TableData>) -> Self {
57        Self {
58            api_request: ApiRequest::default(),
59            app_token: app_token.to_string(),
60            tables,
61        }
62    }
63}
64
65#[derive(Default)]
66pub struct BatchCreateTablesRequestBuilder {
67    request: BatchCreateTablesRequest,
68}
69
70impl BatchCreateTablesRequestBuilder {
71    /// 多维表格的 app_token
72    pub fn app_token(mut self, app_token: impl ToString) -> Self {
73        self.request.app_token = app_token.to_string();
74        self
75    }
76
77    /// 数据表信息列表
78    pub fn tables(mut self, tables: Vec<TableData>) -> Self {
79        self.request.tables = tables;
80        self
81    }
82
83    /// 添加单个数据表
84    pub fn add_table(mut self, table: TableData) -> Self {
85        self.request.tables.push(table);
86        self
87    }
88
89    pub fn build(self) -> BatchCreateTablesRequest {
90        self.request
91    }
92}
93
94impl_executable_builder_owned!(
95    BatchCreateTablesRequestBuilder,
96    AppTableService,
97    BatchCreateTablesRequest,
98    BaseResponse<BatchCreateTablesResponse>,
99    batch_create
100);
101
102#[derive(Serialize)]
103struct BatchCreateTablesRequestBody {
104    tables: Vec<TableData>,
105}
106
107#[derive(Deserialize, Debug)]
108pub struct BatchCreateTablesResponse {
109    /// 数据表信息列表
110    pub table_ids: Vec<String>,
111}
112
113impl ApiResponseTrait for BatchCreateTablesResponse {
114    fn data_format() -> ResponseFormat {
115        ResponseFormat::Data
116    }
117}
118
119#[cfg(test)]
120#[allow(unused_variables, unused_unsafe)]
121mod tests {
122    use super::*;
123    use crate::service::bitable::v1::app_table::TableField;
124
125    #[test]
126    fn test_batch_create_tables_request() {
127        let table1 = TableData::new("用户表")
128            .with_fields(vec![TableField::text("姓名"), TableField::text("邮箱")]);
129
130        let table2 = TableData::new("订单表").with_fields(vec![
131            TableField::text("订单号"),
132            TableField::number("金额"),
133            TableField::single_select("状态", vec!["待支付".to_string(), "已支付".to_string()]),
134        ]);
135
136        let request = BatchCreateTablesRequest::builder()
137            .app_token("bascnmBA*****yGehy8")
138            .add_table(table1)
139            .add_table(table2)
140            .build();
141
142        assert_eq!(request.app_token, "bascnmBA*****yGehy8");
143        assert_eq!(request.tables.len(), 2);
144        assert_eq!(request.tables[0].name, "用户表");
145        assert_eq!(request.tables[1].name, "订单表");
146    }
147
148    #[test]
149    fn test_batch_create_tables_with_vec() {
150        let tables = vec![
151            TableData::new("表格1"),
152            TableData::new("表格2"),
153            TableData::new("表格3"),
154        ];
155
156        let request = BatchCreateTablesRequest::new("bascnmBA*****yGehy8", tables);
157        assert_eq!(request.tables.len(), 3);
158    }
159}