2016-04-21 15:41:25 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
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-05-17 10:32:05 +02:00
|
|
|
ethusd: FromStr::from_str(s).unwrap()
|
2016-03-28 21:58:59 +02:00
|
|
|
}),
|
|
|
|
_ => None
|
|
|
|
}))
|
2016-03-28 00:49:35 +02:00
|
|
|
}
|
|
|
|
}
|