Skip to main content

openlark_workflow/v1/task/
create.rs

1//! 创建任务(v1)
2//!
3//! docPath: https://open.feishu.cn/document/server-docs/docs/task-v1/task/create
4
5use openlark_core::{
6    SDKResult,
7    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
8    config::Config,
9    validate_required,
10};
11use serde::{Deserialize, Serialize};
12use std::sync::Arc;
13
14/// 创建任务请求体(v1)
15#[derive(Debug, Clone, Serialize, Default)]
16/// 创建任务请求体。
17pub struct CreateTaskBodyV1 {
18    /// 任务标题
19    pub summary: String,
20
21    /// 任务描述
22    #[serde(skip_serializing_if = "Option::is_none")]
23    /// 任务描述。
24    pub description: Option<String>,
25
26    /// 任务开始时间
27    #[serde(skip_serializing_if = "Option::is_none")]
28    /// 任务开始时间。
29    pub start: Option<String>,
30
31    /// 任务截止时间
32    #[serde(skip_serializing_if = "Option::is_none")]
33    /// 任务截止时间。
34    pub due: Option<String>,
35
36    /// 任务优先级(1-5)
37    #[serde(skip_serializing_if = "Option::is_none")]
38    /// 任务优先级。
39    pub priority: Option<i32>,
40}
41
42/// 创建任务响应(v1)
43#[derive(Debug, Clone, Deserialize)]
44/// 创建任务响应。
45pub struct CreateTaskResponseV1 {
46    /// 任务 GUID
47    pub task_guid: String,
48
49    /// 任务标题
50    pub summary: String,
51}
52
53/// 创建任务请求(v1)
54#[derive(Debug, Clone)]
55/// 创建任务请求构建器。
56pub struct CreateTaskRequestV1 {
57    config: Arc<Config>,
58    body: CreateTaskBodyV1,
59}
60
61impl CreateTaskRequestV1 {
62    /// 创建新的请求构建器。
63    pub fn new(config: Arc<Config>) -> Self {
64        Self {
65            config,
66            body: CreateTaskBodyV1::default(),
67        }
68    }
69
70    /// 设置任务标题
71    pub fn summary(mut self, summary: impl Into<String>) -> Self {
72        self.body.summary = summary.into();
73        self
74    }
75
76    /// 设置任务描述
77    pub fn description(mut self, description: impl Into<String>) -> Self {
78        self.body.description = Some(description.into());
79        self
80    }
81
82    /// 设置任务开始时间
83    pub fn start(mut self, start: impl Into<String>) -> Self {
84        self.body.start = Some(start.into());
85        self
86    }
87
88    /// 设置任务截止时间
89    pub fn due(mut self, due: impl Into<String>) -> Self {
90        self.body.due = Some(due.into());
91        self
92    }
93
94    /// 设置任务优先级(1-5)
95    pub fn priority(mut self, priority: i32) -> Self {
96        self.body.priority = Some(priority);
97        self
98    }
99
100    /// 执行请求
101    pub async fn execute(self) -> SDKResult<CreateTaskResponseV1> {
102        self.execute_with_options(openlark_core::req_option::RequestOption::default())
103            .await
104    }
105
106    /// 执行请求(带选项)
107    pub async fn execute_with_options(
108        self,
109        option: openlark_core::req_option::RequestOption,
110    ) -> SDKResult<CreateTaskResponseV1> {
111        validate_required!(self.body.summary.trim(), "任务标题不能为空");
112
113        let api_endpoint = crate::common::api_endpoints::TaskApiV1::TaskCreate;
114        let mut request = ApiRequest::<CreateTaskResponseV1>::post(api_endpoint.to_url());
115
116        let body_json = serde_json::to_value(&self.body).map_err(|e| {
117            openlark_core::error::validation_error("序列化请求体失败", e.to_string().as_str())
118        })?;
119
120        request = request.body(body_json);
121
122        let response =
123            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
124        response.data.ok_or_else(|| {
125            openlark_core::error::validation_error("响应数据为空", "服务器没有返回有效的数据")
126        })
127    }
128}
129
130impl ApiResponseTrait for CreateTaskResponseV1 {
131    fn data_format() -> ResponseFormat {
132        ResponseFormat::Data
133    }
134}
135
136#[cfg(test)]
137#[allow(unused_imports)]
138mod tests {
139    use std::sync::Arc;
140
141    use super::*;
142
143    #[test]
144    fn test_create_task_v1_builder() {
145        let config = Arc::new(
146            openlark_core::config::Config::builder()
147                .app_id("test")
148                .app_secret("test")
149                .build(),
150        );
151
152        let request = CreateTaskRequestV1::new(config)
153            .summary("测试任务")
154            .description("任务描述")
155            .priority(3);
156
157        assert_eq!(request.body.summary, "测试任务");
158        assert_eq!(request.body.description, Some("任务描述".to_string()));
159        assert_eq!(request.body.priority, Some(3));
160    }
161
162    #[test]
163    fn test_task_api_v1_url() {
164        let endpoint = crate::common::api_endpoints::TaskApiV1::TaskCreate;
165        assert_eq!(endpoint.to_url(), "/open-apis/task/v1/tasks");
166    }
167}