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 GetDocumentRawContentParams {
pub document_id: String,
pub lang: Option<i32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetDocumentRawContentResponse {
pub content: String,
}
impl ApiResponseTrait for GetDocumentRawContentResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub struct GetDocumentRawContentRequest {
config: Config,
}
impl GetDocumentRawContentRequest {
pub fn new(config: Config) -> Self {
Self { config }
}
pub async fn execute(
self,
params: GetDocumentRawContentParams,
) -> SDKResult<GetDocumentRawContentResponse> {
self.execute_with_options(params, RequestOption::default())
.await
}
pub async fn execute_with_options(
self,
params: GetDocumentRawContentParams,
option: RequestOption,
) -> SDKResult<GetDocumentRawContentResponse> {
validate_required!(params.document_id, "文档ID不能为空");
let api_endpoint = DocxApiV1::DocumentRawContent(params.document_id.clone());
let mut api_request: ApiRequest<GetDocumentRawContentResponse> =
ApiRequest::get(&api_endpoint.to_url());
if let Some(lang) = params.lang {
api_request = api_request.query("lang", &lang.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).unwrap();
assert_eq!(value["field"], "data");
}
}