open_lark/service/cloud_docs/bitable/v1/app_table/
batch_create.rs1use 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::{AppTableService, TableData};
17
18impl AppTableService {
19 pub async fn batch_create(
21 &self,
22 request: BatchCreateTablesRequest,
23 option: Option<RequestOption>,
24 ) -> SDKResult<BaseResponse<BatchCreateTablesResponse>> {
25 let mut api_req = request.api_request;
26 api_req.http_method = Method::POST;
27 api_req.api_path = format!(
28 "/open-apis/bitable/v1/apps/{}/tables/batch_create",
29 request.app_token
30 );
31 api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
32 api_req.body = serde_json::to_vec(&BatchCreateTablesRequestBody {
33 tables: request.tables,
34 })?;
35
36 let api_resp = Transport::request(api_req, &self.config, option).await?;
37 Ok(api_resp)
38 }
39}
40
41#[derive(Debug, Default)]
43pub struct BatchCreateTablesRequest {
44 api_request: ApiRequest,
45 app_token: String,
47 tables: Vec<TableData>,
49}
50
51impl BatchCreateTablesRequest {
52 pub fn builder() -> BatchCreateTablesRequestBuilder {
53 BatchCreateTablesRequestBuilder::default()
54 }
55
56 pub fn new(app_token: impl ToString, tables: Vec<TableData>) -> Self {
58 Self {
59 api_request: ApiRequest::default(),
60 app_token: app_token.to_string(),
61 tables,
62 }
63 }
64}
65
66#[derive(Default)]
67pub struct BatchCreateTablesRequestBuilder {
68 request: BatchCreateTablesRequest,
69}
70
71impl BatchCreateTablesRequestBuilder {
72 pub fn app_token(mut self, app_token: impl ToString) -> Self {
74 self.request.app_token = app_token.to_string();
75 self
76 }
77
78 pub fn tables(mut self, tables: Vec<TableData>) -> Self {
80 self.request.tables = tables;
81 self
82 }
83
84 pub fn add_table(mut self, table: TableData) -> Self {
86 self.request.tables.push(table);
87 self
88 }
89
90 pub fn build(self) -> BatchCreateTablesRequest {
91 self.request
92 }
93}
94
95impl_executable_builder_owned!(
96 BatchCreateTablesRequestBuilder,
97 AppTableService,
98 BatchCreateTablesRequest,
99 BaseResponse<BatchCreateTablesResponse>,
100 batch_create
101);
102
103#[derive(Serialize)]
104struct BatchCreateTablesRequestBody {
105 tables: Vec<TableData>,
106}
107
108#[derive(Deserialize, Debug)]
109pub struct BatchCreateTablesResponse {
110 pub table_ids: Vec<String>,
112}
113
114impl ApiResponseTrait for BatchCreateTablesResponse {
115 fn data_format() -> ResponseFormat {
116 ResponseFormat::Data
117 }
118}
119
120#[cfg(test)]
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}