use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
validate_required,
};
use serde::{Deserialize, Serialize};
use super::super::models::WikiTask;
use crate::common::api_endpoints::WikiApiV2;
pub struct GetWikiTaskRequest {
task_id: String,
task_type: Option<String>,
config: Config,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetWikiTaskResponse {
pub task: Option<WikiTask>,
}
impl ApiResponseTrait for GetWikiTaskResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl GetWikiTaskRequest {
pub fn new(config: Config) -> Self {
Self {
task_id: String::new(),
task_type: None,
config,
}
}
pub fn task_id(mut self, task_id: impl Into<String>) -> Self {
self.task_id = task_id.into();
self
}
pub fn task_type(mut self, task_type: impl Into<String>) -> Self {
self.task_type = Some(task_type.into());
self
}
pub async fn execute(self) -> SDKResult<GetWikiTaskResponse> {
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<GetWikiTaskResponse> {
validate_required!(self.task_id, "任务ID不能为空");
let api_endpoint = WikiApiV2::TaskGet(self.task_id.clone());
let mut api_request: ApiRequest<GetWikiTaskResponse> = api_endpoint.to_request();
if let Some(task_type) = self.task_type {
api_request = api_request.query("task_type", &task_type);
}
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_wiki_task_returns_data_on_success() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/open-apis/wiki/v2/tasks/tk001"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"code": 0, "msg": "success", "data": {}
})))
.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 = GetWikiTaskRequest::new(config)
.task_id("tk001")
.execute()
.await
.expect("获取任务结果应成功");
assert!(resp.task.is_none());
let received = server.received_requests().await.unwrap_or_default();
assert_eq!(received.len(), 1);
assert_eq!(received[0].url.path(), "/open-apis/wiki/v2/tasks/tk001");
}
}