use openlark_core::{
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
error::SDKResult,
http::Transport,
validate_required,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct CreateTableRequest {
config: Config,
app_token: String,
table: TableData,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CreateTableResponse {
pub table_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub default_view_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub field_id_list: Option<Vec<String>>,
}
impl ApiResponseTrait for CreateTableResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl CreateTableRequest {
pub fn new(config: Config) -> Self {
Self {
config,
app_token: String::new(),
table: TableData::default(),
}
}
pub fn app_token(mut self, app_token: String) -> Self {
self.app_token = app_token;
self
}
pub fn table(mut self, table: TableData) -> Self {
self.table = table;
self
}
pub async fn execute(self) -> SDKResult<CreateTableResponse> {
self.execute_with_options(openlark_core::req_option::RequestOption::default())
.await
}
pub async fn execute_with_options(
self,
option: openlark_core::req_option::RequestOption,
) -> SDKResult<CreateTableResponse> {
validate_required!(self.app_token.trim(), "app_token");
if self.table.name.trim().is_empty() {
return Err(openlark_core::error::validation_error(
"name",
"数据表名称不能为空",
));
}
if self.table.name.len() > 100 {
return Err(openlark_core::error::validation_error(
"name",
"数据表名称长度不能超过100个字符",
));
}
let name = self.table.name.as_str();
if name.contains('/') {
return Err(openlark_core::error::validation_error(
"name",
"数据表名称不能包含 '/'",
));
}
if name.contains('\\') {
return Err(openlark_core::error::validation_error(
"name",
"数据表名称不能包含 '\\\\'",
));
}
if name.contains('?') {
return Err(openlark_core::error::validation_error(
"name",
"数据表名称不能包含 '?'",
));
}
if name.contains('*') {
return Err(openlark_core::error::validation_error(
"name",
"数据表名称不能包含 '*'",
));
}
if name.contains(':') {
return Err(openlark_core::error::validation_error(
"name",
"数据表名称不能包含 ':'",
));
}
if name.contains('[') || name.contains(']') {
return Err(openlark_core::error::validation_error(
"name",
"数据表名称不能包含 '[' 或 ']'",
));
}
if self.table.default_view_name.is_some() && self.table.fields.is_none() {
return Err(openlark_core::error::validation_error(
"fields",
"当填写 default_view_name 时,必须同时填写 fields",
));
}
if let Some(ref default_view_name) = self.table.default_view_name
&& (default_view_name.contains('[') || default_view_name.contains(']'))
{
return Err(openlark_core::error::validation_error(
"default_view_name",
"默认视图名称不能包含 '[' 或 ']'",
));
}
use crate::common::api_endpoints::BitableApiV1;
let api_endpoint = BitableApiV1::TableCreate(self.app_token.clone());
let request_body = CreateTableRequestBody { table: self.table };
let api_request: ApiRequest<CreateTableResponse> =
ApiRequest::post(&api_endpoint.to_url()).body(serde_json::to_vec(&request_body)?);
let response = Transport::request(api_request, &self.config, Some(option)).await?;
response.data.ok_or_else(|| {
openlark_core::error::validation_error("响应数据为空", "服务器没有返回有效的数据")
})
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct TableData {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub default_view_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub fields: Option<Vec<TableField>>,
}
impl TableData {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
default_view_name: None,
fields: None,
}
}
pub fn with_default_view_name(mut self, view_name: impl Into<String>) -> Self {
self.default_view_name = Some(view_name.into());
self
}
pub fn with_fields(mut self, fields: Vec<TableField>) -> Self {
self.fields = Some(fields);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TableField {
pub field_name: String,
#[serde(rename = "type")]
pub field_type: i32,
#[serde(skip_serializing_if = "Option::is_none")]
pub property: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<FieldDescription>,
}
#[cfg(test)]
mod tests {
use super::*;
use openlark_core::testing::prelude::test_runtime;
#[test]
fn test_empty_app_token() {
let config = Config::default();
let table = TableData::new("测试表");
let request = CreateTableRequest::new(config)
.app_token("".to_string())
.table(table);
let rt = test_runtime();
let result = rt.block_on(request.execute());
assert!(result.is_err());
}
#[test]
fn test_empty_table_name() {
let config = Config::default();
let table = TableData::new("");
let request = CreateTableRequest::new(config)
.app_token("app_token".to_string())
.table(table);
let rt = test_runtime();
let result = rt.block_on(request.execute());
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.to_string().contains("数据表名称不能为空"));
}
#[test]
fn test_table_name_too_long() {
let config = Config::default();
let table = TableData::new("a".repeat(101));
let request = CreateTableRequest::new(config)
.app_token("app_token".to_string())
.table(table);
let rt = test_runtime();
let result = rt.block_on(request.execute());
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.to_string().contains("数据表名称长度"));
}
#[test]
fn test_table_name_with_invalid_chars() {
let config = Config::default();
let table = TableData::new("test/table");
let request = CreateTableRequest::new(config)
.app_token("app_token".to_string())
.table(table);
let rt = test_runtime();
let result = rt.block_on(request.execute());
assert!(result.is_err());
}
#[test]
fn test_response_trait() {
assert_eq!(CreateTableResponse::data_format(), ResponseFormat::Data);
}
}
impl TableField {
pub fn new(name: impl Into<String>, field_type: i32) -> Self {
Self {
field_name: name.into(),
field_type,
property: None,
description: None,
}
}
pub fn text(name: impl Into<String>) -> Self {
Self::new(name, 1) }
pub fn number(name: impl Into<String>) -> Self {
Self::new(name, 2) }
pub fn single_select(name: impl Into<String>, options: Vec<String>) -> Self {
let options_value: Vec<serde_json::Value> = options
.into_iter()
.map(|opt| serde_json::json!({"name": opt}))
.collect();
Self {
field_name: name.into(),
field_type: 3, property: Some(serde_json::json!({"options": options_value})),
description: None,
}
}
pub fn multi_select(name: impl Into<String>, options: Vec<String>) -> Self {
let options_value: Vec<serde_json::Value> = options
.into_iter()
.map(|opt| serde_json::json!({"name": opt}))
.collect();
Self {
field_name: name.into(),
field_type: 4, property: Some(serde_json::json!({"options": options_value})),
description: None,
}
}
pub fn date(name: impl Into<String>) -> Self {
Self::new(name, 5) }
}
#[derive(Serialize)]
struct CreateTableRequestBody {
table: TableData,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FieldDescription {
#[serde(skip_serializing_if = "Option::is_none")]
pub disable_sync: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub text: Option<String>,
}