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 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}