openlark_workflow/v2/section/
update.rs1use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
6use crate::v2::section::models::{UpdateSectionBody, UpdateSectionResponse};
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 UpdateSectionRequest {
18 config: Arc<Config>,
20 tasklist_guid: String,
22 section_guid: String,
24 body: UpdateSectionBody,
26}
27
28impl UpdateSectionRequest {
29 pub fn new(config: Arc<Config>, tasklist_guid: String, section_guid: String) -> Self {
31 Self {
32 config,
33 tasklist_guid,
34 section_guid,
35 body: UpdateSectionBody::default(),
36 }
37 }
38
39 pub fn summary(mut self, summary: impl Into<String>) -> Self {
41 self.body.summary = Some(summary.into());
42 self
43 }
44
45 pub fn description(mut self, description: impl Into<String>) -> Self {
47 self.body.description = Some(description.into());
48 self
49 }
50
51 pub async fn execute(self) -> SDKResult<UpdateSectionResponse> {
53 self.execute_with_options(openlark_core::req_option::RequestOption::default())
54 .await
55 }
56
57 pub async fn execute_with_options(
59 self,
60 option: openlark_core::req_option::RequestOption,
61 ) -> SDKResult<UpdateSectionResponse> {
62 validate_required!(self.tasklist_guid.trim(), "任务清单GUID不能为空");
64 validate_required!(self.section_guid.trim(), "分组GUID不能为空");
65
66 let api_endpoint =
67 TaskApiV2::SectionUpdate(self.tasklist_guid.clone(), self.section_guid.clone());
68 let mut request = ApiRequest::<UpdateSectionResponse>::put(api_endpoint.to_url());
69
70 let request_body = &self.body;
71 request = request.body(serialize_params(request_body, "更新分组")?);
72
73 let response =
74 openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
75 extract_response_data(response, "更新分组")
76 }
77}
78
79impl ApiResponseTrait for UpdateSectionResponse {
80 fn data_format() -> ResponseFormat {
81 ResponseFormat::Data
82 }
83}
84
85#[cfg(test)]
86#[allow(unused_imports)]
87mod tests {
88 use std::sync::Arc;
89
90 use super::*;
91
92 #[test]
93 fn test_update_section_builder() {
94 let config = Arc::new(
95 openlark_core::config::Config::builder()
96 .app_id("test")
97 .app_secret("test")
98 .build(),
99 );
100
101 let request = UpdateSectionRequest::new(
102 config,
103 "tasklist_123".to_string(),
104 "section_456".to_string(),
105 )
106 .summary("更新的标题");
107
108 assert_eq!(request.tasklist_guid, "tasklist_123");
109 assert_eq!(request.section_guid, "section_456");
110 assert_eq!(request.body.summary, Some("更新的标题".to_string()));
111 }
112}