openlark_workflow/v1/task/
complete.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 CompleteTaskBodyV1 {
17 #[serde(skip_serializing_if = "Option::is_none")]
19 pub completed_at: Option<String>,
21}
22
23#[derive(Debug, Clone, Deserialize)]
25pub struct CompleteTaskResponseV1 {
27 pub id: String,
29 pub summary: String,
31 pub is_completed: bool,
33 #[serde(default)]
35 pub completed_at: Option<String>,
37}
38
39#[derive(Debug, Clone)]
41pub struct CompleteTaskRequestV1 {
43 config: Arc<Config>,
44 task_id: String,
45 body: CompleteTaskBodyV1,
46}
47
48impl CompleteTaskRequestV1 {
49 pub fn new(config: Arc<Config>, task_id: impl Into<String>) -> Self {
51 Self {
52 config,
53 task_id: task_id.into(),
54 body: CompleteTaskBodyV1::default(),
55 }
56 }
57
58 pub fn completed_at(mut self, completed_at: impl Into<String>) -> Self {
60 self.body.completed_at = Some(completed_at.into());
61 self
62 }
63
64 pub async fn execute(self) -> SDKResult<CompleteTaskResponseV1> {
66 self.execute_with_options(openlark_core::req_option::RequestOption::default())
67 .await
68 }
69
70 pub async fn execute_with_options(
72 self,
73 option: openlark_core::req_option::RequestOption,
74 ) -> SDKResult<CompleteTaskResponseV1> {
75 let api_endpoint = crate::common::api_endpoints::TaskApiV1::TaskComplete(self.task_id);
76 let mut request = ApiRequest::<CompleteTaskResponseV1>::post(api_endpoint.to_url());
77
78 let body_json = serde_json::to_value(&self.body).map_err(|e| {
79 openlark_core::error::validation_error("序列化请求体失败", e.to_string().as_str())
80 })?;
81
82 request = request.body(body_json);
83
84 openlark_core::http::Transport::request_typed(
85 request,
86 &self.config,
87 Some(option),
88 "响应数据为空",
89 )
90 .await
91 }
92}
93
94impl ApiResponseTrait for CompleteTaskResponseV1 {
95 fn data_format() -> ResponseFormat {
96 ResponseFormat::Data
97 }
98}
99
100#[cfg(test)]
101#[allow(unused_imports)]
102mod tests {
103 use std::sync::Arc;
104
105 use super::*;
106
107 #[test]
108 fn test_complete_task_v1_builder() {
109 let config = Arc::new(
110 openlark_core::config::Config::builder()
111 .app_id("test")
112 .app_secret("test")
113 .build(),
114 );
115
116 let request =
117 CompleteTaskRequestV1::new(config, "test_task_id").completed_at("2024-01-15T10:00:00Z");
118
119 assert_eq!(request.task_id, "test_task_id");
120 assert_eq!(
121 request.body.completed_at,
122 Some("2024-01-15T10:00:00Z".to_string())
123 );
124 }
125
126 #[test]
127 fn test_task_api_v1_complete_url() {
128 let endpoint =
129 crate::common::api_endpoints::TaskApiV1::TaskComplete("task_123".to_string());
130 assert_eq!(
131 endpoint.to_url(),
132 "/open-apis/task/v1/tasks/task_123/complete"
133 );
134 }
135}