openlark_workflow/v2/task/
update.rs1use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
6use crate::v2::task::models::{UpdateTaskBody, UpdateTaskResponse};
7use openlark_core::{
8 SDKResult,
9 api::{ApiRequest, ApiResponseTrait, ResponseFormat},
10 config::Config,
11 validate_required,
12};
13use std::sync::Arc;
14
15#[derive(Debug, Clone)]
17pub struct UpdateTaskRequest {
18 config: Arc<Config>,
20 task_guid: String,
22 body: UpdateTaskBody,
24}
25
26impl UpdateTaskRequest {
27 pub fn new(config: Arc<Config>, task_guid: String) -> Self {
29 Self {
30 config,
31 task_guid,
32 body: UpdateTaskBody::default(),
33 }
34 }
35
36 pub fn summary(mut self, summary: impl Into<String>) -> Self {
38 self.body.summary = Some(summary.into());
39 self
40 }
41
42 pub fn description(mut self, description: impl Into<String>) -> Self {
44 self.body.description = Some(description.into());
45 self
46 }
47
48 pub fn start(mut self, start: impl Into<String>) -> Self {
50 self.body.start = Some(start.into());
51 self
52 }
53
54 pub fn due(mut self, due: impl Into<String>) -> Self {
56 self.body.due = Some(due.into());
57 self
58 }
59
60 pub fn priority(mut self, priority: i32) -> Self {
62 self.body.priority = Some(priority);
63 self
64 }
65
66 pub fn custom_fields(mut self, custom_fields: serde_json::Value) -> Self {
68 self.body.custom_fields = Some(custom_fields);
69 self
70 }
71
72 pub fn followers(mut self, followers: Vec<String>) -> Self {
74 self.body.followers = Some(followers);
75 self
76 }
77
78 pub fn assignee(mut self, assignee: impl Into<String>) -> Self {
80 self.body.assignee = Some(assignee.into());
81 self
82 }
83
84 pub fn status(mut self, status: impl Into<String>) -> Self {
86 self.body.status = Some(status.into());
87 self
88 }
89
90 pub async fn execute(self) -> SDKResult<UpdateTaskResponse> {
92 self.execute_with_options(openlark_core::req_option::RequestOption::default())
93 .await
94 }
95
96 pub async fn execute_with_options(
98 self,
99 option: openlark_core::req_option::RequestOption,
100 ) -> SDKResult<UpdateTaskResponse> {
101 validate_required!(self.task_guid.trim(), "任务GUID不能为空");
103
104 let api_endpoint = TaskApiV2::TaskUpdate(self.task_guid.clone());
105 let mut request = ApiRequest::<UpdateTaskResponse>::patch(api_endpoint.to_url());
106
107 let request_body = &self.body;
108 request = request.body(serialize_params(request_body, "更新任务")?);
109
110 openlark_core::http::Transport::request_typed(
111 request,
112 &self.config,
113 Some(option),
114 "更新任务",
115 )
116 .await
117 }
118}
119
120impl ApiResponseTrait for UpdateTaskResponse {
121 fn data_format() -> ResponseFormat {
122 ResponseFormat::Data
123 }
124}
125
126#[cfg(test)]
127#[allow(unused_imports)]
128mod tests {
129 use std::sync::Arc;
130
131 use super::*;
132
133 #[test]
134 fn test_update_task_builder() {
135 let config = openlark_core::config::Config::builder()
136 .app_id("test")
137 .app_secret("test")
138 .build();
139
140 let request = UpdateTaskRequest::new(Arc::new(config), "task_123".to_string())
141 .summary("更新后的标题")
142 .priority(4);
143
144 assert_eq!(request.task_guid, "task_123");
145 assert_eq!(request.body.summary, Some("更新后的标题".to_string()));
146 }
147}