Skip to main content

openlark_workflow/v2/task/
get.rs

1//! 获取任务详情
2//!
3//! docPath: https://open.feishu.cn/document/server-docs/docs/task-v2/task/get
4
5use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
6use crate::v2::task::models::GetTaskResponse;
7use openlark_core::{
8    SDKResult,
9    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
10    config::Config,
11    validate_required,
12};
13use std::sync::Arc;
14
15/// 获取任务请求
16#[derive(Debug, Clone)]
17pub struct GetTaskRequest {
18    /// 配置信息
19    config: Arc<Config>,
20    /// 任务 GUID
21    task_guid: String,
22}
23
24impl GetTaskRequest {
25    /// 创建新的请求构建器。
26    pub fn new(config: Arc<Config>, task_guid: String) -> Self {
27        Self { config, task_guid }
28    }
29
30    /// 执行请求
31    pub async fn execute(self) -> SDKResult<GetTaskResponse> {
32        self.execute_with_options(openlark_core::req_option::RequestOption::default())
33            .await
34    }
35
36    /// 执行请求(带选项)
37    pub async fn execute_with_options(
38        self,
39        option: openlark_core::req_option::RequestOption,
40    ) -> SDKResult<GetTaskResponse> {
41        // 验证必填字段
42        validate_required!(self.task_guid.trim(), "任务GUID不能为空");
43
44        let api_endpoint = TaskApiV2::TaskGet(self.task_guid.clone());
45        let request = ApiRequest::<GetTaskResponse>::get(api_endpoint.to_url());
46
47        let response =
48            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
49        extract_response_data(response, "获取任务")
50    }
51}
52
53impl ApiResponseTrait for GetTaskResponse {
54    fn data_format() -> ResponseFormat {
55        ResponseFormat::Data
56    }
57}
58
59#[cfg(test)]
60#[allow(unused_imports)]
61mod tests {
62    use std::sync::Arc;
63
64    use super::*;
65
66    #[test]
67    fn test_get_task_request() {
68        let config = openlark_core::config::Config::builder()
69            .app_id("test")
70            .app_secret("test")
71            .build();
72
73        let request = GetTaskRequest::new(Arc::new(config), "task_123".to_string());
74
75        assert_eq!(request.task_guid, "task_123");
76    }
77}