postmark/api/server/
create_server.rs

1use std::borrow::Cow;
2
3use crate::api::server::{DeliveryType, ServerColor};
4use crate::Endpoint;
5use serde::{Deserialize, Serialize};
6use typed_builder::TypedBuilder;
7
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
9#[serde(rename_all = "PascalCase")]
10#[derive(TypedBuilder)]
11pub struct CreateServerRequest {
12    #[builder(setter(into))]
13    pub name: String,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    #[builder(default, setter(into, strip_option))]
16    pub color: Option<ServerColor>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    #[builder(default, setter(into, strip_option))]
19    pub delivery_type: Option<DeliveryType>,
20    #[builder(default = true)]
21    pub smtp_api_activated: bool,
22}
23
24#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
25#[serde(rename_all = "PascalCase")]
26pub struct CreateServerResponse {
27    #[serde(rename = "ID")]
28    pub id: isize,
29    pub name: String,
30    pub api_tokens: Vec<String>,
31}
32
33impl Endpoint for CreateServerRequest {
34    type Request = CreateServerRequest;
35    type Response = CreateServerResponse;
36
37    fn endpoint(&self) -> Cow<'static, str> {
38        "/servers".into()
39    }
40
41    fn body(&self) -> &Self::Request {
42        self
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use httptest::matchers::request;
49    use httptest::{responders::*, Expectation, Server};
50    use serde_json::json;
51
52    use crate::reqwest::PostmarkClient;
53    use crate::Query;
54
55    use super::*;
56
57    const NAME: &str = "Staging Testing";
58
59    #[tokio::test]
60    pub async fn create_new_server() {
61        let server = Server::run();
62
63        server.expect(
64            Expectation::matching(request::method_path("POST", "/servers")).respond_with(
65                json_encoded(json!({
66                  "ID": 1,
67                  "Name": "Staging Testing",
68                  "ApiTokens": [
69                    "server token"
70                  ],
71                  "Color": "red",
72                  "SmtpApiActivated": true,
73                  "RawEmailEnabled": false,
74                  "DeliveryType": "Live",
75                  "ServerLink": "https://postmarkapp.com/servers/1/streams",
76                  "InboundAddress": "yourhash@inbound.postmarkapp.com",
77                  "InboundHookUrl": "http://hooks.example.com/inbound",
78                  "BounceHookUrl": "http://hooks.example.com/bounce",
79                  "OpenHookUrl": "http://hooks.example.com/open",
80                  "DeliveryHookUrl": "http://hooks.example.com/delivery",
81                  "PostFirstOpenOnly": false,
82                  "InboundDomain": "",
83                  "InboundHash": "yourhash",
84                  "InboundSpamThreshold": 5,
85                  "TrackOpens": false,
86                  "TrackLinks": "None",
87                  "IncludeBounceContentInHook": true,
88                  "ClickHookUrl": "http://hooks.example.com/click",
89                  "EnableSmtpApiErrorHooks": false
90                })),
91            ),
92        );
93
94        let client = PostmarkClient::builder()
95            .base_url(server.url("/").to_string())
96            .build();
97
98        let req = CreateServerRequest::builder().name(NAME).build();
99
100        assert_eq!(
101            serde_json::to_value(&req).unwrap(),
102            json!({
103                "Name": NAME,
104                "SmtpApiActivated": true,
105            })
106        );
107
108        req.execute(&client)
109            .await
110            .expect("Should get a response and be able to json decode it");
111    }
112}