postal-rs 0.1.0

A libary which provides an API to Postal
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! The crate provides an interface to [Postal]'s http [API].
//!
//! # Examples
//!
//! ```no_run
//! use postal_rs::{Client, DetailsInterest, Message, SendResult};
//! use std::env;
//! 
//! #[tokio::main]
//! async fn main() {
//!    let address = env::var("POSTAL_ADDRESS").unwrap_or_default();
//!    let token = env::var("POSTAL_TOKEN").unwrap_or_default();
//!
//!    let message = Message::default()
//!        .to(&["example@gmail.com".to_owned()])
//!        .from("test@yourserver.io")
//!        .subject("Hello World")
//!        .text("A test message");
//!    let client = Client::new(address, token).unwrap();
//!    let _ = client
//!        .send(message)
//!        .await
//!        .unwrap();
//!}
//!
//! ```
//!
//! [Postal]: https://postal.atech.media/
//! [API]: https://github.com/postalhq/postal/wiki/Using-the-API

mod error;

pub use error::PostalError;

use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use serde_json::Value as Json;
use std::collections::HashMap;
use url::Url;

/// Client holds a session information
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Client {
    address: Url,
    token: String,
}

impl Client {
    /// Constructs a new instance of client
    pub fn new<U, S>(url: U, token: S) -> Result<Self, PostalError>
    where
        U: AsRef<str>,
        S: Into<String>,
    {
        let url = Url::parse(url.as_ref())?;
        let token = token.into();

        Ok(Self {
            address: url,
            token,
        })
    }

    /// Sends a message to Postal
    pub async fn send<M: Into<Message>>(&self, message: M) -> Result<Vec<SendResult>, PostalError> {
        let address = self.address.join("/api/v1/send/message")?;
        let message = message.into();

        let client = reqwest::Client::new();
        let res = client
            .post(address)
            .json(&message)
            .header("X-Server-API-Key", &self.token)
            .send()
            .await?;

        handle_send(res).await
    }

    /// Sends a standart SMTP message to Postal
    pub async fn send_raw<M: Into<RawMessage>>(
        &self,
        message: M,
    ) -> Result<Vec<SendResult>, PostalError> {
        let address = self.address.join("/api/v1/send/raw")?;
        let message = message.into();

        let client = reqwest::Client::new();
        let res = client
            .post(address)
            .json(&message)
            .header("X-Server-API-Key", &self.token)
            .send()
            .await?;

        handle_send(res).await
    }

    /// Asks a Postal server to provide an information details
    /// about a message
    ///
    /// By default it provides a limited information.
    /// To increase this volume you can specify expansions via [DetailsInterest]
    ///
    /// [DetailsInterest]: ./struct.DetailsInterest.html
    pub async fn get_message_details<I: Into<DetailsInterest>>(
        &self,
        interest: I,
    ) -> Result<HashMap<String, Json>, PostalError> {
        let interest = interest.into();
        let address = self.address.join("/api/v1/messages/message")?;

        let client = reqwest::Client::new();
        let body: Json = interest.into();
        let res = client
            .post(address)
            .json(&body)
            .header("X-Server-API-Key", &self.token)
            .send()
            .await?;

        check_status(res.status())?;

        let data: api_structures::Responce<HashMap<String, Json>> = res.json().await?;
        let data = check_responce(data)?;

        Ok(data)
    }

    /// Obtains a delivery information according to a message.
    pub async fn get_message_deliveries(
        &self,
        id: MessageHash,
    ) -> Result<Vec<HashMap<String, Json>>, PostalError> {
        let address = self.address.join("/api/v1/messages/deliveries")?;

        let client = reqwest::Client::new();
        let body: Json = serde_json::json!({ "id": id });
        let res = client
            .post(address)
            .json(&body)
            .header("X-Server-API-Key", &self.token)
            .send()
            .await?;

        check_status(res.status())?;

        let data: api_structures::Responce<Vec<HashMap<String, Json>>> = res.json().await?;
        let data = check_responce(data)?;

        Ok(data)
    }
}

