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 let response =
85 openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
86 response.data.ok_or_else(|| {
87 openlark_core::error::validation_error("响应数据为空", "服务器没有返回有效的数据")
88 })
89 }
90}
91
92impl ApiResponseTrait for CompleteTaskResponseV1 {
93 fn data_format() -> ResponseFormat {
94 ResponseFormat::Data
95 }
96}
97
98#[cfg(test)]
99#[allow(unused_imports)]
100mod tests {
101 use std::sync::Arc;
102
103 use super::*;
104
105 #[test]
106 fn test_complete_task_v1_builder() {
107 let config = Arc::new(
108 openlark_core::config::Config::builder()
109 .app_id("test")
110 .app_secret("test")
111 .build(),
112 );
113
114 let request =
115 CompleteTaskRequestV1::new(config, "test_task_id").completed_at("2024-01-15T10:00:00Z");
116
117 assert_eq!(request.task_id, "test_task_id");
118 assert_eq!(
119 request.body.completed_at,
120 Some("2024-01-15T10:00:00Z".to_string())
121 );
122 }
123
124 #[test]
125 fn test_task_api_v1_complete_url() {
126 let endpoint =
127 crate::common::api_endpoints::TaskApiV1::TaskComplete("task_123".to_string());
128 assert_eq!(
129 endpoint.to_url(),
130 "/open-apis/task/v1/tasks/task_123/complete"
131 );
132 }
133}