Skip to main content

openlark_workflow/v2/section/
tasks.rs

1//! 获取自定义分组任务列表
2//!
3//! docPath: <https://open.feishu.cn/document/server-docs/docs/task-v2/section-tasks/list>
4
5use crate::common::api_endpoints::TaskApiV2;
6use crate::v2::task::models::TaskItem;
7use openlark_core::{
8    SDKResult,
9    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
10    config::Config,
11    validate_required,
12};
13use serde::{Deserialize, Serialize};
14use std::sync::Arc;
15
16/// 获取自定义分组任务列表响应
17#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
18pub struct ListSectionTasksResponse {
19    /// 是否还有更多项
20    #[serde(default)]
21    pub has_more: bool,
22
23    /// 分页标记
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub page_token: Option<String>,
26
27    /// 总数
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub total: Option<i32>,
30
31    /// 任务列表
32    #[serde(default)]
33    pub items: Vec<TaskItem>,
34}
35
36/// 获取自定义分组任务列表请求
37#[derive(Debug, Clone)]
38pub struct GetSectionTasksRequest {
39    /// 配置信息
40    config: Arc<Config>,
41    /// 分组 GUID
42    section_guid: String,
43    /// 分页大小
44    page_size: Option<i32>,
45    /// 分页标记
46    page_token: Option<String>,
47    /// 筛选条件(公式字符串)
48    filter: Option<String>,
49    /// 排序条件(JSON 数组)
50    sort: Option<serde_json::Value>,
51}
52
53impl GetSectionTasksRequest {
54    /// 创建新的实例。
55    pub fn new(config: Arc<Config>, section_guid: impl Into<String>) -> Self {
56        Self {
57            config,
58            section_guid: section_guid.into(),
59            page_size: None,
60            page_token: None,
61            filter: None,
62            sort: None,
63        }
64    }
65
66    /// 设置分页大小
67    pub fn page_size(mut self, page_size: i32) -> Self {
68        self.page_size = Some(page_size);
69        self
70    }
71
72    /// 设置分页标记
73    pub fn page_token(mut self, page_token: impl Into<String>) -> Self {
74        self.page_token = Some(page_token.into());
75        self
76    }
77
78    /// 设置筛选条件(公式字符串)
79    pub fn filter(mut self, filter: impl Into<String>) -> Self {
80        self.filter = Some(filter.into());
81        self
82    }
83
84    /// 设置排序条件(JSON 数组)
85    pub fn sort(mut self, sort: serde_json::Value) -> Self {
86        self.sort = Some(sort);
87        self
88    }
89
90    /// 执行请求
91    pub async fn execute(self) -> SDKResult<ListSectionTasksResponse> {
92        self.execute_with_options(openlark_core::req_option::RequestOption::default())
93            .await
94    }
95
96    /// 执行请求(带选项)
97    pub async fn execute_with_options(
98        self,
99        option: openlark_core::req_option::RequestOption,
100    ) -> SDKResult<ListSectionTasksResponse> {
101        // 验证必填字段
102        validate_required!(self.section_guid.trim(), "分组GUID不能为空");
103
104        let api_endpoint = TaskApiV2::SectionGetTasks(self.section_guid.clone());
105        let mut request = ApiRequest::<ListSectionTasksResponse>::get(api_endpoint.to_url());
106
107        // 构建查询参数
108        if let Some(page_size) = self.page_size {
109            request = request.query("page_size", page_size.to_string());
110        }
111        if let Some(page_token) = &self.page_token {
112            request = request.query("page_token", page_token);
113        }
114        if let Some(filter) = &self.filter {
115            request = request.query("filter", filter);
116        }
117        if let Some(sort) = &self.sort {
118            request = request.query("sort", sort.to_string());
119        }
120
121        openlark_core::http::Transport::request_typed(
122            request,
123            &self.config,
124            Some(option),
125            "获取自定义分组任务列表",
126        )
127        .await
128    }
129}
130
131impl ApiResponseTrait for ListSectionTasksResponse {
132    fn data_format() -> ResponseFormat {
133        ResponseFormat::Data
134    }
135}
136
137#[cfg(test)]
138#[allow(unused_imports)]
139mod tests {
140    use super::{GetSectionTasksRequest, TaskApiV2};
141    use std::sync::Arc;
142
143    #[test]
144    fn test_get_section_tasks_request() {
145        let config = openlark_core::config::Config::builder()
146            .app_id("test")
147            .app_secret("test")
148            .build();
149
150        let request = GetSectionTasksRequest::new(Arc::new(config), "section_123")
151            .page_size(20)
152            .filter("status = incomplete");
153
154        assert_eq!(request.section_guid, "section_123");
155        assert_eq!(request.page_size, Some(20));
156        assert_eq!(request.filter, Some("status = incomplete".to_string()));
157    }
158
159    #[test]
160    fn test_section_get_tasks_api_v2_url() {
161        let endpoint = TaskApiV2::SectionGetTasks("section_123".to_string());
162        assert_eq!(
163            endpoint.to_url(),
164            "/open-apis/task/v2/sections/section_123/tasks"
165        );
166    }
167}