emailit 2.0.3

The official Rust SDK for the Emailit Email API
Documentation
//! Email sending and retrieval service.

use std::sync::Arc;

use crate::client::BaseClient;
use crate::collection::Collection;
use crate::error::Error;
use crate::types::{CreateEmailBaseOptions, Email, EmailBody, ListEmailsParams, UpdateEmailParams};

/// Service for sending emails and retrieving email details.
///
/// Accessed via [`Emailit::emails`](crate::Emailit::emails).
pub struct EmailService {
    pub(crate) client: Arc<BaseClient>,
}

impl EmailService {
    /// Sends an email.
    ///
    /// `POST /v2/emails`
    pub async fn send(&self, params: CreateEmailBaseOptions) -> Result<Email, Error> {
        self.client
            .request("POST", "/v2/emails", to_body(&params), None)
            .await
    }

    /// Lists emails with optional pagination.
    ///
    /// `GET /v2/emails`
    pub async fn list(&self, params: Option<ListEmailsParams>) -> Result<Collection<Email>, Error> {
        let query = params.map(|p| {
            let mut q = Vec::new();
            if let Some(page) = p.page {
                q.push(("page", page.to_string()));
            }
            if let Some(limit) = p.limit {
                q.push(("limit", limit.to_string()));
            }
            q
        });
        self.client
            .request::<Collection<Email>>("GET", "/v2/emails", None, query.as_deref())
            .await
    }

    /// Retrieves an email by ID.
    ///
    /// `GET /v2/emails/:id`
    pub async fn get(&self, id: &str) -> Result<Email, Error> {
        let path = format!("/v2/emails/{}", urlencoding::encode(id));
        self.client.request("GET", &path, None, None).await
    }

    /// Retrieves the raw source of an email.
    ///
    /// `GET /v2/emails/:id/raw`
    pub async fn get_raw(&self, id: &str) -> Result<Email, Error> {
        let path = format!("/v2/emails/{}/raw", urlencoding::encode(id));
        self.client.request("GET", &path, None, None).await
    }

    /// Retrieves the attachments of an email.
    ///
    /// `GET /v2/emails/:id/attachments`
    pub async fn get_attachments(&self, id: &str) -> Result<Collection<serde_json::Value>, Error> {
        let path = format!("/v2/emails/{}/attachments", urlencoding::encode(id));
        self.client.request("GET", &path, None, None).await
    }

    /// Retrieves the HTML and plain-text body of an email.
    ///
    /// `GET /v2/emails/:id/body`
    pub async fn get_body(&self, id: &str) -> Result<EmailBody, Error> {
        let path = format!("/v2/emails/{}/body", urlencoding::encode(id));
        self.client.request("GET", &path, None, None).await
    }

    /// Retrieves the metadata of an email.
    ///
    /// `GET /v2/emails/:id/meta`
    pub async fn get_meta(&self, id: &str) -> Result<Email, Error> {
        let path = format!("/v2/emails/{}/meta", urlencoding::encode(id));
        self.client.request("GET", &path, None, None).await
    }

    /// Updates a scheduled email (e.g. reschedule delivery).
    ///
    /// `POST /v2/emails/:id`
    pub async fn update(&self, id: &str, params: UpdateEmailParams) -> Result<Email, Error> {
        let path = format!("/v2/emails/{}", urlencoding::encode(id));
        self.client
            .request("POST", &path, to_body(&params), None)
            .await
    }

    /// Cancels a scheduled email.
    ///
    /// `POST /v2/emails/:id/cancel`
    pub async fn cancel(&self, id: &str) -> Result<Email, Error> {
        let path = format!("/v2/emails/{}/cancel", urlencoding::encode(id));
        self.client.request("POST", &path, None, None).await
    }

    /// Retries delivery of a failed email.
    ///
    /// `POST /v2/emails/:id/retry`
    pub async fn retry(&self, id: &str) -> Result<Email, Error> {
        let path = format!("/v2/emails/{}/retry", urlencoding::encode(id));
        self.client.request("POST", &path, None, None).await
    }
}

fn to_body(v: &impl serde::Serialize) -> Option<serde_json::Value> {
    serde_json::to_value(v).ok()
}