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        let response =
98            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
99        extract_response_data(response, "添加任务依赖")
100    }
101}
102
103impl ApiResponseTrait for AddDependenciesResponse {
104    fn data_format() -> ResponseFormat {
105        ResponseFormat::Data
106    }
107}
108
109#[cfg(test)]
110#[allow(unused_imports)]
111mod tests {
112    use std::sync::Arc;
113
114    use super::*;
115
116    #[test]
117    fn test_add_dependencies_builder() {
118        let config = Arc::new(
119            openlark_core::config::Config::builder()
120                .app_id("test")
121                .app_secret("test")
122                .build(),
123        );
124
125        let request = AddDependenciesRequest::new(config, "task_123")
126            .dependencies(vec!["task_456".to_string(), "task_789".to_string()]);
127
128        assert_eq!(request.task_guid, "task_123");
129        assert_eq!(request.body.dependencies, vec!["task_456", "task_789"]);
130    }
131
132    #[test]
133    fn test_add_dependency_single() {
134        let config = Arc::new(
135            openlark_core::config::Config::builder()
136                .app_id("test")
137                .app_secret("test")
138                .build(),
139        );
140
141        let request = AddDependenciesRequest::new(config, "task_123")
142            .add_dependency("task_456")
143            .add_dependency("task_789");
144
145        assert_eq!(request.body.dependencies, vec!["task_456", "task_789"]);
146    }
147
148    #[test]
149    fn test_task_add_dependencies_api_v2_url() {
150        let endpoint = TaskApiV2::TaskAddDependencies("task_123".to_string());
151        assert_eq!(
152            endpoint.to_url(),
153            "/open-apis/task/v2/tasks/task_123/add_dependencies"
154        );
155    }
156}