Skip to main content

openlark_workflow/v2/section/
update.rs

1//! 更新分组
2//!
3//! docPath: <https://open.feishu.cn/document/server-docs/docs/task-v2/section/update>
4
5use 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/// 更新分组请求
16#[derive(Debug, Clone)]
17pub struct UpdateSectionRequest {
18    /// 配置信息
19    config: Arc<Config>,
20    /// 分组 GUID
21    section_guid: String,
22    /// 请求体
23    body: UpdateSectionBody,
24}
25
26impl UpdateSectionRequest {
27    /// 创建新的请求构建器。
28    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    /// 设置分组标题
37    pub fn summary(mut self, summary: impl Into<String>) -> Self {
38        self.body.summary = Some(summary.into());
39        self
40    }
41
42    /// 设置分组描述
43    pub fn description(mut self, description: impl Into<String>) -> Self {
44        self.body.description = Some(description.into());
45        self
46    }
47
48    /// 执行请求
49    pub async fn execute(self) -> SDKResult<UpdateSectionResponse> {
50        self.execute_with_options(openlark_core::req_option::RequestOption::default())
51            .await
52    }
53
54    /// 执行请求(带选项)
55    pub async fn execute_with_options(
56        self,
57        option: openlark_core::req_option::RequestOption,
58    ) -> SDKResult<UpdateSectionResponse> {
59        // 验证必填字段
60        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}