use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
req_option::RequestOption,
validate_required,
};
use serde::{Deserialize, Serialize};
use crate::common::{api_endpoints::DocxApiV1, api_utils::*};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchDeleteDocumentBlockChildrenParams {
#[serde(skip_serializing)]
pub document_id: String,
#[serde(skip_serializing)]
pub block_id: String,
#[serde(skip_serializing)]
pub document_revision_id: Option<i64>,
pub start_index: i32,
pub end_index: i32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchDeleteDocumentBlockChildrenResponse {
pub document_revision_id: i64,
pub client_token: String,
}
impl ApiResponseTrait for BatchDeleteDocumentBlockChildrenResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub struct BatchDeleteDocumentBlockChildrenRequest {
config: Config,
}
impl BatchDeleteDocumentBlockChildrenRequest {
pub fn new(config: Config) -> Self {
Self { config }
}
pub async fn execute(
self,
params: BatchDeleteDocumentBlockChildrenParams,
) -> SDKResult<BatchDeleteDocumentBlockChildrenResponse> {
self.execute_with_options(params, RequestOption::default())
.await
}
pub async fn execute_with_options(
self,
params: BatchDeleteDocumentBlockChildrenParams,
option: RequestOption,
) -> SDKResult<BatchDeleteDocumentBlockChildrenResponse> {
validate_required!(params.document_id, "文档ID不能为空");
validate_required!(params.block_id, "父块ID不能为空");
let api_endpoint = DocxApiV1::DocumentBlockChildrenBatchDelete(
params.document_id.clone(),
params.block_id.clone(),
);
let mut api_request: ApiRequest<BatchDeleteDocumentBlockChildrenResponse> = api_endpoint
.to_request()
.body(serialize_params(¶ms, "删除块")?);
if let Some(document_revision_id) = params.document_revision_id {
api_request =
api_request.query("document_revision_id", &document_revision_id.to_string());
}
Transport::request_typed(api_request, &self.config, Some(option), "删除块").await
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use wiremock::MockServer;
use wiremock::matchers::{header, method, path};
use wiremock::{Mock, ResponseTemplate};
#[tokio::test]
async fn test_batch_delete_document_block_children_returns_data_on_success() {
let server = MockServer::start().await;
Mock::given(method("DELETE"))
.and(path(
"/open-apis/docx/v1/documents/doc1/blocks/blk1/children/batch_delete",
))
.and(header("Authorization", "Bearer test-tenant-token"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"code": 0, "msg": "success",
"data": { "document_revision_id": 2, "client_token": "ct001" }
})))
.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();
let resp = BatchDeleteDocumentBlockChildrenRequest::new(config)
.execute_with_options(
BatchDeleteDocumentBlockChildrenParams {
document_id: "doc1".into(),
block_id: "blk1".into(),
document_revision_id: Some(-1),
start_index: 0,
end_index: 1,
},
RequestOption::builder()
.tenant_access_token("test-tenant-token")
.build(),
)
.await
.expect("删除子块应成功");
assert_eq!(resp.document_revision_id, 2);
let received = server.received_requests().await.unwrap_or_default();
assert_eq!(received.len(), 1);
assert_eq!(
received[0].url.path(),
"/open-apis/docx/v1/documents/doc1/blocks/blk1/children/batch_delete"
);
let query: std::collections::HashMap<_, _> = received[0]
.url
.query_pairs()
.map(|(key, value)| (key.into_owned(), value.into_owned()))
.collect();
assert_eq!(
query.get("document_revision_id").map(String::as_str),
Some("-1")
);
let body: serde_json::Value =
serde_json::from_slice(&received[0].body).expect("请求体应为合法 JSON");
assert_eq!(body["start_index"], 0);
assert_eq!(body["end_index"], 1);
}
}