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