Skip to main content

openlark_workflow/v2/section/
create.rs

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