postmark/api/message_streams/
delete_suppression.rs1use std::borrow::Cow;
2
3use crate::api::message_streams::{StreamIdOrName, SuppressionStatusType};
4use crate::Endpoint;
5use serde::{Deserialize, Serialize};
6use typed_builder::TypedBuilder;
7
8#[derive(Debug, Clone, PartialEq, Serialize)]
9#[serde(rename_all = "PascalCase")]
10#[derive(TypedBuilder)]
11pub struct DeleteSuppressionRequest {
12 #[serde(skip)]
13 pub stream_id: StreamIdOrName,
14 pub suppressions: Vec<Emails>,
15}
16
17#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
18#[serde(rename_all = "PascalCase")]
19pub struct DeleteSuppressionResponse {
20 pub suppressions: Vec<DeleteSuppression>,
21}
22
23#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
24#[serde(rename_all = "PascalCase")]
25pub struct DeleteSuppression {
26 pub email_address: String,
27 pub status: SuppressionStatusType,
28 pub message: Option<String>,
29}
30
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32#[serde(rename_all = "PascalCase")]
33pub struct Emails {
34 pub email_address: String,
35}
36
37impl Endpoint for DeleteSuppressionRequest {
38 type Request = DeleteSuppressionRequest;
39 type Response = DeleteSuppressionResponse;
40
41 fn endpoint(&self) -> Cow<'static, str> {
42 format!("/message-streams/{}/suppressions/delete", self.stream_id).into()
43 }
44
45 fn body(&self) -> &Self::Request {
46 self
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use crate::reqwest::PostmarkClient;
53 use crate::Query;
54 use httptest::matchers::request;
55 use httptest::{responders::*, Expectation, Server};
56 use serde_json::json;
57
58 use super::*;
59
60 const STREAM_ID: &str = "my-stream-id";
61
62 const EMAIL_1: &str = "good.address@wildbit.com";
63 const EMAIL_2: &str = "not.suppressed@wildbit.com";
64 const EMAIL_3: &str = "spammy.address@wildbit.com";
65 #[tokio::test]
66 pub async fn create_new_server() {
67 let server = Server::run();
68
69 server.expect(
70 Expectation::matching(request::method_path(
71 "POST",
72 format!("/message-streams/{STREAM_ID}/suppressions/delete"),
73 ))
74 .respond_with(json_encoded(json!({
75 "Suppressions":[
76 {
77 "EmailAddress":EMAIL_1,
78 "Status":"Deleted",
79 "Message": null
80 },
81 {
82 "EmailAddress":EMAIL_2,
83 "Status":"Deleted",
84 "Message": null
85 },
86 {
87 "EmailAddress":EMAIL_3,
88 "Status":"Failed",
89 "Message": "You do not have the required authority to change this suppression."
90 }
91 ]
92 }))),
93 );
94
95 let client = PostmarkClient::builder()
96 .base_url(server.url("/").to_string())
97 .build();
98
99 let req = DeleteSuppressionRequest::builder()
100 .stream_id(StreamIdOrName::StreamId(String::from(STREAM_ID)))
101 .suppressions(Vec::from([
102 Emails {
103 email_address: String::from(EMAIL_1),
104 },
105 Emails {
106 email_address: String::from(EMAIL_2),
107 },
108 Emails {
109 email_address: String::from(EMAIL_3),
110 },
111 ]))
112 .build();
113
114 print!("{}\n", req.endpoint());
115
116 req.execute(&client)
117 .await
118 .expect("Should get a response and be able to json decode it");
119 }
120}