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        let response =
92            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
93        extract_response_data(response, "任务加入清单")
94    }
95}
96
97impl ApiResponseTrait for AddTasklistResponse {
98    fn data_format() -> ResponseFormat {
99        ResponseFormat::Data
100    }
101}
102
103#[cfg(test)]
104#[allow(unused_imports)]
105mod tests {
106    use std::sync::Arc;
107
108    use super::*;
109
110    #[test]
111    fn test_add_tasklist_builder() {
112        let config = Arc::new(
113            openlark_core::config::Config::builder()
114                .app_id("test")
115                .app_secret("test")
116                .build(),
117        );
118
119        let request = AddTasklistRequest::new(config, "task_123")
120            .tasklist_guid("tasklist_456")
121            .section_guid("section_789");
122
123        assert_eq!(request.task_guid, "task_123");
124        assert_eq!(request.body.tasklist_guid, "tasklist_456");
125        assert_eq!(request.body.section_guid, Some("section_789".to_string()));
126    }
127
128    #[test]
129    fn test_task_add_tasklist_api_v2_url() {
130        let endpoint = TaskApiV2::TaskAddTasklist("task_123".to_string());
131        assert_eq!(
132            endpoint.to_url(),
133            "/open-apis/task/v2/tasks/task_123/add_tasklist"
134        );
135    }
136}