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