openlark_workflow/v2/task/
add_reminders.rs1use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
6use openlark_core::{
7 SDKResult,
8 api::{ApiRequest, ApiResponseTrait, ResponseFormat},
9 config::Config,
10 validate_required,
11};
12use serde::{Deserialize, Serialize};
13use std::sync::Arc;
14
15#[derive(Debug, Clone, Serialize, Default)]
17pub struct AddRemindersBody {
18 pub reminders: Vec<String>,
20}
21
22#[derive(Debug, Clone, Deserialize)]
24pub struct TaskReminder {
25 pub reminder_guid: String,
27 pub remind_at: String,
29 pub created_at: String,
31}
32
33#[derive(Debug, Clone, Deserialize)]
35pub struct AddRemindersResponse {
36 pub task_guid: String,
38 #[serde(default)]
40 pub reminders: Vec<TaskReminder>,
41}
42
43#[derive(Debug, Clone)]
45pub struct AddRemindersRequest {
46 config: Arc<Config>,
48 task_guid: String,
50 body: AddRemindersBody,
52}
53
54impl AddRemindersRequest {
55 pub fn new(config: Arc<Config>, task_guid: impl Into<String>) -> Self {
57 Self {
58 config,
59 task_guid: task_guid.into(),
60 body: AddRemindersBody::default(),
61 }
62 }
63
64 pub fn reminders(mut self, reminders: Vec<String>) -> Self {
66 self.body.reminders = reminders;
67 self
68 }
69
70 pub fn add_reminder(mut self, reminder: impl Into<String>) -> Self {
72 self.body.reminders.push(reminder.into());
73 self
74 }
75
76 pub async fn execute(self) -> SDKResult<AddRemindersResponse> {
78 self.execute_with_options(openlark_core::req_option::RequestOption::default())
79 .await
80 }
81
82 pub async fn execute_with_options(
84 self,
85 option: openlark_core::req_option::RequestOption,
86 ) -> SDKResult<AddRemindersResponse> {
87 validate_required!(self.task_guid.trim(), "任务GUID不能为空");
89 validate_required!(self.body.reminders, "提醒时间列表不能为空");
90
91 let api_endpoint = TaskApiV2::TaskAddReminders(self.task_guid.clone());
92 let mut request = ApiRequest::<AddRemindersResponse>::post(api_endpoint.to_url());
93
94 let request_body = &self.body;
95 request = request.body(serialize_params(request_body, "添加任务提醒")?);
96
97 openlark_core::http::Transport::request_typed(
98 request,
99 &self.config,
100 Some(option),
101 "添加任务提醒",
102 )
103 .await
104 }
105}
106
107impl ApiResponseTrait for AddRemindersResponse {
108 fn data_format() -> ResponseFormat {
109 ResponseFormat::Data
110 }
111}
112
113#[cfg(test)]
114#[allow(unused_imports)]
115mod tests {
116 use std::sync::Arc;
117
118 use super::*;
119
120 #[test]
121 fn test_add_reminders_builder() {
122 let config = Arc::new(
123 openlark_core::config::Config::builder()
124 .app_id("test")
125 .app_secret("test")
126 .build(),
127 );
128
129 let request = AddRemindersRequest::new(config, "task_123")
130 .reminders(vec!["2024-01-01T09:00:00Z".to_string()]);
131
132 assert_eq!(request.task_guid, "task_123");
133 assert_eq!(request.body.reminders, vec!["2024-01-01T09:00:00Z"]);
134 }
135
136 #[test]
137 fn test_add_reminder_single() {
138 let config = Arc::new(
139 openlark_core::config::Config::builder()
140 .app_id("test")
141 .app_secret("test")
142 .build(),
143 );
144
145 let request = AddRemindersRequest::new(config, "task_123")
146 .add_reminder("2024-01-01T09:00:00Z")
147 .add_reminder("2024-01-02T09:00:00Z");
148
149 assert_eq!(
150 request.body.reminders,
151 vec!["2024-01-01T09:00:00Z", "2024-01-02T09:00:00Z"]
152 );
153 }
154
155 #[test]
156 fn test_task_add_reminders_api_v2_url() {
157 let endpoint = TaskApiV2::TaskAddReminders("task_123".to_string());
158 assert_eq!(
159 endpoint.to_url(),
160 "/open-apis/task/v2/tasks/task_123/add_reminders"
161 );
162 }
163}