Skip to main content

openlark_workflow/v2/task/
add_dependencies.rs

1//! 添加任务依赖
2//!
3//! docPath: <https://open.feishu.cn/document/server-docs/docs/task-v2/task-add_dependencies/create>
4
5use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
6use openlark_core::{
7    SDKResult,
8    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
9    config::Config,
10    validate_required,
11};
12use serde::{Deserialize, Serialize};
13use std::sync::Arc;
14
15/// 添加任务依赖请求体
16#[derive(Debug, Clone, Serialize, Default)]
17pub struct AddDependenciesBody {
18    /// 依赖的任务 GUID 列表
19    pub dependencies: Vec<String>,
20}
21
22/// 任务依赖信息
23#[derive(Debug, Clone, Deserialize)]
24pub struct TaskDependency {
25    /// 依赖 GUID
26    pub dependency_guid: String,
27    /// 依赖的任务 GUID
28    pub dependent_task_guid: String,
29    /// 创建时间
30    pub created_at: String,
31}
32
33/// 添加任务依赖响应
34#[derive(Debug, Clone, Deserialize)]
35pub struct AddDependenciesResponse {
36    /// 任务 GUID
37    pub task_guid: String,
38    /// 添加的依赖列表
39    #[serde(default)]
40    pub dependencies: Vec<TaskDependency>,
41}
42
43/// 添加任务依赖请求
44#[derive(Debug, Clone)]
45pub struct AddDependenciesRequest {
46    /// 配置信息
47    config: Arc<Config>,
48    /// 任务 GUID
49    task_guid: String,
50    /// 请求体
51    body: AddDependenciesBody,
52}
53
54impl AddDependenciesRequest {
55    /// 创建新的请求构建器。
56    pub fn new(config: Arc<Config>, task_guid: impl Into<String>) -> Self {
57        Self {
58            config,
59            task_guid: task_guid.into(),
60            body: AddDependenciesBody::default(),
61        }
62    }
63
64    /// 设置依赖的任务 GUID 列表
65    pub fn dependencies(mut self, dependencies: Vec<String>) -> Self {
66        self.body.dependencies = dependencies;
67        self
68    }
69
70    /// 添加单个依赖任务
71    pub fn add_dependency(mut self, dependency_task_guid: impl Into<String>) -> Self {
72        self.body.dependencies.push(dependency_task_guid.into());
73        self
74    }
75
76    /// 执行请求
77    pub async fn execute(self) -> SDKResult<AddDependenciesResponse> {
78        self.execute_with_options(openlark_core::req_option::RequestOption::default())
79            .await
80    }
81
82    /// 执行请求(带选项)
83    pub async fn execute_with_options(
84        self,
85        option: openlark_core::req_option::RequestOption,
86    ) -> SDKResult<AddDependenciesResponse> {
87        // 验证必填字段
88        validate_required!(self.task_guid.trim(), "任务GUID不能为空");
89        validate_required!(self.body.dependencies, "依赖任务列表不能为空");
90
91        let api_endpoint = TaskApiV2::TaskAddDependencies(self.task_guid.clone());
92        let mut request = ApiRequest::<AddDependenciesResponse>::post(api_endpoint.to_url());
93
94        let request_body = &self.body;
95        request = request.body(serialize_params(request_body, "添加任务依赖")?);
96
97        openlark_core::http::Transport::request_typed(
98            request,
99            &self.config,
100            Some(option),
101            "添加任务依赖",
102        )
103        .await
104    }
105}
106
107impl ApiResponseTrait for AddDependenciesResponse {
108    fn data_format() -> ResponseFormat {
109        ResponseFormat::Data
110    }
111}
112
113#[cfg(test)]
114#[allow(unused_imports)]
115mod tests {
116    use std::sync::Arc;
117
118    use super::*;
119
120    #[test]
121    fn test_add_dependencies_builder() {
122        let config = Arc::new(
123            openlark_core::config::Config::builder()
124                .app_id("test")
125                .app_secret("test")
126                .build(),
127        );
128
129        let request = AddDependenciesRequest::new(config, "task_123")
130            .dependencies(vec!["task_456".to_string(), "task_789".to_string()]);
131
132        assert_eq!(request.task_guid, "task_123");
133        assert_eq!(request.body.dependencies, vec!["task_456", "task_789"]);
134    }
135
136    #[test]
137    fn test_add_dependency_single() {
138        let config = Arc::new(
139            openlark_core::config::Config::builder()
140                .app_id("test")
141                .app_secret("test")
142                .build(),
143        );
144
145        let request = AddDependenciesRequest::new(config, "task_123")
146            .add_dependency("task_456")
147            .add_dependency("task_789");
148
149        assert_eq!(request.body.dependencies, vec!["task_456", "task_789"]);
150    }
151
152    #[test]
153    fn test_task_add_dependencies_api_v2_url() {
154        let endpoint = TaskApiV2::TaskAddDependencies("task_123".to_string());
155        assert_eq!(
156            endpoint.to_url(),
157            "/open-apis/task/v2/tasks/task_123/add_dependencies"
158        );
159    }
160}