qstash-rs 0.6.0

A Rust SDK for Upstash QStash
Documentation
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
    }
}