openethereum/parity/price_info.rs

30 lines
749 B
Rust
Raw Normal View History

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();
// 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())
.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 {
ethusd: FromStr::from_str(&s).unwrap()
}),
_ => None
}))
2016-03-28 00:49:35 +02:00
}
}