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 ConvertContentToBlocksParams {
pub content_type: ContentType,
pub content: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ContentType {
#[serde(rename = "markdown")]
Markdown,
#[serde(rename = "html")]
Html,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConvertContentToBlocksResponse {
#[serde(default)]
pub first_level_block_ids: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub blocks: Option<Vec<DocxBlock>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub block_id_to_image_urls: Option<serde_json::Value>,
}
impl ApiResponseTrait for ConvertContentToBlocksResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub struct ConvertContentToBlocksRequest {
config: Config,
}
impl ConvertContentToBlocksRequest {
pub fn new(config: Config) -> Self {
Self { config }
}
pub async fn execute(
self,
params: ConvertContentToBlocksParams,
) -> SDKResult<ConvertContentToBlocksResponse> {
self.execute_with_options(params, RequestOption::default())
.await
}
pub async fn execute_with_options(
self,
params: ConvertContentToBlocksParams,
option: RequestOption,
) -> SDKResult<ConvertContentToBlocksResponse> {
validate_required!(params.content, "源内容不能为空");
let api_endpoint = DocxApiV1::DocumentConvert;
let api_request: ApiRequest<ConvertContentToBlocksResponse> = api_endpoint
.to_request()
.body(serialize_params(¶ms, "Markdown/HTML 内容转换为文档块")?);
Transport::request_typed(
api_request,
&self.config,
Some(option),
"Markdown/HTML 内容转换为文档块",
)
.await
}
}
#[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_convert_content_to_blocks_returns_data_on_success() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/open-apis/docx/documents/blocks/convert"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"code": 0, "msg": "success",
"data": { "first_level_block_ids": ["blk1", "blk2"] }
})))
.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 = ConvertContentToBlocksRequest::new(config)
.execute(ConvertContentToBlocksParams {
content_type: ContentType::Markdown,
content: "# 标题".into(),
})
.await
.expect("转换内容应成功");
assert_eq!(resp.first_level_block_ids.len(), 2);
assert_eq!(resp.first_level_block_ids[0], "blk1");
let received = server.received_requests().await.unwrap_or_default();
assert_eq!(received.len(), 1);
assert_eq!(
received[0].url.path(),
"/open-apis/docx/documents/blocks/convert"
);
let sent: serde_json::Value = serde_json::from_slice(&received[0].body).unwrap();
assert_eq!(sent["content_type"], "markdown");
}
}