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