2016-03-28 00:49:35 +02:00
|
|
|
use rustc_serialize::json::Json;
|
|
|
|
use std::io::Read;
|
|
|
|
use hyper::Client;
|
|
|
|
use hyper::header::Connection;
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
pub struct PriceInfo {
|
|
|
|
pub ethusd: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PriceInfo {
|
|
|
|
pub fn get() -> Option<PriceInfo> {
|
|
|
|
let mut body = String::new();
|
2016-03-28 21:58:59 +02:00
|
|
|
// TODO: Handle each error type properly
|
|
|
|
Client::new()
|
2016-03-28 00:49:35 +02:00
|
|
|
.get("http://api.etherscan.io/api?module=stats&action=ethprice")
|
|
|
|
.header(Connection::close())
|
2016-03-28 21:58:59 +02:00
|
|
|
.send().ok()
|
|
|
|
.and_then(|mut s| s.read_to_string(&mut body).ok())
|
|
|
|
.and_then(|_| Json::from_str(&body).ok())
|
|
|
|
.and_then(|json| json.find_path(&["result", "ethusd"])
|
2016-04-06 10:07:24 +02:00
|
|
|
.and_then(|obj| match *obj {
|
|
|
|
Json::String(ref s) => Some(PriceInfo {
|
2016-03-28 21:58:59 +02:00
|
|
|
ethusd: FromStr::from_str(&s).unwrap()
|
|
|
|
}),
|
|
|
|
_ => None
|
|
|
|
}))
|
2016-03-28 00:49:35 +02:00
|
|
|
}
|
|
|
|
}
|