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 let response =
111 openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
112 extract_response_data(response, "更新任务")
113 }
114}
115
116impl ApiResponseTrait for UpdateTaskResponse {
117 fn data_format() -> ResponseFormat {
118 ResponseFormat::Data
119 }
120}
121
122#[cfg(test)]
123#[allow(unused_imports)]
124mod tests {
125 use std::sync::Arc;
126
127 use super::*;
128
129 #[test]
130 fn test_update_task_builder() {
131 let config = openlark_core::config::Config::builder()
132 .app_id("test")
133 .app_secret("test")
134 .build();
135
136 let request = UpdateTaskRequest::new(Arc::new(config), "task_123".to_string())
137 .summary("更新后的标题")
138 .priority(4);
139
140 assert_eq!(request.task_guid, "task_123");
141 assert_eq!(request.body.summary, Some("更新后的标题".to_string()));
142 }
143}