Skip to main content

openlark_workflow/v2/tasklist/
create.rs

1//! 创建任务清单
2//!
3//! docPath: https://open.feishu.cn/document/server-docs/docs/task-v2/tasklist/create
4
5use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
6use crate::v2::tasklist::models::{CreateTasklistBody, CreateTasklistResponse, TasklistIcon};
7use openlark_core::{
8    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
9    config::Config,
10    validate_required, SDKResult,
11};
12use std::sync::Arc;
13
14/// 创建任务清单请求
15#[derive(Debug, Clone)]
16pub struct CreateTasklistRequest {
17    /// 配置信息
18    config: Arc<Config>,
19    /// 请求体
20    body: CreateTasklistBody,
21}
22
23impl CreateTasklistRequest {
24    pub fn new(config: Arc<Config>) -> Self {
25        Self {
26            config,
27            body: CreateTasklistBody::default(),
28        }
29    }
30
31    /// 设置任务清单标题
32    pub fn summary(mut self, summary: impl Into<String>) -> Self {
33        self.body.summary = summary.into();
34        self
35    }
36
37    /// 设置任务清单描述
38    pub fn description(mut self, description: impl Into<String>) -> Self {
39        self.body.description = Some(description.into());
40        self
41    }
42
43    /// 设置任务清单图标
44    pub fn icon(mut self, icon: TasklistIcon) -> Self {
45        self.body.icon = Some(icon);
46        self
47    }
48
49    /// 执行请求
50    pub async fn execute(self) -> SDKResult<CreateTasklistResponse> {
51        self.execute_with_options(openlark_core::req_option::RequestOption::default())
52            .await
53    }
54
55    /// 执行请求(带选项)
56    pub async fn execute_with_options(
57        self,
58        option: openlark_core::req_option::RequestOption,
59    ) -> SDKResult<CreateTasklistResponse> {
60        // 验证必填字段
61        validate_required!(self.body.summary.trim(), "任务清单标题不能为空");
62
63        let api_endpoint = TaskApiV2::TasklistCreate;
64        let mut request = ApiRequest::<CreateTasklistResponse>::post(api_endpoint.to_url());
65
66        let request_body = &self.body;
67        request = request.body(serialize_params(request_body, "创建任务清单")?);
68
69        let response =
70            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
71        extract_response_data(response, "创建任务清单")
72    }
73}
74
75impl ApiResponseTrait for CreateTasklistResponse {
76    fn data_format() -> ResponseFormat {
77        ResponseFormat::Data
78    }
79}
80
81#[cfg(test)]
82#[allow(unused_imports)]
83mod tests {
84    use std::sync::Arc;
85
86    use super::*;
87
88    #[test]
89    fn test_create_tasklist_builder() {
90        let config = Arc::new(
91            openlark_core::config::Config::builder()
92                .app_id("test")
93                .app_secret("test")
94                .build(),
95        );
96
97        let request = CreateTasklistRequest::new(config)
98            .summary("测试任务清单")
99            .description("任务清单描述");
100
101        assert_eq!(request.body.summary, "测试任务清单");
102        assert_eq!(request.body.description, Some("任务清单描述".to_string()));
103    }
104}