Skip to main content

openlark_workflow/v1/task/reminder/
create.rs

1//! 创建任务提醒(v1)
2//!
3//! docPath: https://open.feishu.cn/document/server-docs/docs/task-v1/taskreminder/create
4
5use openlark_core::{
6    SDKResult,
7    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
8    config::Config,
9};
10use serde::{Deserialize, Serialize};
11use std::sync::Arc;
12
13/// 创建任务提醒请求体(v1)
14#[derive(Debug, Clone, Serialize, Default)]
15/// 创建任务提醒请求体。
16pub struct CreateTaskReminderBodyV1 {
17    /// 提醒时间(Unix 时间戳)
18    pub trigger_time: i64,
19    /// 提醒类型
20    pub type_: Option<String>,
21}
22
23/// 创建任务提醒响应(v1)
24#[derive(Debug, Clone, Deserialize)]
25/// 创建任务提醒响应。
26pub struct CreateTaskReminderResponseV1 {
27    /// 提醒 ID
28    pub reminder_id: String,
29}
30
31/// 创建任务提醒请求(v1)
32#[derive(Debug, Clone)]
33/// 创建任务提醒请求构建器。
34pub struct CreateTaskReminderRequestV1 {
35    config: Arc<Config>,
36    task_id: String,
37    body: CreateTaskReminderBodyV1,
38}
39
40impl CreateTaskReminderRequestV1 {
41    /// 创建新的请求构建器。
42    pub fn new(config: Arc<Config>, task_id: impl Into<String>) -> Self {
43        Self {
44            config,
45            task_id: task_id.into(),
46            body: CreateTaskReminderBodyV1::default(),
47        }
48    }
49
50    /// 设置提醒时间(Unix 时间戳)
51    pub fn trigger_time(mut self, trigger_time: i64) -> Self {
52        self.body.trigger_time = trigger_time;
53        self
54    }
55
56    /// 设置提醒类型
57    pub fn type_(mut self, type_: impl Into<String>) -> Self {
58        self.body.type_ = Some(type_.into());
59        self
60    }
61
62    /// 执行请求
63    pub async fn execute(self) -> SDKResult<CreateTaskReminderResponseV1> {
64        self.execute_with_options(openlark_core::req_option::RequestOption::default())
65            .await
66    }
67
68    /// 执行请求(带选项)
69    pub async fn execute_with_options(
70        self,
71        option: openlark_core::req_option::RequestOption,
72    ) -> SDKResult<CreateTaskReminderResponseV1> {
73        let api_endpoint =
74            crate::common::api_endpoints::TaskApiV1::TaskReminderCreate(self.task_id.clone());
75        let mut request = ApiRequest::<CreateTaskReminderResponseV1>::post(api_endpoint.to_url());
76
77        let body_json = serde_json::to_value(&self.body).map_err(|e| {
78            openlark_core::error::validation_error("序列化请求体失败", e.to_string().as_str())
79        })?;
80
81        request = request.body(body_json);
82
83        let response =
84            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
85        response.data.ok_or_else(|| {
86            openlark_core::error::validation_error("响应数据为空", "服务器没有返回有效的数据")
87        })
88    }
89}
90
91impl ApiResponseTrait for CreateTaskReminderResponseV1 {
92    fn data_format() -> ResponseFormat {
93        ResponseFormat::Data
94    }
95}
96
97#[cfg(test)]
98#[allow(unused_imports)]
99mod tests {
100    use std::sync::Arc;
101
102    use super::*;
103
104    #[test]
105    fn test_create_task_reminder_v1_builder() {
106        let config = Arc::new(
107            openlark_core::config::Config::builder()
108                .app_id("test")
109                .app_secret("test")
110                .build(),
111        );
112
113        let request = CreateTaskReminderRequestV1::new(config.clone(), "task_123")
114            .trigger_time(1704067200)
115            .type_("absolute");
116
117        assert_eq!(request.body.trigger_time, 1704067200);
118        assert_eq!(request.body.type_, Some("absolute".to_string()));
119    }
120
121    #[test]
122    fn test_task_reminder_create_v1_url() {
123        let endpoint =
124            crate::common::api_endpoints::TaskApiV1::TaskReminderCreate("task_123".to_string());
125        assert_eq!(
126            endpoint.to_url(),
127            "/open-apis/task/v1/tasks/task_123/reminders"
128        );
129    }
130}