sentd 1.0.0

Official Rust SDK for the SENTD Email API
Documentation
use std::collections::HashMap;

use crate::client::Sentd;
use crate::error::Error;
use crate::types::*;

/// Emails API
pub struct Emails {
    client: Sentd,
}

impl Emails {
    pub(crate) fn new(client: Sentd) -> Self {
        Self { client }
    }

    /// Send an email
    pub async fn send(&self, request: SendEmailRequest) -> Result<SendEmailResponse, Error> {
        self.client.post("/api/send", &request).await
    }

    /// List emails
    pub async fn list(&self, params: Option<ListEmailsParams>) -> Result<ListEmailsResponse, Error> {
        let mut query = String::from("/api/emails");

        if let Some(p) = params {
            let mut parts = vec![];
            if let Some(limit) = p.limit {
                parts.push(format!("limit={}", limit));
            }
            if let Some(offset) = p.offset {
                parts.push(format!("offset={}", offset));
            }
            if let Some(status) = p.status {
                parts.push(format!("status={}", status));
            }
            if let Some(from) = p.from {
                parts.push(format!("from={}", from));
            }
            if let Some(to) = p.to {
                parts.push(format!("to={}", to));
            }
            if !parts.is_empty() {
                query = format!("{}?{}", query, parts.join("&"));
            }
        }

        self.client.get(&query).await
    }

    /// Get an email by ID
    pub async fn get(&self, id: &str) -> Result<GetEmailResponse, Error> {
        self.client.get(&format!("/api/emails/{}", id)).await
    }

    /// Cancel a scheduled email
    pub async fn cancel(&self, id: &str) -> Result<SuccessResponse, Error> {
        self.client.delete(&format!("/api/emails/{}", id)).await
    }

    /// Reschedule a scheduled email
    pub async fn reschedule(&self, id: &str, send_at: &str, timezone: Option<&str>) -> Result<GetEmailResponse, Error> {
        let mut body = HashMap::new();
        body.insert("send_at", send_at);
        if let Some(tz) = timezone {
            body.insert("send_at_timezone", tz);
        }

        self.client.patch(&format!("/api/emails/{}", id), &body).await
    }

    /// Resend a failed email
    pub async fn resend(&self, id: &str) -> Result<SendEmailResponse, Error> {
        self.client.post(&format!("/api/emails/{}/resend", id), &()).await
    }
}

/// Parameters for listing emails
#[derive(Debug, Default)]
pub struct ListEmailsParams {
    pub limit: Option<i32>,
    pub offset: Option<i32>,
    pub status: Option<String>,
    pub from: Option<String>,
    pub to: Option<String>,
}