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