Skip to main content

openlark_workflow/v1/task/
patch.rs

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