Skip to main content

openlark_workflow/v1/task/
get.rs

1//! 获取指定任务(v1)
2//!
3//! docPath: https://open.feishu.cn/document/server-docs/docs/task-v1/task/get
4
5use openlark_core::{
6    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
7    config::Config,
8    SDKResult,
9};
10use serde::Deserialize;
11use std::sync::Arc;
12
13/// 任务信息(v1)
14#[derive(Debug, Clone, Deserialize)]
15pub struct TaskV1 {
16    /// 任务 ID
17    pub id: String,
18    /// 任务标题
19    pub summary: String,
20    /// 任务描述
21    #[serde(default)]
22    pub description: Option<String>,
23    /// 任务开始时间
24    #[serde(default)]
25    pub start: Option<String>,
26    /// 任务截止时间
27    #[serde(default)]
28    pub due: Option<String>,
29    /// 任务优先级(1-5)
30    #[serde(default)]
31    pub priority: Option<i32>,
32    /// 任务是否完成
33    pub is_completed: bool,
34    /// 创建时间
35    pub created_at: String,
36    /// 更新时间
37    pub updated_at: String,
38}
39
40/// 获取任务响应(v1)
41#[derive(Debug, Clone, Deserialize)]
42pub struct GetTaskResponseV1 {
43    /// 任务信息
44    pub task: TaskV1,
45}
46
47/// 获取任务请求(v1)
48#[derive(Debug, Clone)]
49pub struct GetTaskRequestV1 {
50    config: Arc<Config>,
51    task_id: String,
52}
53
54impl GetTaskRequestV1 {
55    pub fn new(config: Arc<Config>, task_id: impl Into<String>) -> Self {
56        Self {
57            config,
58            task_id: task_id.into(),
59        }
60    }
61
62    /// 执行请求
63    pub async fn execute(self) -> SDKResult<GetTaskResponseV1> {
64        self.execute_with_options(openlark_core::req_option::RequestOption::default())
65            .await
66    }
67
68    /// 执行请求(带选项)
69    pub async fn execute_with_options(
70        self,
71        option: openlark_core::req_option::RequestOption,
72    ) -> SDKResult<GetTaskResponseV1> {
73        let api_endpoint = crate::common::api_endpoints::TaskApiV1::TaskGet(self.task_id);
74        let request = ApiRequest::<GetTaskResponseV1>::get(api_endpoint.to_url());
75
76        let response =
77            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
78        response.data.ok_or_else(|| {
79            openlark_core::error::validation_error("响应数据为空", "服务器没有返回有效的数据")
80        })
81    }
82}
83
84impl ApiResponseTrait for GetTaskResponseV1 {
85    fn data_format() -> ResponseFormat {
86        ResponseFormat::Data
87    }
88}
89
90#[cfg(test)]
91#[allow(unused_imports)]
92mod tests {
93    use std::sync::Arc;
94
95    use super::*;
96
97    #[test]
98    fn test_get_task_v1_builder() {
99        let config = Arc::new(
100            openlark_core::config::Config::builder()
101                .app_id("test")
102                .app_secret("test")
103                .build(),
104        );
105
106        let request = GetTaskRequestV1::new(config, "test_task_id");
107        assert_eq!(request.task_id, "test_task_id");
108    }
109
110    #[test]
111    fn test_task_api_v1_get_url() {
112        let endpoint = crate::common::api_endpoints::TaskApiV1::TaskGet("task_123".to_string());
113        assert_eq!(endpoint.to_url(), "/open-apis/task/v1/tasks/task_123");
114    }
115}