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