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
use std::borrow::Cow;

use super::send_email::{SendEmailRequest, SendEmailResponse};
use crate::Endpoint;

/// Send multiple emails at once
pub type SendEmailBatchRequest = Vec<SendEmailRequest>;
/// Response for [`SendEmailBatchRequest`]
pub type SendEmailBatchResponse = Vec<SendEmailResponse>;

impl Endpoint for SendEmailBatchRequest {
    type Request = SendEmailBatchRequest;
    type Response = SendEmailBatchResponse;

    fn endpoint(&self) -> Cow<'static, str> {
        "/email/batch".into()
    }

    fn body(&self) -> &Self::Request {
        self
    }
}

#[cfg(test)]
mod tests {
    use httptest::matchers::request;
    use httptest::{responders::*, Expectation, Server};
    use serde_json::json;

    use crate::api::email::*;
    use crate::reqwest::PostmarkClient;
    use crate::Query;

    #[tokio::test]
    pub async fn send_email_test() {
        let server = Server::run();

        server.expect(
            Expectation::matching(request::method_path("POST", "/email/batch")).respond_with(
                json_encoded(json!([{
                    "To": "receiver@example.com",
                    "SubmittedAt": "2014-02-17T07:25:01.4178645-05:00",
                    "MessageID": "0a129aee-e1cd-480d-b08d-4f48548ff48d",
                    "ErrorCode": 0,
                    "Message": "OK"
                },{
                    "ErrorCode": 406,
                    "Message": "You tried to send to a recipient that has been marked as inactive. Found inactive addresses: example@example.com. Inactive recipients are ones that have generated a hard bounce, a spam complaint, or a manual suppression."
                }])),
            ),
        );

        let client = PostmarkClient::builder()
            .base_url(server.url("/").to_string())
            .build();

        let req_builder = SendEmailRequest::builder()
            .from("pa@example.com")
            .body(Body::Text("hello matt".into()))
            .subject("hello");

        let req: SendEmailBatchRequest = vec![
            req_builder.clone().to("mathieu@example.com").build(),
            req_builder.to("pa@example.com").build(),
        ];

        req.execute(&client)
            .await
            .expect("Should get a response and be able to json decode it");
    }
}