use crate::common::api_endpoints::DocxApiV1;
use openlark_core::{
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
req_option::RequestOption,
validate_required, SDKResult,
};
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>,
}
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> =
ApiRequest::post(&api_endpoint.to_url())
.body(serialize_params(¶ms, "Markdown/HTML 内容转换为文档块")?);
let response = Transport::request(api_request, &self.config, Some(option)).await?;
extract_response_data(response, "Markdown/HTML 内容转换为文档块")
}
}
#[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).unwrap();
assert_eq!(value["field"], "data");
}
}