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    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 CreateSectionRequest {
18    /// 配置信息
19    config: Arc<Config>,
20    /// 请求体
21    body: CreateSectionBody,
22}
23
24impl CreateSectionRequest {
25    /// 创建新的请求构建器。
26    pub fn new(config: Arc<Config>) -> Self {
27        Self {
28            config,
29            body: CreateSectionBody::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 async fn execute(self) -> SDKResult<CreateSectionResponse> {
47        self.execute_with_options(openlark_core::req_option::RequestOption::default())
48            .await
49    }
50
51    /// 执行请求(带选项)
52    pub async fn execute_with_options(
53        self,
54        option: openlark_core::req_option::RequestOption,
55    ) -> SDKResult<CreateSectionResponse> {
56        // 验证必填字段
57        validate_required!(self.body.summary.trim(), "分组标题不能为空");
58
59        let api_endpoint = TaskApiV2::SectionCreate;
60        let mut request = ApiRequest::<CreateSectionResponse>::post(api_endpoint.to_url());
61
62        let request_body = &self.body;
63        request = request.body(serialize_params(request_body, "创建分组")?);
64
65        openlark_core::http::Transport::request_typed(
66            request,
67            &self.config,
68            Some(option),
69            "创建分组",
70        )
71        .await
72    }
73}
74
75impl ApiResponseTrait for CreateSectionResponse {
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_section_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 = CreateSectionRequest::new(config).summary("测试分组");
98
99        assert_eq!(request.body.summary, "测试分组");
100    }
101}