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
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?)
    }
}