Skip to main content

openlark_workflow/v2/task/
create.rs

1//! 创建任务
2//!
3//! docPath: https://open.feishu.cn/document/server-docs/docs/task-v2/task/create
4
5use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
6use crate::v2::task::models::{CreateTaskBody, CreateTaskResponse};
7use openlark_core::{
8    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
9    config::Config,
10    validate_required, SDKResult,
11};
12use std::sync::Arc;
13
14/// 创建任务请求
15#[derive(Debug, Clone)]
16pub struct CreateTaskRequest {
17    /// 配置信息
18    config: Arc<Config>,
19    /// 请求体
20    body: CreateTaskBody,
21}
22
23impl CreateTaskRequest {
24    pub fn new(config: Arc<Config>) -> Self {
25        Self {
26            config,
27            body: CreateTaskBody::default(),
28        }
29    }
30
31    /// 设置任务标题
32    pub fn summary(mut self, summary: impl Into<String>) -> Self {
33        self.body.summary = summary.into();
34        self
35    }
36
37    /// 设置任务描述
38    pub fn description(mut self, description: impl Into<String>) -> Self {
39        self.body.description = Some(description.into());
40        self
41    }
42
43    /// 设置任务开始时间
44    pub fn start(mut self, start: impl Into<String>) -> Self {
45        self.body.start = Some(start.into());
46        self
47    }
48
49    /// 设置任务截止时间
50    pub fn due(mut self, due: impl Into<String>) -> Self {
51        self.body.due = Some(due.into());
52        self
53    }
54
55    /// 设置任务所属的任务清单 GUID
56    pub fn tasklist_guid(mut self, tasklist_guid: impl Into<String>) -> Self {
57        self.body.tasklist_guid = Some(tasklist_guid.into());
58        self
59    }
60
61    /// 设置任务所属的分组 GUID
62    pub fn section_guid(mut self, section_guid: impl Into<String>) -> Self {
63        self.body.section_guid = Some(section_guid.into());
64        self
65    }
66
67    /// 设置任务优先级(1-5)
68    pub fn priority(mut self, priority: i32) -> Self {
69        self.body.priority = Some(priority);
70        self
71    }
72
73    /// 设置自定义字段
74    pub fn custom_fields(mut self, custom_fields: serde_json::Value) -> Self {
75        self.body.custom_fields = Some(custom_fields);
76        self
77    }
78
79    /// 设置任务关注者
80    pub fn followers(mut self, followers: Vec<String>) -> Self {
81        self.body.followers = Some(followers);
82        self
83    }
84
85    /// 设置子任务
86    pub fn subtasks(mut self, subtasks: Vec<serde_json::Value>) -> Self {
87        self.body.subtasks = Some(subtasks);
88        self
89    }
90
91    /// 设置任务执行者
92    pub fn assignee(mut self, assignee: impl Into<String>) -> Self {
93        self.body.assignee = Some(assignee.into());
94        self
95    }
96
97    /// 设置任务提醒时间
98    pub fn remind_time(mut self, remind_time: impl Into<String>) -> Self {
99        self.body.remind_time = Some(remind_time.into());
100        self
101    }
102
103    /// 设置重复规则
104    pub fn repeat_rule(mut self, repeat_rule: serde_json::Value) -> Self {
105        self.body.repeat_rule = Some(repeat_rule);
106        self
107    }
108
109    /// 执行请求
110    pub async fn execute(self) -> SDKResult<CreateTaskResponse> {
111        self.execute_with_options(openlark_core::req_option::RequestOption::default())
112            .await
113    }
114
115    /// 执行请求(带选项)
116    pub async fn execute_with_options(
117        self,
118        option: openlark_core::req_option::RequestOption,
119    ) -> SDKResult<CreateTaskResponse> {
120        // 验证必填字段
121        validate_required!(self.body.summary.trim(), "任务标题不能为空");
122
123        let api_endpoint = TaskApiV2::TaskCreate;
124        let mut request = ApiRequest::<CreateTaskResponse>::post(api_endpoint.to_url());
125
126        let request_body = &self.body;
127        request = request.body(serialize_params(request_body, "创建任务")?);
128
129        let response =
130            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
131        extract_response_data(response, "创建任务")
132    }
133}
134
135impl ApiResponseTrait for CreateTaskResponse {
136    fn data_format() -> ResponseFormat {
137        ResponseFormat::Data
138    }
139}
140
141#[cfg(test)]
142#[allow(unused_imports)]
143mod tests {
144    use std::sync::Arc;
145
146    use super::*;
147
148    #[test]
149    fn test_create_task_builder() {
150        let config = Arc::new(
151            openlark_core::config::Config::builder()
152                .app_id("test")
153                .app_secret("test")
154                .build(),
155        );
156
157        let request = CreateTaskRequest::new(config)
158            .summary("测试任务")
159            .description("任务描述")
160            .priority(3);
161
162        assert_eq!(request.body.summary, "测试任务");
163        assert_eq!(request.body.description, Some("任务描述".to_string()));
164        assert_eq!(request.body.priority, Some(3));
165    }
166
167    #[test]
168    fn test_task_api_v2_url() {
169        let endpoint = TaskApiV2::TaskCreate;
170        assert_eq!(endpoint.to_url(), "/open-apis/task/v2/tasks");
171
172        let endpoint = TaskApiV2::TaskGet("task_123".to_string());
173        assert!(endpoint.to_url().contains("task_123"));
174    }
175}