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    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
9    config::Config,
10    validate_required, SDKResult,
11};
12use std::sync::Arc;
13
14/// 更新分组请求
15#[derive(Debug, Clone)]
16pub struct UpdateSectionRequest {
17    /// 配置信息
18    config: Arc<Config>,
19    /// 任务清单 GUID
20    tasklist_guid: String,
21    /// 分组 GUID
22    section_guid: String,
23    /// 请求体
24    body: UpdateSectionBody,
25}
26
27impl UpdateSectionRequest {
28    pub fn new(config: Arc<Config>, tasklist_guid: String, section_guid: String) -> Self {
29        Self {
30            config,
31            tasklist_guid,
32            section_guid,
33            body: UpdateSectionBody::default(),
34        }
35    }
36
37    /// 设置分组标题
38    pub fn summary(mut self, summary: impl Into<String>) -> Self {
39        self.body.summary = Some(summary.into());
40        self
41    }
42
43    /// 设置分组描述
44    pub fn description(mut self, description: impl Into<String>) -> Self {
45        self.body.description = Some(description.into());
46        self
47    }
48
49    /// 执行请求
50    pub async fn execute(self) -> SDKResult<UpdateSectionResponse> {
51        self.execute_with_options(openlark_core::req_option::RequestOption::default())
52            .await
53    }
54
55    /// 执行请求(带选项)
56    pub async fn execute_with_options(
57        self,
58        option: openlark_core::req_option::RequestOption,
59    ) -> SDKResult<UpdateSectionResponse> {
60        // 验证必填字段
61        validate_required!(self.tasklist_guid.trim(), "任务清单GUID不能为空");
62        validate_required!(self.section_guid.trim(), "分组GUID不能为空");
63
64        let api_endpoint =
65            TaskApiV2::SectionUpdate(self.tasklist_guid.clone(), self.section_guid.clone());
66        let mut request = ApiRequest::<UpdateSectionResponse>::put(api_endpoint.to_url());
67
68        let request_body = &self.body;
69        request = request.body(serialize_params(request_body, "更新分组")?);
70
71        let response =
72            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
73        extract_response_data(response, "更新分组")
74    }
75}
76
77impl ApiResponseTrait for UpdateSectionResponse {
78    fn data_format() -> ResponseFormat {
79        ResponseFormat::Data
80    }
81}
82
83#[cfg(test)]
84#[allow(unused_imports)]
85mod tests {
86    use std::sync::Arc;
87
88    use super::*;
89
90    #[test]
91    fn test_update_section_builder() {
92        let config = Arc::new(
93            openlark_core::config::Config::builder()
94                .app_id("test")
95                .app_secret("test")
96                .build(),
97        );
98
99        let request = UpdateSectionRequest::new(
100            config,
101            "tasklist_123".to_string(),
102            "section_456".to_string(),
103        )
104        .summary("更新的标题");
105
106        assert_eq!(request.tasklist_guid, "tasklist_123");
107        assert_eq!(request.section_guid, "section_456");
108        assert_eq!(request.body.summary, Some("更新的标题".to_string()));
109    }
110}