use openlark_core::{
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
error::SDKResult,
http::Transport,
validate_required,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct PatchTableRequest {
config: Config,
app_token: String,
table_id: String,
name: Option<String>,
}
impl PatchTableRequest {
pub fn new(config: Config) -> Self {
Self {
config,
app_token: String::new(),
table_id: String::new(),
name: None,
}
}
pub fn app_token(mut self, app_token: String) -> Self {
self.app_token = app_token;
self
}
pub fn table_id(mut self, table_id: String) -> Self {
self.table_id = table_id;
self
}
pub fn name(mut self, name: String) -> Self {
self.name = Some(name);
self
}
pub async fn execute(self) -> SDKResult<PatchTableResponse> {
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<PatchTableResponse> {
validate_required!(self.app_token.trim(), "app_token");
validate_required!(self.table_id.trim(), "table_id");
let name = self
.name
.ok_or_else(|| openlark_core::error::validation_error("name", "数据表名称不能为空"))?;
if name.trim().is_empty() {
return Err(openlark_core::error::validation_error(
"name",
"数据表名称不能为空",
));
}
if name.len() > 100 {
return Err(openlark_core::error::validation_error(
"name",
"数据表名称长度不能超过100个字符",
));
}
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",
"数据表名称不能包含 '[' 或 ']'",
));
}
use crate::common::api_endpoints::BitableApiV1;
let api_endpoint = BitableApiV1::TablePatch(self.app_token.clone(), self.table_id.clone());
let request_body = PatchTableRequestBody { name };
let api_request: ApiRequest<PatchTableResponse> =
ApiRequest::patch(&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(Serialize)]
struct PatchTableRequestBody {
name: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PatchTableResponse {
pub name: Option<String>,
}
impl ApiResponseTrait for PatchTableResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[cfg(test)]
mod tests {
use serde_json;
#[test]
fn test_serialization_roundtrip() {
let json = r#"{"test": "value"}"#;
assert!(serde_json::from_str::<serde_json::Value>(json).is_ok());
}
#[test]
fn test_deserialization_from_json() {
let json = r#"{"field": "data"}"#;
let value: serde_json::Value = serde_json::from_str(json).unwrap();
assert_eq!(value["field"], "data");
}
}