Skip to main content

openlark_workflow/v2/section/
list.rs

1//! 获取分组列表
2//!
3//! docPath: https://open.feishu.cn/document/server-docs/docs/task-v2/section/list
4
5use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
6use crate::v2::section::models::ListSectionsResponse;
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 ListSectionsRequest {
18    /// 配置信息
19    config: Arc<Config>,
20    /// 任务清单 GUID
21    tasklist_guid: String,
22    /// 分页大小
23    page_size: Option<i32>,
24    /// 分页标记
25    page_token: Option<String>,
26}
27
28impl ListSectionsRequest {
29    /// 创建新的请求构建器。
30    pub fn new(config: Arc<Config>, tasklist_guid: String) -> Self {
31        Self {
32            config,
33            tasklist_guid,
34            page_size: None,
35            page_token: None,
36        }
37    }
38
39    /// 设置分页大小
40    pub fn page_size(mut self, page_size: i32) -> Self {
41        self.page_size = Some(page_size);
42        self
43    }
44
45    /// 设置分页标记
46    pub fn page_token(mut self, page_token: impl Into<String>) -> Self {
47        self.page_token = Some(page_token.into());
48        self
49    }
50
51    /// 执行请求
52    pub async fn execute(self) -> SDKResult<ListSectionsResponse> {
53        self.execute_with_options(openlark_core::req_option::RequestOption::default())
54            .await
55    }
56
57    /// 执行请求(带选项)
58    pub async fn execute_with_options(
59        self,
60        option: openlark_core::req_option::RequestOption,
61    ) -> SDKResult<ListSectionsResponse> {
62        // 验证必填字段
63        validate_required!(self.tasklist_guid.trim(), "任务清单GUID不能为空");
64
65        let api_endpoint = TaskApiV2::SectionList(self.tasklist_guid.clone());
66        let mut request = ApiRequest::<ListSectionsResponse>::get(api_endpoint.to_url());
67
68        // 构建查询参数
69        if let Some(page_size) = self.page_size {
70            request = request.query("page_size", page_size.to_string());
71        }
72        if let Some(page_token) = &self.page_token {
73            request = request.query("page_token", page_token);
74        }
75
76        let response =
77            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
78        extract_response_data(response, "获取分组列表")
79    }
80}
81
82impl ApiResponseTrait for ListSectionsResponse {
83    fn data_format() -> ResponseFormat {
84        ResponseFormat::Data
85    }
86}
87
88#[cfg(test)]
89#[allow(unused_imports)]
90mod tests {
91    use std::sync::Arc;
92
93    use super::*;
94
95    #[test]
96    fn test_list_sections_request() {
97        let config = openlark_core::config::Config::builder()
98            .app_id("test")
99            .app_secret("test")
100            .build();
101
102        let request =
103            ListSectionsRequest::new(Arc::new(config), "tasklist_123".to_string()).page_size(20);
104
105        assert_eq!(request.tasklist_guid, "tasklist_123");
106        assert_eq!(request.page_size, Some(20));
107    }
108}