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        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}