Skip to main content

mailjet_rs/client/
response.rs

1use hyper::body::to_bytes;
2use hyper::Body;
3use serde::{Deserialize, Serialize};
4use serde_json::from_str;
5
6/// Details from the message sent returned by
7/// Mailjet when a request is successful
8#[derive(Debug, Serialize, Deserialize)]
9pub struct Sent {
10    #[serde(rename = "Email")]
11    pub email: String,
12    #[serde(rename = "MessageID")]
13    pub message_id: u64,
14    #[serde(rename = "MessageUUID")]
15    pub message_uuid: String,
16}
17
18/// Response from Mailjet when consuming the Send API
19#[derive(Debug, Serialize, Deserialize)]
20pub struct Response {
21    #[serde(rename = "Sent")]
22    pub sent: Vec<Sent>,
23}
24
25impl Response {
26    /// Creates an `Error` instance from the API response
27    pub async fn from_api_response(body: Body) -> Self {
28        let bytes = to_bytes(body).await.unwrap();
29        let response = String::from_utf8(bytes.to_vec()).expect("response was not valid utf-8");
30        let response: Response =
31            from_str(response.as_str()).expect("invalid response from mailjet api");
32
33        response
34    }
35}