openlark_workflow/v2/section/
create.rs1use 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#[derive(Debug, Clone)]
17pub struct CreateSectionRequest {
18 config: Arc<Config>,
20 body: CreateSectionBody,
22}
23
24impl CreateSectionRequest {
25 pub fn new(config: Arc<Config>) -> Self {
27 Self {
28 config,
29 body: CreateSectionBody::default(),
30 }
31 }
32
33 pub fn summary(mut self, summary: impl Into<String>) -> Self {
35 self.body.summary = summary.into();
36 self
37 }
38
39 pub fn description(mut self, description: impl Into<String>) -> Self {
41 self.body.description = Some(description.into());
42 self
43 }
44
45 pub async fn execute(self) -> SDKResult<CreateSectionResponse> {
47 self.execute_with_options(openlark_core::req_option::RequestOption::default())
48 .await
49 }
50
51 pub async fn execute_with_options(
53 self,
54 option: openlark_core::req_option::RequestOption,
55 ) -> SDKResult<CreateSectionResponse> {
56 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}