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 section_guid: String,
22 body: UpdateSectionBody,
24}
25
26impl UpdateSectionRequest {
27 pub fn new(config: Arc<Config>, section_guid: String) -> Self {
29 Self {
30 config,
31 section_guid,
32 body: UpdateSectionBody::default(),
33 }
34 }
35
36 pub fn summary(mut self, summary: impl Into<String>) -> Self {
38 self.body.summary = Some(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<UpdateSectionResponse> {
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<UpdateSectionResponse> {
59 validate_required!(self.section_guid.trim(), "分组GUID不能为空");
61
62 let api_endpoint = TaskApiV2::SectionUpdate(self.section_guid.clone());
63 let mut request = ApiRequest::<UpdateSectionResponse>::patch(api_endpoint.to_url());
64
65 let request_body = &self.body;
66 request = request.body(serialize_params(request_body, "更新分组")?);
67
68 let response =
69 openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
70 extract_response_data(response, "更新分组")
71 }
72}
73
74impl ApiResponseTrait for UpdateSectionResponse {
75 fn data_format() -> ResponseFormat {
76 ResponseFormat::Data
77 }
78}
79
80#[cfg(test)]
81#[allow(unused_imports)]
82mod tests {
83 use std::sync::Arc;
84
85 use super::*;
86
87 #[test]
88 fn test_update_section_builder() {
89 let config = Arc::new(
90 openlark_core::config::Config::builder()
91 .app_id("test")
92 .app_secret("test")
93 .build(),
94 );
95
96 let request =
97 UpdateSectionRequest::new(config, "section_456".to_string()).summary("更新的标题");
98
99 assert_eq!(request.section_guid, "section_456");
100 assert_eq!(request.body.summary, Some("更新的标题".to_string()));
101 }
102}