use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
validate_required,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::common::api_endpoints::DocxApiV1;
pub struct GetDocumentRequest {
document_id: String,
config: Config,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetDocumentResponse {
pub document: Document,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Document {
pub document_id: String,
pub revision_id: i64,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cover: Option<DocumentCover>,
#[serde(skip_serializing_if = "Option::is_none")]
pub display_setting: Option<DocumentDisplaySetting>,
#[serde(default, flatten)]
pub extra: HashMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocumentCover {
pub token: String,
pub offset_ratio_x: i32,
pub offset_ratio_y: i32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocumentDisplaySetting {
pub show_authors: bool,
pub show_comment_count: bool,
pub show_create_time: bool,
pub show_like_count: bool,
pub show_pv: bool,
pub show_uv: bool,
}
impl ApiResponseTrait for GetDocumentResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl GetDocumentRequest {
pub fn new(config: Config) -> Self {
Self {
document_id: String::new(),
config,
}
}
pub fn document_id(mut self, document_id: impl Into<String>) -> Self {
self.document_id = document_id.into();
self
}
pub async fn execute(self) -> SDKResult<GetDocumentResponse> {
self.execute_with_options(openlark_core::req_option::RequestOption::default())
.await
}
pub async fn execute_with_options(
self,
option: openlark_core::req_option::RequestOption,
) -> SDKResult<GetDocumentResponse> {
validate_required!(self.document_id, "文档ID不能为空");
let api_endpoint = DocxApiV1::DocumentGet(self.document_id.clone());
let api_request: ApiRequest<GetDocumentResponse> = api_endpoint.to_request();
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::{method, path};
use wiremock::{Mock, ResponseTemplate};
#[tokio::test]
async fn test_get_document_returns_data_on_success() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/open-apis/docx/v1/documents/doc1"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"code": 0, "msg": "success",
"data": { "document": { "document_id": "doc1", "revision_id": 1 } }
})))
.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 = GetDocumentRequest::new(config)
.document_id("doc1")
.execute()
.await
.expect("获取文档应成功");
assert_eq!(resp.document.document_id, "doc1");
assert_eq!(resp.document.revision_id, 1);
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/doc1");
}
}