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, "application/json")
.body(body)
.send()?;
serde_json::from_str::<Response>(&response.text()?).map_err(From::from)
}
}