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};
#[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> = api_endpoint.to_request();
if let Some(lang) = params.lang {
api_request = api_request.query("lang", &lang.to_string());
}
Transport::request_typed(
api_request,
&self.config,
Some(option),
"获取文档纯文本内容",
)
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use wiremock::MockServer;
use wiremock::matchers::{header, method, path};
use wiremock::{Mock, ResponseTemplate};
#[tokio::test]
async fn test_get_document_raw_content_returns_data_on_success() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/open-apis/docx/v1/documents/doc%201/raw_content"))
.and(header("Authorization", "Bearer test-tenant-token"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"code": 0, "msg": "success",
"data": { "content": "文档纯文本内容" }
})))
.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 = GetDocumentRawContentRequest::new(config)
.execute_with_options(
GetDocumentRawContentParams {
document_id: "doc 1".into(),
lang: Some(0),
},
RequestOption::builder()
.tenant_access_token("test-tenant-token")
.build(),
)
.await
.expect("获取文档纯文本应成功");
assert_eq!(resp.content, "文档纯文本内容");
let received = server.received_requests().await.unwrap_or_default();
assert_eq!(received.len(), 1);
assert_eq!(
received[0].url.path(),
"/open-apis/docx/v1/documents/doc%201/raw_content"
);
assert_eq!(received[0].url.query(), Some("lang=0"));
}
}