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> = api_endpoint.to_request();
Transport::request_typed(api_request, &self.config, Some(option), "Bitable 删除字段").await
}
}
#[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 super::*;
use serde_json::json;
use wiremock::MockServer;
use wiremock::matchers::{method, path};
use wiremock::{Mock, ResponseTemplate};
#[tokio::test]
async fn test_delete_field_returns_data_on_success() {
let server = MockServer::start().await;
Mock::given(method("DELETE"))
.and(path(
"/open-apis/bitable/v1/apps/app001/tables/tbl001/fields/fld001",
))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"code": 0, "msg": "success", "data": { "field_id": "fld001", "deleted": true }
})))
.mount(&server)
.await;
let config = Config::builder()
.app_id("ci_app_id")
.app_secret("ci_app_secret")
.base_url(server.uri())
.enable_token_cache(false)
.build();
DeleteFieldRequest::new(config)
.app_token("app001".into())
.table_id("tbl001".into())
.field_id("fld001".into())
.execute()
.await
.expect("删除字段应成功");
let received = server.received_requests().await.unwrap_or_default();
assert_eq!(received.len(), 1);
assert_eq!(
received[0].url.path(),
"/open-apis/bitable/v1/apps/app001/tables/tbl001/fields/fld001"
);
}
}