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
use reqwest::{header::CONTENT_TYPE, Client};
use serde_json;
use error::Result;
use message::Message;
use response::Response;
static API_URL: &'static str = "https://inject.socketlabs.com/api/v1/email";
#[derive(Debug, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct Request<'a> {
server_id: u16,
api_key: String,
messages: Vec<Message<'a>>,
}
impl<'a> Request<'a> {
pub fn new(server_id: u16, api_key: String, messages: Vec<Message<'a>>) -> Result<Request> {
Ok(Request {
server_id: server_id,
api_key: api_key,
messages: messages,
})
}
pub fn send(&self) -> Result<Response> {
let body = serde_json::to_string(&self)?;
let client = Client::new();
let mut response = client
.post(API_URL)
.header(CONTENT_TYPE, "json")
.body(body)
.send()?;
serde_json::from_str::<Response>(&response.text()?).map_err(From::from)
}
}