Skip to main content

postmark/api/server/
create_server.rs

1use std::borrow::Cow;
2
3use crate::Endpoint;
4use crate::api::server::{DeliveryType, Server, ServerColor};
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
24impl Endpoint for CreateServerRequest {
25    type Request = CreateServerRequest;
26    type Response = Server;
27
28    fn endpoint(&self) -> Cow<'static, str> {
29        "/servers".into()
30    }
31
32    fn body(&self) -> &Self::Request {
33        self
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use httptest::matchers::request;
40    use httptest::{Expectation, Server, responders::*};
41    use serde_json::json;
42
43    use crate::Query;
44    use crate::reqwest::PostmarkClient;
45
46    use super::*;
47
48    const NAME: &str = "Staging Testing";
49
50    #[tokio::test]
51    pub async fn create_new_server() {
52        let server = Server::run();
53
54        server.expect(
55            Expectation::matching(request::method_path("POST", "/servers")).respond_with(
56                json_encoded(json!({
57                  "ID": 1,
58                  "Name": "Staging Testing",
59                  "ApiTokens": [
60                    "server token"
61                  ],
62                  "Color": "red",
63                  "SmtpApiActivated": true,
64                  "RawEmailEnabled": false,
65                  "DeliveryType": "Live",
66                  "ServerLink": "https://postmarkapp.com/servers/1/streams",
67                  "InboundAddress": "yourhash@inbound.postmarkapp.com",
68                  "InboundHookUrl": "http://hooks.example.com/inbound",
69                  "BounceHookUrl": "http://hooks.example.com/bounce",
70                  "OpenHookUrl": "http://hooks.example.com/open",
71                  "DeliveryHookUrl": "http://hooks.example.com/delivery",
72                  "PostFirstOpenOnly": false,
73                  "InboundDomain": "",
74                  "InboundHash": "yourhash",
75                  "InboundSpamThreshold": 5,
76                  "TrackOpens": false,
77                  "TrackLinks": "None",
78                  "IncludeBounceContentInHook": true,
79                  "ClickHookUrl": "http://hooks.example.com/click",
80                  "EnableSmtpApiErrorHooks": false
81                })),
82            ),
83        );
84
85        let client = PostmarkClient::builder()
86            .base_url(server.url("/").to_string())
87            .build();
88
89        let req = CreateServerRequest::builder().name(NAME).build();
90
91        assert_eq!(
92            serde_json::to_value(&req).unwrap(),
93            json!({
94                "Name": NAME,
95                "SmtpApiActivated": true,
96            })
97        );
98
99        req.execute(&client)
100            .await
101            .expect("Should get a response and be able to json decode it");
102    }
103}