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