openlark_workflow/v1/task/reminder/
create.rs1use openlark_core::{
6 SDKResult,
7 api::{ApiRequest, ApiResponseTrait, ResponseFormat},
8 config::Config,
9};
10use serde::{Deserialize, Serialize};
11use std::sync::Arc;
12
13#[derive(Debug, Clone, Serialize, Default)]
15pub struct CreateTaskReminderBodyV1 {
17 pub trigger_time: i64,
19 pub type_: Option<String>,
21}
22
23#[derive(Debug, Clone, Deserialize)]
25pub struct CreateTaskReminderResponseV1 {
27 pub reminder_id: String,
29}
30
31#[derive(Debug, Clone)]
33pub struct CreateTaskReminderRequestV1 {
35 config: Arc<Config>,
36 task_id: String,
37 body: CreateTaskReminderBodyV1,
38}
39
40impl CreateTaskReminderRequestV1 {
41 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 pub fn trigger_time(mut self, trigger_time: i64) -> Self {
52 self.body.trigger_time = trigger_time;
53 self
54 }
55
56 pub fn type_(mut self, type_: impl Into<String>) -> Self {
58 self.body.type_ = Some(type_.into());
59 self
60 }
61
62 pub async fn execute(self) -> SDKResult<CreateTaskReminderResponseV1> {
64 self.execute_with_options(openlark_core::req_option::RequestOption::default())
65 .await
66 }
67
68 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 openlark_core::http::Transport::request_typed(
84 request,
85 &self.config,
86 Some(option),
87 "响应数据为空",
88 )
89 .await
90 }
91}
92
93impl ApiResponseTrait for CreateTaskReminderResponseV1 {
94 fn data_format() -> ResponseFormat {
95 ResponseFormat::Data
96 }
97}
98
99#[cfg(test)]
100#[allow(unused_imports)]
101mod tests {
102 use std::sync::Arc;
103
104 use super::*;
105
106 #[test]
107 fn test_create_task_reminder_v1_builder() {
108 let config = Arc::new(
109 openlark_core::config::Config::builder()
110 .app_id("test")
111 .app_secret("test")
112 .build(),
113 );
114
115 let request = CreateTaskReminderRequestV1::new(config.clone(), "task_123")
116 .trigger_time(1704067200)
117 .type_("absolute");
118
119 assert_eq!(request.body.trigger_time, 1704067200);
120 assert_eq!(request.body.type_, Some("absolute".to_string()));
121 }
122
123 #[test]
124 fn test_task_reminder_create_v1_url() {
125 let endpoint =
126 crate::common::api_endpoints::TaskApiV1::TaskReminderCreate("task_123".to_string());
127 assert_eq!(
128 endpoint.to_url(),
129 "/open-apis/task/v1/tasks/task_123/reminders"
130 );
131 }
132}