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 openlark_core::http::Transport::request_typed(
69 request,
70 &self.config,
71 Some(option),
72 "更新分组",
73 )
74 .await
75 }
76}
77
78impl ApiResponseTrait for UpdateSectionResponse {
79 fn data_format() -> ResponseFormat {
80 ResponseFormat::Data
81 }
82}
83
84#[cfg(test)]
85#[allow(unused_imports)]
86mod tests {
87 use std::sync::Arc;
88
89 use super::*;
90
91 #[test]
92 fn test_update_section_builder() {
93 let config = Arc::new(
94 openlark_core::config::Config::builder()
95 .app_id("test")
96 .app_secret("test")
97 .build(),
98 );
99
100 let request =
101 UpdateSectionRequest::new(config, "section_456".to_string()).summary("更新的标题");
102
103 assert_eq!(request.section_guid, "section_456");
104 assert_eq!(request.body.summary, Some("更新的标题".to_string()));
105 }
106}