Skip to main content

openlark_workflow/v1/task/
complete.rs

1//! 完成任务(v1)
2//!
3//! docPath: https://open.feishu.cn/document/server-docs/docs/task-v1/task/complete
4
5use openlark_core::{
6    SDKResult,
7    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
8    config::Config,
9};
10use serde::{Deserialize, Serialize};
11use std::sync::Arc;
12
13/// 完成任务请求体(v1)
14#[derive(Debug, Clone, Serialize, Default)]
15/// 完成任务请求体。
16pub struct CompleteTaskBodyV1 {
17    /// 完成时间(可选)
18    #[serde(skip_serializing_if = "Option::is_none")]
19    /// 完成时间。
20    pub completed_at: Option<String>,
21}
22
23/// 完成任务响应(v1)
24#[derive(Debug, Clone, Deserialize)]
25/// 完成任务响应。
26pub struct CompleteTaskResponseV1 {
27    /// 任务 ID
28    pub id: String,
29    /// 任务标题
30    pub summary: String,
31    /// 任务是否完成
32    pub is_completed: bool,
33    /// 完成时间
34    #[serde(default)]
35    /// 完成时间。
36    pub completed_at: Option<String>,
37}
38
39/// 完成任务请求(v1)
40#[derive(Debug, Clone)]
41/// 完成任务请求构建器。
42pub struct CompleteTaskRequestV1 {
43    config: Arc<Config>,
44    task_id: String,
45    body: CompleteTaskBodyV1,
46}
47
48impl CompleteTaskRequestV1 {
49    /// 创建新的请求构建器。
50    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    /// 设置完成时间
59    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    /// 执行请求
65    pub async fn execute(self) -> SDKResult<CompleteTaskResponseV1> {
66        self.execute_with_options(openlark_core::req_option::RequestOption::default())
67            .await
68    }
69
70    /// 执行请求(带选项)
71    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}