use openlark_core::{
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
error::SDKResult,
http::Transport,
validate_required,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct DeleteFieldRequest {
config: Config,
app_token: String,
table_id: String,
field_id: String,
}
impl DeleteFieldRequest {
pub fn new(config: Config) -> Self {
Self {
config,
app_token: String::new(),
table_id: String::new(),
field_id: String::new(),
}
}
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 field_id(mut self, field_id: String) -> Self {
self.field_id = field_id;
self
}
pub async fn execute(self) -> SDKResult<DeleteFieldResponse> {
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<DeleteFieldResponse> {
validate_required!(self.app_token.trim(), "app_token");
validate_required!(self.table_id.trim(), "table_id");
validate_required!(self.field_id.trim(), "field_id");
use crate::common::api_endpoints::BitableApiV1;
let api_endpoint = BitableApiV1::FieldDelete(
self.app_token.clone(),
self.table_id.clone(),
self.field_id.clone(),
);
let api_request: ApiRequest<DeleteFieldResponse> =
ApiRequest::delete(&api_endpoint.to_url());
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)]
pub struct DeleteFieldResponse {
pub field_id: String,
pub deleted: bool,
}
impl ApiResponseTrait for DeleteFieldResponse {
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).expect("JSON 反序列化失败");
assert_eq!(value["field"], "data");
}
}