async fn handle_send(resp: reqwest::Response) -> Result<Vec<SendResult>, PostalError> {
    check_status(resp.status())?;

    let data: api_structures::Responce<api_structures::MessageSucessData> = resp.json().await?;
    let data = check_responce(data)?;

    let messages = data
        .messages
        .into_iter()
        .map(|(to, m)| SendResult { to, id: m.id })
        .collect();

    Ok(messages)
}

fn check_status(sc: StatusCode) -> Result<(), PostalError> {
    match sc {
        StatusCode::OK => Ok(()),
        StatusCode::INTERNAL_SERVER_ERROR => Err(PostalError::InternalServerError),
        StatusCode::MOVED_PERMANENTLY | StatusCode::PERMANENT_REDIRECT => {
            Err(PostalError::ExpectedAlternativeUrl)
        }
        StatusCode::SERVICE_UNAVAILABLE => Err(PostalError::ServiceUnavailableError),
        // according to postal docs it's imposible to get a different status code
        // https://krystal.github.io/postal-api/index.html
        _ => unreachable!(),
    }
}

fn check_responce<T>(data: api_structures::Responce<T>) -> Result<T, PostalError> {
    match data {
        api_structures::Responce::Success { data, .. } => Ok(data),
        api_structures::Responce::Error { data, .. } => Err(PostalError::Error {
            code: data.code,
            message: data.message,
        }),
        // the format of this error is unclear
        api_structures::Responce::ParameterError {} => unimplemented!(),
    }
}

/// MessageHash represents a hash which can be used to
/// get a different information bout a message.
pub type MessageHash = u64;

/// Message represents a email which can be sent
#[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
pub struct Message {
    ///The e-mail addresses of the recipients (max 50)
    pub to: Option<Vec<String>>,
    /// The e-mail addresses of any CC contacts (max 50)
    pub cc: Option<Vec<String>>,
    /// The e-mail addresses of any BCC contacts (max 50)
    pub bcc: Option<Vec<String>>,
    /// The e-mail address for the From header
    pub from: Option<String>,
    /// The e-mail address for the Sender header
    pub sender: Option<String>,
    /// The subject of the e-mail
    pub subject: Option<String>,
    /// The tag of the e-mail
    pub tag: Option<String>,
    /// Set the reply-to address for the mail
    pub reply_to: Option<String>,
    /// The plain text body of the e-mail
    pub plain_body: Option<String>,
    /// The HTML body of the e-mail
    pub html_body: Option<String>,
    /// An array of attachments for this e-mail
    pub attachments: Option<Vec<Vec<u8>>>,
    /// A hash of additional headers
    pub headers: Option<MessageHash>,
    /// Is this message a bounce?
    pub bounce: Option<bool>,
}

impl Message {
    pub fn from<S: Into<String>>(mut self, s: S) -> Self {
        self.from = Some(s.into());
        self
    }

    pub fn to(mut self, to: &[String]) -> Self {
        self.to = Some(to.to_vec());
        self
    }

    pub fn subject<S: Into<String>>(mut self, s: S) -> Self {
        self.subject = Some(s.into());
        self
    }

    pub fn text<S: Into<String>>(mut self, s: S) -> Self {
        self.plain_body = Some(s.into());
        self
    }

    pub fn html<S: Into<String>>(mut self, s: S) -> Self {
        self.html_body = Some(s.into());
        self
    }
}

/// RawMessage allows you to send us a raw RFC2822 formatted message along with
/// the recipients that it should be sent to.
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
pub struct RawMessage {
    /// The address that should be logged as sending the message
    pub mail_from: String,
    /// The addresses this message should be sent to
    pub rcpt_to: Vec<String>,
    /// A base64 encoded RFC2822 message to send
    pub data: String,
    /// Is this message a bounce?
    pub bounce: Option<bool>,
}

impl RawMessage {
    pub fn new<S1: Into<String>, S2: Into<String>>(to: &[String], from: S1, data: S2) -> Self {
        Self {
            rcpt_to: to.to_owned(),
            mail_from: from.into(),
            data: data.into(),
            bounce: None,
        }
    }
}

/// DetailsInterest contains an options which can be used to
/// turn on expansions while obtaining details of a message.
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
pub struct DetailsInterest {
    id: MessageHash,
    status: Option<()>,
    details: Option<()>,
    inspection: Option<()>,
    plain_body: Option<()>,
    html_body: Option<()>,
    attachments: Option<()>,
    headers: Option<()>,
    raw_message: Option<()>,
}

impl DetailsInterest {
    pub fn new(id: MessageHash) -> Self {
        id.into()
    }

    pub fn with_status(mut self) -> Self {
        self.status = Some(());
        self
    }

    pub fn with_details(mut self) -> Self {
        self.details = Some(());
        self
    }

    pub fn with_inspection(mut self) -> Self {
        self.inspection = Some(());
        self
    }

    pub fn with_plain_body(mut self) -> Self {
        self.plain_body = Some(());
        self
    }

    pub fn with_html_body(mut self) -> Self {
        self.html_body = Some(());
        self
    }

    pub fn with_headers(mut self) -> Self {
        self.headers = Some(());
        self
    }

    pub fn with_raw_message(mut self) -> Self {
        self.raw_message = Some(());
        self
    }

    fn build_expansions_list(&self) -> Option<Vec<Json>> {
        let mut expansions: Option<Vec<Json>> = None;
        if self.status.is_some() {
            expansions = Some(expansions.unwrap_or_default());
            expansions
                .as_mut()
                .unwrap()
                .push(Json::String("status".to_owned()));
        }
        if self.details.is_some() {
            expansions = Some(expansions.unwrap_or_default());
            expansions
                .as_mut()
                .unwrap()
                .push(Json::String("details".to_owned()));
        }
        if self.inspection.is_some() {
            expansions = Some(expansions.unwrap_or_default());
            expansions
                .as_mut()
                .unwrap()
                .push(Json::String("inspection".to_owned()));
        }
        if self.plain_body.is_some() {
            expansions = Some(expansions.unwrap_or_default());
            expansions
                .as_mut()
                .unwrap()
                .push(Json::String("plain_body".to_owned()));
        }
        if self.html_body.is_some() {
            expansions = Some(expansions.unwrap_or_default());
            expansions
                .as_mut()
                .unwrap()
                .push(Json::String("html_body".to_owned()));
        }
        if self.headers.is_some() {
            expansions = Some(expansions.unwrap_or_default());
            expansions
                .as_mut()
                .unwrap()
                .push(Json::String("headers".to_owned()));
        }
        if self.raw_message.is_some() {
            expansions = Some(expansions.unwrap_or_default());
            expansions
                .as_mut()
                .unwrap()
                .push(Json::String("raw_message".to_owned()));
        }

        expansions
    }
}

impl Into<DetailsInterest> for MessageHash {
    fn into(self) -> DetailsInterest {
        DetailsInterest {
            id: self,
            status: None,
            details: None,
            inspection: None,
            plain_body: None,
            html_body: None,
            attachments: None,
            headers: None,
            raw_message: None,
        }
    }
}

impl Into<Json> for DetailsInterest {
    fn into(self) -> Json {
        let mut map: HashMap<String, Json> = HashMap::new();
        map.insert("id".to_owned(), self.id.into());

        let expansions = self.build_expansions_list();
        if let Some(expansions) = expansions {
            map.insert("_expansions".to_owned(), Json::Array(expansions));
        }

        serde_json::json!(map)
    }
}

/// SendResult represent a result of sending request
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct SendResult {
    /// An email To which an email was sent
    pub to: String,
    /// A message id which can be used to retrieve message details
    /// and message deliveries
    pub id: MessageHash,
}

mod api_structures {
    use super::*;

    #[derive(Debug, Clone, Serialize, Deserialize)]
    #[serde(tag = "status", rename_all = "camelCase")]
    pub enum Responce<D> {
        Success {
            time: f64,
            flags: HashMap<String, u64>,
            data: D,
        },
        ParameterError {},
        Error {
            time: f64,
            flags: HashMap<String, u64>,
            data: ResponceError,
        },
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct MessageSucessData {
        pub message_id: String,
        pub messages: HashMap<String, MessageDataTo>,
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct MessageDataTo {
        pub id: u64,
        pub token: String,
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct ResponceError {
        pub code: String,
        pub message: String,
    }
}