openlark_workflow/v1/task/
patch.rs1use openlark_core::{
6 SDKResult,
7 api::{ApiRequest, ApiResponseTrait, ResponseFormat},
8 config::Config,
9};
10use serde::{Deserialize, Serialize};
11use std::sync::Arc;
12
13#[derive(Debug, Clone, Serialize, Default)]
15pub struct UpdateTaskBodyV1 {
17 #[serde(skip_serializing_if = "Option::is_none")]
19 pub summary: Option<String>,
21
22 #[serde(skip_serializing_if = "Option::is_none")]
24 pub description: Option<String>,
26
27 #[serde(skip_serializing_if = "Option::is_none")]
29 pub start: Option<String>,
31
32 #[serde(skip_serializing_if = "Option::is_none")]
34 pub due: Option<String>,
36
37 #[serde(skip_serializing_if = "Option::is_none")]
39 pub priority: Option<i32>,
41}
42
43#[derive(Debug, Clone, Deserialize)]
45pub struct UpdateTaskResponseV1 {
47 pub id: String,
49 pub summary: String,
51 #[serde(default)]
53 pub description: Option<String>,
55 #[serde(default)]
57 pub start: Option<String>,
59 #[serde(default)]
61 pub due: Option<String>,
63 #[serde(default)]
65 pub priority: Option<i32>,
67 pub is_completed: bool,
69 pub created_at: String,
71 pub updated_at: String,
73}
74
75#[derive(Debug, Clone)]
77pub struct UpdateTaskRequestV1 {
79 config: Arc<Config>,
80 task_id: String,
81 body: UpdateTaskBodyV1,
82}
83
84impl UpdateTaskRequestV1 {
85 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 pub fn summary(mut self, summary: impl Into<String>) -> Self {
96 self.body.summary = Some(summary.into());
97 self
98 }
99
100 pub fn description(mut self, description: impl Into<String>) -> Self {
102 self.body.description = Some(description.into());
103 self
104 }
105
106 pub fn start(mut self, start: impl Into<String>) -> Self {
108 self.body.start = Some(start.into());
109 self
110 }
111
112 pub fn due(mut self, due: impl Into<String>) -> Self {
114 self.body.due = Some(due.into());
115 self
116 }
117
118 pub fn priority(mut self, priority: i32) -> Self {
120 self.body.priority = Some(priority);
121 self
122 }
123
124 pub async fn execute(self) -> SDKResult<UpdateTaskResponseV1> {
126 self.execute_with_options(openlark_core::req_option::RequestOption::default())
127 .await
128 }
129
130 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 openlark_core::http::Transport::request_typed(
145 request,
146 &self.config,
147 Some(option),
148 "响应数据为空",
149 )
150 .await
151 }
152}
153
154impl ApiResponseTrait for UpdateTaskResponseV1 {
155 fn data_format() -> ResponseFormat {
156 ResponseFormat::Data
157 }
158}
159
160#[cfg(test)]
161#[allow(unused_imports)]
162mod tests {
163 use std::sync::Arc;
164
165 use super::*;
166
167 #[test]
168 fn test_update_task_v1_builder() {
169 let config = Arc::new(
170 openlark_core::config::Config::builder()
171 .app_id("test")
172 .app_secret("test")
173 .build(),
174 );
175
176 let request = UpdateTaskRequestV1::new(config, "test_task_id")
177 .summary("更新后的任务标题")
178 .description("更新后的描述")
179 .priority(4);
180
181 assert_eq!(request.task_id, "test_task_id");
182 assert_eq!(request.body.summary, Some("更新后的任务标题".to_string()));
183 assert_eq!(request.body.description, Some("更新后的描述".to_string()));
184 assert_eq!(request.body.priority, Some(4));
185 }
186
187 #[test]
188 fn test_task_api_v1_update_url() {
189 let endpoint = crate::common::api_endpoints::TaskApiV1::TaskUpdate("task_123".to_string());
190 assert_eq!(endpoint.to_url(), "/open-apis/task/v1/tasks/task_123");
191 }
192}