Created
September 28, 2016 18:49
-
-
Save ngc0202/589f8c80ef7abddd287da60f834e1d5a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate serde; | |
extern crate serde_json; | |
extern crate hyper; | |
use std::fmt; | |
use std::error::Error; | |
#[derive(Debug)] | |
pub enum ShoutError { | |
Json(serde_json::Error), | |
Http(hyper::Error), | |
Io(::std::io::Error), | |
Error(&'static str), | |
} | |
impl Error for ShoutError { | |
fn description(&self) -> &str { | |
match *self { | |
ShoutError::Json(ref err) => err.description(), | |
ShoutError::Http(ref err) => err.description(), | |
ShoutError::Io(ref err) => err.description(), | |
ShoutError::Error(err) => err, | |
} | |
} | |
} | |
impl fmt::Display for ShoutError { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
match *self { | |
ShoutError::Json(ref err) => write!(f, "JSON Error: {}", err), | |
ShoutError::Http(ref err) => write!(f, "HTTP Error: {}", err), | |
ShoutError::Io(ref err) => write!(f, "IO Error: {}", err), | |
ShoutError::Error(err) => write!(f, "{}", err), | |
} | |
} | |
} | |
impl From<serde_json::Error> for ShoutError { | |
fn from(err: serde_json::Error) -> ShoutError { | |
ShoutError::Json(err) | |
} | |
} | |
impl From<hyper::Error> for ShoutError { | |
fn from(err: hyper::Error) -> ShoutError { | |
ShoutError::Http(err) | |
} | |
} | |
impl From<::std::io::Error> for ShoutError { | |
fn from(err: ::std::io::Error) -> ShoutError { | |
ShoutError::Io(err) | |
} | |
} | |
impl From<&'static str> for ShoutError { | |
fn from(err: &'static str) -> ShoutError { | |
ShoutError::Error(err) | |
} | |
} | |
pub type ShoutResult = Result<String, ShoutError>; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate serde; | |
extern crate serde_json; | |
extern crate hyper; | |
mod error; | |
pub use error::{ShoutError, ShoutResult}; | |
use std::io::Read; | |
use serde_json::{Map, Value}; | |
use hyper::client::Client; | |
use hyper::header::{Headers, ContentType}; | |
use hyper::mime::{Mime, TopLevel, SubLevel}; | |
pub fn upcase(thing_to_yell: &str) -> ShoutResult { | |
let mut response = String::new(); | |
let mut headers = Headers::new(); | |
headers.set( | |
ContentType(Mime(TopLevel::Application, SubLevel::Json, vec![])) | |
); | |
let mut map = Map::new(); | |
map.insert("INPUT".to_owned(), thing_to_yell); | |
let request = try!(serde_json::to_string(&map).map_err(ShoutError::Json)); | |
let client = Client::new(); | |
let _ = try!( | |
try!(client.post("HTTP://API.SHOUTCLOUD.IO/V1/SHOUT") | |
.headers(headers) | |
.body(&request) | |
.send() | |
.map_err(ShoutError::Http)) | |
.read_to_string(&mut response) | |
); | |
let response: Value = try!(serde_json::from_str(&response)); | |
let response = try!( | |
response | |
.find("OUTPUT") | |
.ok_or("Got invalid response.")) | |
.as_str() | |
.unwrap() | |
.to_string(); | |
Ok(response) | |
} | |
#[test] | |
fn it_works() { | |
let up = upcase("hello, world").unwrap(); | |
assert_eq!(up, "HELLO, WORLD"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment