postmark/api/templates/
copy_templates.rs

1use std::borrow::Cow;
2
3use serde::{Deserialize, Serialize};
4use typed_builder::TypedBuilder;
5
6use crate::api::templates::{TemplateAction, TemplateType};
7use crate::Endpoint;
8
9#[derive(Debug, Clone, PartialEq, Serialize)]
10#[serde(rename_all = "PascalCase")]
11#[derive(TypedBuilder)]
12pub struct CopyTemplatesRequest {
13    #[serde(rename = "SourceServerID")]
14    pub source_server_id: isize,
15    #[serde(rename = "DestinationServerID")]
16    pub destination_server_id: isize,
17    #[builder(default = true)]
18    pub perform_changes: bool,
19}
20
21#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
22#[serde(rename_all = "PascalCase")]
23pub struct CopyTemplatesResponse {
24    pub total_count: isize,
25    pub templates: Vec<Template>,
26}
27
28#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
29#[serde(rename_all = "PascalCase")]
30pub struct Template {
31    pub action: TemplateAction,
32    pub template_id: isize,
33    pub alias: Option<String>,
34    pub name: String,
35    pub template_type: TemplateType,
36}
37
38impl Endpoint for CopyTemplatesRequest {
39    type Request = CopyTemplatesRequest;
40    type Response = CopyTemplatesResponse;
41
42    fn endpoint(&self) -> Cow<'static, str> {
43        "/templates/push".into()
44    }
45
46    fn body(&self) -> &Self::Request {
47        self
48    }
49
50    fn method(&self) -> http::Method {
51        http::Method::PUT
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use httptest::matchers::request;
58    use httptest::{responders::*, Expectation, Server};
59    use serde_json::json;
60
61    use crate::reqwest::PostmarkClient;
62    use crate::Query;
63
64    use super::*;
65
66    #[tokio::test]
67    pub async fn push_templates() {
68        let server = Server::run();
69
70        const SOURCE_SERVER: isize = 12345;
71        const DESTINATION_SERVER: isize = 23456;
72
73        server.expect(
74            Expectation::matching(request::method_path("PUT", "/templates/push")).respond_with(
75                json_encoded(json!({
76                    "TotalCount": 1,
77                    "Templates": [
78                        {
79                            "Action": "Create",
80                            "TemplateId": 7270,
81                            "Alias": "comment-notification",
82                            "Name": "Comment notification",
83                            "TemplateType": "Standard"
84                        }
85                    ]
86                })),
87            ),
88        );
89
90        let client = PostmarkClient::builder()
91            .base_url(server.url("/").to_string())
92            .build();
93
94        let req = CopyTemplatesRequest::builder()
95            .source_server_id(SOURCE_SERVER)
96            .destination_server_id(DESTINATION_SERVER)
97            .build();
98
99        assert_eq!(
100            serde_json::to_value(&req).unwrap(),
101            json!({
102                "SourceServerID": SOURCE_SERVER,
103                "DestinationServerID": DESTINATION_SERVER,
104                "PerformChanges": 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}