Skip to main content

openlark_workflow/v2/task/
add_tasklist.rs

1//! 任务加入清单
2//!
3//! docPath: <https://open.feishu.cn/document/server-docs/docs/task-v2/task-add_tasklist/create>
4
5use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
6use openlark_core::{
7    SDKResult,
8    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
9    config::Config,
10    validate_required,
11};
12use serde::{Deserialize, Serialize};
13use std::sync::Arc;
14
15/// 任务加入清单请求体
16#[derive(Debug, Clone, Serialize, Default)]
17pub struct AddTasklistBody {
18    /// 任务清单 GUID
19    pub tasklist_guid: String,
20    /// 分组 GUID(可选)
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub section_guid: Option<String>,
23}
24
25/// 任务加入清单响应
26#[derive(Debug, Clone, Deserialize)]
27pub struct AddTasklistResponse {
28    /// 任务 GUID
29    pub task_guid: String,
30    /// 任务清单 GUID
31    pub tasklist_guid: String,
32    /// 分组 GUID
33    #[serde(default)]
34    pub section_guid: Option<String>,
35}
36
37/// 任务加入清单请求
38#[derive(Debug, Clone)]
39pub struct AddTasklistRequest {
40    /// 配置信息
41    config: Arc<Config>,
42    /// 任务 GUID
43    task_guid: String,
44    /// 请求体
45    body: AddTasklistBody,
46}
47
48impl AddTasklistRequest {
49    /// 创建新的请求构建器。
50    pub fn new(config: Arc<Config>, task_guid: impl Into<String>) -> Self {
51        Self {
52            config,
53            task_guid: task_guid.into(),
54            body: AddTasklistBody::default(),
55        }
56    }
57
58    /// 设置任务清单 GUID
59    pub fn tasklist_guid(mut self, tasklist_guid: impl Into<String>) -> Self {
60        self.body.tasklist_guid = tasklist_guid.into();
61        self
62    }
63
64    /// 设置分组 GUID
65    pub fn section_guid(mut self, section_guid: impl Into<String>) -> Self {
66        self.body.section_guid = Some(section_guid.into());
67        self
68    }
69
70    /// 执行请求
71    pub async fn execute(self) -> SDKResult<AddTasklistResponse> {
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<AddTasklistResponse> {
81        // 验证必填字段
82        validate_required!(self.task_guid.trim(), "任务GUID不能为空");
83        validate_required!(self.body.tasklist_guid.trim(), "任务清单GUID不能为空");
84
85        let api_endpoint = TaskApiV2::TaskAddTasklist(self.task_guid.clone());
86        let mut request = ApiRequest::<AddTasklistResponse>::post(api_endpoint.to_url());
87
88        let request_body = &self.body;
89        request = request.body(serialize_params(request_body, "任务加入清单")?);
90
91        openlark_core::http::Transport::request_typed(
92            request,
93            &self.config,
94            Some(option),
95            "任务加入清单",
96        )
97        .await
98    }
99}
100
101impl ApiResponseTrait for AddTasklistResponse {
102    fn data_format() -> ResponseFormat {
103        ResponseFormat::Data
104    }
105}
106
107#[cfg(test)]
108#[allow(unused_imports)]
109mod tests {
110    use std::sync::Arc;
111
112    use super::*;
113
114    #[test]
115    fn test_add_tasklist_builder() {
116        let config = Arc::new(
117            openlark_core::config::Config::builder()
118                .app_id("test")
119                .app_secret("test")
120                .build(),
121        );
122
123        let request = AddTasklistRequest::new(config, "task_123")
124            .tasklist_guid("tasklist_456")
125            .section_guid("section_789");
126
127        assert_eq!(request.task_guid, "task_123");
128        assert_eq!(request.body.tasklist_guid, "tasklist_456");
129        assert_eq!(request.body.section_guid, Some("section_789".to_string()));
130    }
131
132    #[test]
133    fn test_task_add_tasklist_api_v2_url() {
134        let endpoint = TaskApiV2::TaskAddTasklist("task_123".to_string());
135        assert_eq!(
136            endpoint.to_url(),
137            "/open-apis/task/v2/tasks/task_123/add_tasklist"
138        );
139    }
140}