postmark_client/bounce.rs
1use crate::{Client, Result};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Deserialize)]
5#[serde(rename_all = "PascalCase")]
6pub struct DeliveryStat {
7 pub name: String,
8 #[serde(rename = "Type")]
9 pub bounce_type: Option<String>,
10 pub count: i64,
11}
12
13#[derive(Debug, Deserialize)]
14#[serde(rename_all = "PascalCase")]
15pub struct DeliveryStatsResponse {
16 /// Number of inactive emails
17 pub inactive_mails: i64,
18 /// List of [bounce types](https://postmarkapp.com/developer/api/bounce-api#bounce-types) with total counts.
19 pub bounces: Vec<DeliveryStat>,
20}
21
22#[derive(Debug, Serialize, Default)]
23#[serde(rename_all = "camelCase")]
24pub struct BouncesQueryParamaters {
25 /// Number of bounces to return per request. Max 500. Count + Offset cannot exceed 10,000 bounces.
26 pub count: u16,
27 /// Number of bounces to skip. Count + Offset cannot exceed 10,000 bounces.
28 pub offset: u16,
29 /// Filter by [type of bounce](https://postmarkapp.com/developer/api/bounce-api#bounce-types)
30 #[serde(rename = "type")]
31 pub bounce_type: Option<String>,
32 /// Filter by emails that were deactivated by Postmark due to the bounce. Set to true or false.
33 /// If this isn’t specified it will return both active and inactive.
34 pub inactive: Option<bool>,
35 /// Filter by email address
36 pub email_filter: Option<String>,
37 /// Filter by tag
38 pub tag: Option<String>,
39 /// Filter by messageID
40 pub message_id: Option<String>,
41 /// Filter messages starting from the date/time specified (inclusive). e.g. 2021-01-01T12:00:00.
42 /// Our API uses Eastern Time Zone.
43 #[serde(rename = "fromdate")]
44 pub from_date: Option<String>,
45 /// Filter messages up to the date/time specified (inclusive). e.g. 2021-01-01T12:00:00.
46 /// Our API uses Eastern Time Zone.
47 #[serde(rename = "todate")]
48 pub to_date: Option<String>,
49 /// Filter by message stream ID. If not provided, message will default to the outbound transactional stream.
50 pub message_stream: Option<String>,
51}
52
53impl BouncesQueryParamaters {
54 /// Creates a new BouncesQueryParameters requiring the minimum parameters
55 pub fn new(count: u16, offset: u16) -> BouncesQueryParamaters {
56 BouncesQueryParamaters {
57 count,
58 offset,
59 ..Default::default()
60 }
61 }
62}
63
64#[derive(Debug, Deserialize)]
65#[serde(rename_all = "PascalCase")]
66pub struct BouncedEmail {
67 /// ID of bounce
68 #[serde(rename = "ID")]
69 pub id: String,
70 /// [Bounce type](https://postmarkapp.com/developer/api/bounce-api#bounce-types)
71 #[serde(rename = "Type")]
72 pub bounce_type: String,
73 /// [Bounce type code](https://postmarkapp.com/developer/api/bounce-api#bounce-types)
74 pub type_code: String,
75 /// [Bounce type name](https://postmarkapp.com/developer/api/bounce-api#bounce-types)
76 pub name: String,
77 /// Tag name
78 pub tag: String,
79 /// ID of message
80 #[serde(rename = "MessageID")]
81 pub message_id: String,
82 /// ID of server that sent the message
83 #[serde(rename = "ServerID")]
84 pub server_id: String,
85 /// The outbound sending message stream for the message.
86 pub message_stream: String,
87 /// Description of bounce
88 pub description: String,
89 /// Details on the bounce
90 pub details: String,
91 /// Email address that bounced
92 pub email: String,
93 /// Original sender of the bounced message, if available. For example, spam
94 /// complaints do not include the original sender address.
95 pub from: String,
96 /// Timestamp of bounce
97 pub bounced_at: String,
98 /// Specifies whether or not you can get a [raw dump](https://postmarkapp.com/developer/api/bounce-api#bounce-dump)
99 /// from this bounce. Postmark doesn’t store bounce dumps older than 30 days.
100 pub dump_available: bool,
101 /// Specifies if the bounce caused Postmark to deactivate this email.
102 pub inactive: bool,
103 /// Specifies whether or not you are able to reactivate this email.
104 pub can_activate: bool,
105 /// Email subject
106 pub subject: String,
107 /// Bounce content
108 pub content: String,
109}
110
111#[derive(Debug, Deserialize)]
112#[serde(rename_all = "PascalCase")]
113pub struct BouncedEmailsReponse {
114 /// Number of records returned
115 pub total_count: u16,
116 /// List of individual bounces
117 pub bounces: Vec<BouncedEmail>,
118}
119#[derive(Debug, Deserialize)]
120#[serde(rename_all = "PascalCase")]
121pub struct BounceDumpResponse {
122 /// Raw source of bounce. If no dump is available this will return an empty string.
123 pub body: String,
124}
125#[derive(Debug, Deserialize)]
126#[serde(rename_all = "PascalCase")]
127pub struct ActivateBounceResponse {
128 /// Response message
129 pub message: String,
130 /// Bounce details
131 pub bounce: BouncedEmail,
132}
133
134impl Client {
135 /// Lists all the bounce stats for the server the token is associated with.
136 ///
137 /// <https://postmarkapp.com/developer/api/bounce-api#delivery-stats>
138 pub async fn get_delivery_stats(&self) -> Result<DeliveryStatsResponse> {
139 self.get("/deliverystats").await
140 }
141 /// The bounces search allows you to return up-to 10,000 bounces in a search.
142 /// For searches where you're looking to retrieve more than 10,000 bounces use
143 /// parameters like todate and fromdate to filter the messages.
144 ///
145 /// <https://postmarkapp.com/developer/api/bounce-api#bounces>
146 pub async fn get_bounces(
147 &self,
148 query: &BouncesQueryParamaters,
149 ) -> Result<BouncedEmailsReponse> {
150 self.get_with_query("/bounces", query).await
151 }
152 /// Gets a single bounced email using the provided ID.
153 ///
154 /// <https://postmarkapp.com/developer/api/bounce-api#single-bounce>
155 pub async fn get_bounce(&self, bounce_id: &str) -> Result<BouncedEmail> {
156 self.get(format!("/bounces/{:}", bounce_id).as_str()).await
157 }
158 /// Gets the SMTP dump for the bounced email.
159 ///
160 /// <https://postmarkapp.com/developer/api/bounce-api#bounce-dump>
161 pub async fn get_bounce_dump(&self, bounce_id: &str) -> Result<BounceDumpResponse> {
162 self.get(format!("/bounces/{:}/dump", bounce_id).as_str())
163 .await
164 }
165 /// Reactivates thes email address in the bounced email
166 ///
167 /// <https://postmarkapp.com/developer/api/bounce-api#activate-bounce>
168 pub async fn activate_bounce(&self, bounce_id: &str) -> Result<ActivateBounceResponse> {
169 self.put(format!("/bounces/{:}/activate", bounce_id).as_str())
170 .await
171 }
172}