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    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
9    config::Config,
10    validate_required, SDKResult,
11};
12use std::sync::Arc;
13
14/// 获取分组列表请求
15#[derive(Debug, Clone)]
16pub struct ListSectionsRequest {
17    /// 配置信息
18    config: Arc<Config>,
19    /// 任务清单 GUID
20    tasklist_guid: String,
21    /// 分页大小
22    page_size: Option<i32>,
23    /// 分页标记
24    page_token: Option<String>,
25}
26
27impl ListSectionsRequest {
28    pub fn new(config: Arc<Config>, tasklist_guid: String) -> Self {
29        Self {
30            config,
31            tasklist_guid,
32            page_size: None,
33            page_token: None,
34        }
35    }
36
37    /// 设置分页大小
38    pub fn page_size(mut self, page_size: i32) -> Self {
39        self.page_size = Some(page_size);
40        self
41    }
42
43    /// 设置分页标记
44    pub fn page_token(mut self, page_token: impl Into<String>) -> Self {
45        self.page_token = Some(page_token.into());
46        self
47    }
48
49    /// 执行请求
50    pub async fn execute(self) -> SDKResult<ListSectionsResponse> {
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<ListSectionsResponse> {
60        // 验证必填字段
61        validate_required!(self.tasklist_guid.trim(), "任务清单GUID不能为空");
62
63        let api_endpoint = TaskApiV2::SectionList(self.tasklist_guid.clone());
64        let mut request = ApiRequest::<ListSectionsResponse>::get(api_endpoint.to_url());
65
66        // 构建查询参数
67        if let Some(page_size) = self.page_size {
68            request = request.query("page_size", page_size.to_string());
69        }
70        if let Some(page_token) = &self.page_token {
71            request = request.query("page_token", page_token);
72        }
73
74        let response =
75            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
76        extract_response_data(response, "获取分组列表")
77    }
78}
79
80impl ApiResponseTrait for ListSectionsResponse {
81    fn data_format() -> ResponseFormat {
82        ResponseFormat::Data
83    }
84}
85
86#[cfg(test)]
87#[allow(unused_imports)]
88mod tests {
89    use std::sync::Arc;
90
91    use super::*;
92
93    #[test]
94    fn test_list_sections_request() {
95        let config = openlark_core::config::Config::builder()
96            .app_id("test")
97            .app_secret("test")
98            .build();
99
100        let request =
101            ListSectionsRequest::new(Arc::new(config), "tasklist_123".to_string()).page_size(20);
102
103        assert_eq!(request.tasklist_guid, "tasklist_123");
104        assert_eq!(request.page_size, Some(20));
105    }
106}