Skip to main content

postmark/api/server/
get_server.rs

1use std::borrow::Cow;
2
3use serde::Serialize;
4use typed_builder::TypedBuilder;
5
6use crate::Endpoint;
7use crate::api::endpoint_with_path_segment;
8use crate::api::server::{Server, ServerIdOrName};
9
10#[derive(Debug, Clone, PartialEq, Serialize)]
11#[serde(rename_all = "PascalCase")]
12#[derive(TypedBuilder)]
13pub struct GetServerRequest {
14    #[builder(setter(into))]
15    #[serde(skip)]
16    pub server_id: ServerIdOrName,
17}
18
19impl Endpoint for GetServerRequest {
20    type Request = GetServerRequest;
21    type Response = Server;
22
23    fn endpoint(&self) -> Cow<'static, str> {
24        endpoint_with_path_segment("/servers", &self.server_id.to_string())
25    }
26
27    fn body(&self) -> &Self::Request {
28        self
29    }
30
31    fn method(&self) -> http::Method {
32        http::Method::GET
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use httptest::matchers::request;
39    use httptest::{Expectation, Server, responders::*};
40    use serde_json::json;
41
42    use crate::Query;
43    use crate::reqwest::PostmarkClient;
44
45    use super::*;
46
47    const SERVER_ID: i64 = 123456;
48    #[tokio::test]
49    pub async fn get_server() {
50        let server = Server::run();
51
52        server.expect(
53            Expectation::matching(request::method_path("GET", format!("/servers/{SERVER_ID}")))
54                .respond_with(json_encoded(json!({
55                  "ID": 1,
56                  "Name": "Staging Testing",
57                  "ApiTokens": [
58                    "server token"
59                  ],
60                  "Color": "red",
61                  "SmtpApiActivated": true,
62                  "RawEmailEnabled": false,
63                  "DeliveryType": "Live",
64                  "ServerLink": "https://postmarkapp.com/servers/1/streams",
65                  "InboundAddress": "yourhash@inbound.postmarkapp.com",
66                  "InboundHookUrl": "http://hooks.example.com/inbound",
67                  "BounceHookUrl": "http://hooks.example.com/bounce",
68                  "OpenHookUrl": "http://hooks.example.com/open",
69                  "DeliveryHookUrl": "http://hooks.example.com/delivery",
70                  "PostFirstOpenOnly": false,
71                  "InboundDomain": "",
72                  "InboundHash": "yourhash",
73                  "InboundSpamThreshold": 0,
74                  "TrackOpens": false,
75                  "TrackLinks": "None",
76                  "IncludeBounceContentInHook": true,
77                  "ClickHookUrl": "http://hooks.example.com/click",
78                  "EnableSmtpApiErrorHooks": false
79                }))),
80        );
81
82        let client = PostmarkClient::builder()
83            .base_url(server.url("/").to_string())
84            .build();
85
86        let req = GetServerRequest::builder()
87            .server_id(ServerIdOrName::ServerId(SERVER_ID.into()))
88            .build();
89
90        req.execute(&client)
91            .await
92            .expect("Should get a response and be able to json decode it");
93    }
94}