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        let response =
66            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
67        extract_response_data(response, "创建分组")
68    }
69}
70
71impl ApiResponseTrait for CreateSectionResponse {
72    fn data_format() -> ResponseFormat {
73        ResponseFormat::Data
74    }
75}
76
77#[cfg(test)]
78#[allow(unused_imports)]
79mod tests {
80    use std::sync::Arc;
81
82    use super::*;
83
84    #[test]
85    fn test_create_section_builder() {
86        let config = Arc::new(
87            openlark_core::config::Config::builder()
88                .app_id("test")
89                .app_secret("test")
90                .build(),
91        );
92
93        let request = CreateSectionRequest::new(config).summary("测试分组");
94
95        assert_eq!(request.body.summary, "测试分组");
96    }
97}