Skip to main content

openlark_workflow/v2/task/
delete.rs

1//! 删除任务
2//!
3//! docPath: <https://open.feishu.cn/document/server-docs/docs/task-v2/task/delete>
4
5use crate::common::api_endpoints::TaskApiV2;
6use crate::v2::task::models::DeleteTaskResponse;
7use openlark_core::{
8    SDKResult,
9    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
10    config::Config,
11    validate_required,
12};
13use std::sync::Arc;
14
15/// 删除任务请求
16#[derive(Debug, Clone)]
17pub struct DeleteTaskRequest {
18    /// 配置信息
19    config: Arc<Config>,
20    /// 任务 GUID
21    task_guid: String,
22}
23
24impl DeleteTaskRequest {
25    /// 创建新的请求构建器。
26    pub fn new(config: Arc<Config>, task_guid: String) -> Self {
27        Self { config, task_guid }
28    }
29
30    /// 执行请求
31    pub async fn execute(self) -> SDKResult<DeleteTaskResponse> {
32        self.execute_with_options(openlark_core::req_option::RequestOption::default())
33            .await
34    }
35
36    /// 执行请求(带选项)
37    pub async fn execute_with_options(
38        self,
39        option: openlark_core::req_option::RequestOption,
40    ) -> SDKResult<DeleteTaskResponse> {
41        // 验证必填字段
42        validate_required!(self.task_guid.trim(), "任务GUID不能为空");
43
44        let api_endpoint = TaskApiV2::TaskDelete(self.task_guid.clone());
45        let request = ApiRequest::<DeleteTaskResponse>::delete(api_endpoint.to_url());
46
47        openlark_core::http::Transport::request_typed(
48            request,
49            &self.config,
50            Some(option),
51            "删除任务",
52        )
53        .await
54    }
55}
56
57impl ApiResponseTrait for DeleteTaskResponse {
58    fn data_format() -> ResponseFormat {
59        ResponseFormat::Data
60    }
61}
62
63#[cfg(test)]
64#[allow(unused_imports)]
65mod tests {
66    use std::sync::Arc;
67
68    use super::*;
69
70    #[test]
71    fn test_delete_task_request() {
72        let config = openlark_core::config::Config::builder()
73            .app_id("test")
74            .app_secret("test")
75            .build();
76
77        let request = DeleteTaskRequest::new(Arc::new(config), "task_123".to_string());
78
79        assert_eq!(request.task_guid, "task_123");
80    }
81}