postmark-client 0.2.0

Postmark API client using reqwest
Documentation
use crate::{Client, Result};
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct DeliveryStat {
    pub name: String,
    #[serde(rename = "Type")]
    pub bounce_type: Option<String>,
    pub count: i64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct DeliveryStatsResponse {
    pub inactive_mails: i64,
    pub bounces: Vec<DeliveryStat>,
}

#[derive(Debug, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct BouncesQueryParamaters {
    /// Number of bounces to return per request. Max 500. Count + Offset cannot exceed 10,000 bounces.
    pub count: u16,
    /// Number of bounces to skip. Count + Offset cannot exceed 10,000 bounces.
    pub offset: u16,
    /// Filter by [type of bounce](https://postmarkapp.com/developer/api/bounce-api#bounce-types)
    #[serde(rename = "type")]
    pub bounce_type: Option<String>,
    /// Filter by emails that were deactivated by Postmark due to the bounce. Set to true or false.
    /// If this isn’t specified it will return both active and inactive.
    pub inactive: Option<bool>,
    /// Filter by email address
    pub email_filter: Option<String>,
    /// Filter by tag
    pub tag: Option<String>,
    /// Filter by messageID
    pub message_id: Option<String>,
    /// Filter messages starting from the date/time specified (inclusive). e.g. 2021-01-01T12:00:00.
    /// Our API uses Eastern Time Zone.
    #[serde(rename = "fromdate")]
    pub from_date: Option<String>,
    /// Filter messages up to the date/time specified (inclusive). e.g. 2021-01-01T12:00:00.
    /// Our API uses Eastern Time Zone.
    #[serde(rename = "todate")]
    pub to_date: Option<String>,
    /// Filter by message stream ID. If not provided, message will default to the outbound transactional stream.
    pub message_stream: Option<String>,
}

impl BouncesQueryParamaters {
    /// Creates a new BouncesQueryParameters requiring the minimum parameters
    pub fn new(count: u16, offset: u16) -> BouncesQueryParamaters {
        BouncesQueryParamaters{
            count,
            offset,
            ..Default::default()
        }
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct BouncedEmail {
    #[serde(rename = "ID")]
    pub id: String,
    #[serde(rename = "Type")]
    pub bounce_type: String,
    pub type_code: String,
    pub name: String,
    pub tag: String,
    #[serde(rename = "MessageID")]
    pub message_id: String,
    #[serde(rename = "ServerID")]
    pub server_id: String,
    pub message_stream: String,
    pub description: String,
    pub details: String,
    pub email: String,
    pub from: String,
    pub bounced_at: String,
    pub dump_available: bool,
    pub inactive: bool,
    pub can_activate: bool,
    pub subject: String,
    pub content: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct BouncedEmailsReponse {
    pub total_count: u16,
    pub bounces: Vec<BouncedEmail>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct BounceDumpResponse {
    pub body: String,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ActivateBounceResponse {
    pub message: String,
    pub bounce: BouncedEmail,
}

impl Client {
    /// Lists all the bounce stats for the server the token is associated with.
    ///
    /// <https://postmarkapp.com/developer/api/bounce-api#delivery-stats>
    pub async fn get_delivery_stats(&self) -> Result<DeliveryStatsResponse> {
        Ok(self.get("/deliverystats").await?)
    }
    /// The bounces search allows you to return up-to 10,000 bounces in a search.
    /// For searches where you're looking to retrieve more than 10,000 bounces use
    /// parameters like todate and fromdate to filter the messages.
    ///
    /// <https://postmarkapp.com/developer/api/bounce-api#bounces>
    pub async fn get_bounces(
        &self,
        query: &BouncesQueryParamaters,
    ) -> Result<BouncedEmailsReponse> {
        Ok(self.get_with_query("/bounces", query).await?)
    }
    /// Gets a single bounced email using the provided ID.
    ///
    /// <https://postmarkapp.com/developer/api/bounce-api#single-bounce>
    pub async fn get_bounce(&self, bounce_id: &str) -> Result<BouncedEmail> {
        Ok(self
            .get(format!("/bounces/{:}", bounce_id).as_str())
            .await?)
    }
    /// Gets the SMTP dump for the bounced email.
    ///
    /// <https://postmarkapp.com/developer/api/bounce-api#bounce-dump>
    pub async fn get_bounce_dump(&self, bounce_id: &str) -> Result<BounceDumpResponse> {
        Ok(self
            .get(format!("/bounces/{:}/dump", bounce_id).as_str())
            .await?)
    }
    /// Reactivates thes email address in the bounced email
    ///
    /// <https://postmarkapp.com/developer/api/bounce-api#activate-bounce>
    pub async fn activate_bounce(&self, bounce_id: &str) -> Result<ActivateBounceResponse> {
        Ok(self
            .put(format!("/bounces/{:}/activate", bounce_id).as_str())
            .await?)
    }
}