Skip to main content

postmark/api/webhooks/
get_webhook.rs

1use std::borrow::Cow;
2
3use crate::Endpoint;
4use crate::api::endpoint_with_path_segment;
5use crate::api::webhooks::{Webhook, WebhookId};
6use serde::Serialize;
7use typed_builder::TypedBuilder;
8
9#[derive(Debug, Clone, PartialEq, Serialize, TypedBuilder)]
10#[serde(rename_all = "PascalCase")]
11pub struct GetWebhookRequest {
12    #[builder(setter(into))]
13    #[serde(skip)]
14    pub webhook_id: WebhookId,
15}
16
17impl Endpoint for GetWebhookRequest {
18    type Request = GetWebhookRequest;
19    type Response = Webhook;
20
21    fn endpoint(&self) -> Cow<'static, str> {
22        endpoint_with_path_segment("/webhooks", &self.webhook_id.to_string())
23    }
24
25    fn body(&self) -> &Self::Request {
26        self
27    }
28
29    fn method(&self) -> http::Method {
30        http::Method::GET
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use httptest::matchers::request;
37    use httptest::{Expectation, Server, responders::*};
38    use serde_json::json;
39
40    use crate::Query;
41    use crate::reqwest::PostmarkClient;
42
43    use super::*;
44
45    #[tokio::test]
46    pub async fn get_webhook() {
47        let server = Server::run();
48
49        server.expect(
50            Expectation::matching(request::method_path("GET", "/webhooks/1234567")).respond_with(
51                json_encoded(json!({
52                    "ID": 1234567,
53                    "Url": "https://www.example.com/webhook-test-tracking",
54                    "MessageStream": "outbound",
55                    "HttpAuth": {
56                        "Username": "user",
57                        "Password": "pass"
58                    },
59                    "HttpHeaders": [{"Name": "name", "Value": "value"}],
60                    "Triggers": {
61                        "Open": {"Enabled": true, "PostFirstOpenOnly": false},
62                        "Click": {"Enabled": true},
63                        "Delivery": {"Enabled": true},
64                        "Bounce": {"Enabled": false, "IncludeContent": false},
65                        "SpamComplaint": {"Enabled": false, "IncludeContent": false},
66                        "SubscriptionChange": {"Enabled": true}
67                    }
68                })),
69            ),
70        );
71
72        let client = PostmarkClient::builder()
73            .base_url(server.url("/").to_string())
74            .build();
75
76        let req = GetWebhookRequest::builder().webhook_id(1234567).build();
77
78        let resp = req
79            .execute(&client)
80            .await
81            .expect("Should get a response and be able to json decode it");
82
83        assert_eq!(resp.webhook_id, 1234567);
84        assert_eq!(resp.url, "https://www.example.com/webhook-test-tracking");
85    }
86}