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 tasklist_guid: String,
22 body: CreateSectionBody,
24}
25
26impl CreateSectionRequest {
27 pub fn new(config: Arc<Config>, tasklist_guid: String) -> Self {
29 Self {
30 config,
31 tasklist_guid,
32 body: CreateSectionBody::default(),
33 }
34 }
35
36 pub fn summary(mut self, summary: impl Into<String>) -> Self {
38 self.body.summary = summary.into();
39 self
40 }
41
42 pub fn description(mut self, description: impl Into<String>) -> Self {
44 self.body.description = Some(description.into());
45 self
46 }
47
48 pub async fn execute(self) -> SDKResult<CreateSectionResponse> {
50 self.execute_with_options(openlark_core::req_option::RequestOption::default())
51 .await
52 }
53
54 pub async fn execute_with_options(
56 self,
57 option: openlark_core::req_option::RequestOption,
58 ) -> SDKResult<CreateSectionResponse> {
59 validate_required!(self.tasklist_guid.trim(), "任务清单GUID不能为空");
61 validate_required!(self.body.summary.trim(), "分组标题不能为空");
62
63 let api_endpoint = TaskApiV2::SectionCreate(self.tasklist_guid.clone());
64 let mut request = ApiRequest::<CreateSectionResponse>::post(api_endpoint.to_url());
65
66 let request_body = &self.body;
67 request = request.body(serialize_params(request_body, "创建分组")?);
68
69 let response =
70 openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
71 extract_response_data(response, "创建分组")
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 =
98 CreateSectionRequest::new(config, "tasklist_123".to_string()).summary("测试分组");
99
100 assert_eq!(request.tasklist_guid, "tasklist_123");
101 assert_eq!(request.body.summary, "测试分组");
102 }
103}