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> =
ApiRequest::delete(&api_endpoint.to_url()).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());
}
let response = Transport::request(api_request, &self.config, Some(option)).await?;
extract_response_data(response, "删除块")
}
}
#[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");
}
}