1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use reqwest::Method;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use super::{request::build_schedule_headers, request::ScheduleRequest, Client};
use crate::error::Result;
/// A persisted QStash schedule.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Schedule {
/// Unique identifier for the schedule.
pub schedule_id: String,
/// Cron expression.
pub cron: String,
/// Destination URL or URL Group.
pub destination: String,
/// Creation timestamp in milliseconds.
pub created_at: u64,
/// Delivery method.
pub method: String,
/// Forwarded headers.
pub header: Option<HashMap<String, Vec<String>>>,
/// UTF-8 body when available.
pub body: Option<String>,
/// Base64-encoded body for non-UTF-8 payloads.
pub body_base64: Option<String>,
/// Retry count.
pub retries: Option<u32>,
/// Delay in seconds.
pub delay: Option<u32>,
/// Callback URL.
pub callback: Option<String>,
/// Failure callback URL.
pub failure_callback: Option<String>,
/// Queue name when the schedule publishes through a queue.
pub queue_name: Option<String>,
/// Caller IP recorded by QStash.
pub caller_ip: Option<String>,
/// Whether the schedule is paused.
pub is_paused: bool,
/// Flow control key used by the created message.
pub flow_control_key: Option<String>,
/// Flow control rate.
pub rate: Option<u32>,
/// Flow control period in seconds.
pub period: Option<u32>,
/// Flow control parallelism.
pub parallelism: Option<u32>,
/// Retry delay expression.
pub retry_delay_expression: Option<String>,
/// User-defined label.
pub label: Option<String>,
/// Last schedule trigger timestamp in milliseconds.
pub last_schedule_time: Option<u64>,
/// Next schedule trigger timestamp in milliseconds.
pub next_schedule_time: Option<u64>,
/// States of the most recently triggered messages.
pub last_schedule_states: Option<HashMap<String, String>>,
}
/// Response returned when a schedule is created.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScheduleCreateResponse {
/// Created schedule identifier.
pub schedule_id: String,
}
/// Schedule operations.
pub struct SchedulesApi<'a> {
pub(crate) client: &'a Client,
}
impl SchedulesApi<'_> {
/// Creates or updates a schedule.
pub async fn create(&self, request: ScheduleRequest) -> Result<ScheduleCreateResponse> {
self.client
.http
.send_json(
Method::POST,
&format!("v2/schedules/{}", request.destination.path_value()),
&[],
Some(build_schedule_headers(&request)?),
request.body,
)
.await
}
/// Retrieves a schedule by identifier.
pub async fn get(&self, schedule_id: &str) -> Result<Schedule> {
self.client
.http
.send_json(
Method::GET,
&format!("v2/schedules/{schedule_id}"),
&[],
None,
None,
)
.await
}
/// Lists all schedules.
pub async fn list(&self) -> Result<Vec<Schedule>> {
self.client
.http
.send_json(Method::GET, "v2/schedules", &[], None, None)
.await
}
/// Deletes a schedule.
pub async fn delete(&self, schedule_id: &str) -> Result<()> {
self.client
.http
.send_empty(
Method::DELETE,
&format!("v2/schedules/{schedule_id}"),
&[],
None,
None,
)
.await
}
/// Pauses a schedule.
pub async fn pause(&self, schedule_id: &str) -> Result<()> {
self.client
.http
.send_empty(
Method::POST,
&format!("v2/schedules/{schedule_id}/pause"),
&[],
None,
None,
)
.await
}
/// Resumes a schedule.
pub async fn resume(&self, schedule_id: &str) -> Result<()> {
self.client
.http
.send_empty(
Method::POST,
&format!("v2/schedules/{schedule_id}/resume"),
&[],
None,
None,
)
.await
}
}