use crate::ccm::docx::models::common_types::DocxBlock;
use crate::common::api_endpoints::DocxApiV1;
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_utils::*;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetDocumentBlocksParams {
pub document_id: String,
pub page_size: Option<u32>,
pub page_token: Option<String>,
pub document_revision_id: Option<i64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetDocumentBlocksResponse {
#[serde(default)]
pub items: Vec<DocxBlock>,
pub page_token: Option<String>,
pub has_more: Option<bool>,
}
impl ApiResponseTrait for GetDocumentBlocksResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub struct GetDocumentBlocksRequest {
config: Config,
}
impl GetDocumentBlocksRequest {
pub fn new(config: Config) -> Self {
Self { config }
}
pub async fn execute(
self,
params: GetDocumentBlocksParams,
) -> SDKResult<GetDocumentBlocksResponse> {
self.execute_with_options(params, RequestOption::default())
.await
}
pub async fn execute_with_options(
self,
params: GetDocumentBlocksParams,
option: RequestOption,
) -> SDKResult<GetDocumentBlocksResponse> {
validate_required!(params.document_id, "文档ID不能为空");
let api_endpoint = DocxApiV1::DocumentBlockList(params.document_id.clone());
let mut api_request: ApiRequest<GetDocumentBlocksResponse> =
ApiRequest::get(&api_endpoint.to_url());
if let Some(page_size) = params.page_size {
api_request = api_request.query("page_size", &page_size.to_string());
}
if let Some(page_token) = params.page_token {
api_request = api_request.query("page_token", &page_token);
}
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");
}
}