2016-12-11 19:30:54 +01:00
|
|
|
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
|
2016-07-08 17:26:06 +02:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
use rustc_serialize::json::Json;
|
2016-07-08 20:01:20 +02:00
|
|
|
use std::thread;
|
2016-07-08 17:26:06 +02:00
|
|
|
use std::io::Read;
|
|
|
|
use std::time::Duration;
|
2016-07-08 20:01:20 +02:00
|
|
|
use std::str::FromStr;
|
|
|
|
use std::sync::mpsc;
|
2016-07-08 17:26:06 +02:00
|
|
|
use hyper::client::{Handler, Request, Response, Client};
|
|
|
|
use hyper::{Next, Encoder, Decoder};
|
|
|
|
use hyper::net::HttpStream;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct PriceInfo {
|
|
|
|
pub ethusd: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SetPriceHandler<F: Fn(PriceInfo) + Sync + Send + 'static> {
|
|
|
|
set_price: F,
|
2016-07-08 20:01:20 +02:00
|
|
|
channel: mpsc::Sender<()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: Fn(PriceInfo) + Sync + Send + 'static> Drop for SetPriceHandler<F> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let _ = self.channel.send(());
|
|
|
|
}
|
2016-07-08 17:26:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: Fn(PriceInfo) + Sync + Send + 'static> Handler<HttpStream> for SetPriceHandler<F> {
|
2016-07-09 16:59:15 +02:00
|
|
|
fn on_request(&mut self, _: &mut Request) -> Next { Next::read().timeout(Duration::from_secs(3)) }
|
|
|
|
fn on_request_writable(&mut self, _: &mut Encoder<HttpStream>) -> Next { Next::read().timeout(Duration::from_secs(3)) }
|
|
|
|
fn on_response(&mut self, _: Response) -> Next { Next::read().timeout(Duration::from_secs(3)) }
|
|
|
|
|
2016-07-08 17:26:06 +02:00
|
|
|
fn on_response_readable(&mut self, r: &mut Decoder<HttpStream>) -> Next {
|
|
|
|
let mut body = String::new();
|
|
|
|
let _ = r.read_to_string(&mut body).ok()
|
|
|
|
.and_then(|_| Json::from_str(&body).ok())
|
|
|
|
.and_then(|json| json.find_path(&["result", "ethusd"])
|
|
|
|
.and_then(|obj| match *obj {
|
|
|
|
Json::String(ref s) => Some((self.set_price)(PriceInfo {
|
2016-10-20 23:41:15 +02:00
|
|
|
ethusd: FromStr::from_str(s)
|
|
|
|
.expect("Etherscan API will always return properly formatted price; qed")
|
2016-07-08 17:26:06 +02:00
|
|
|
})),
|
|
|
|
_ => None,
|
|
|
|
}));
|
|
|
|
Next::end()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PriceInfo {
|
|
|
|
pub fn get<F: Fn(PriceInfo) + Sync + Send + 'static>(set_price: F) -> Result<(), ()> {
|
|
|
|
// TODO: Handle each error type properly
|
2016-12-27 12:53:56 +01:00
|
|
|
let client = Client::new().map_err(|_| ())?;
|
2016-07-08 20:01:20 +02:00
|
|
|
thread::spawn(move || {
|
|
|
|
let (tx, rx) = mpsc::channel();
|
2016-10-20 23:41:15 +02:00
|
|
|
let url = FromStr::from_str("http://api.etherscan.io/api?module=stats&action=ethprice")
|
|
|
|
.expect("string known to be a valid URL; qed");
|
|
|
|
let _ = client.request(
|
|
|
|
url,
|
|
|
|
SetPriceHandler {
|
|
|
|
set_price: set_price,
|
|
|
|
channel: tx,
|
|
|
|
}).ok().and_then(|_| rx.recv().ok());
|
2016-07-08 20:01:20 +02:00
|
|
|
client.close();
|
|
|
|
});
|
|
|
|
Ok(())
|
2016-07-08 17:26:06 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-08 20:01:20 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_get_price_info() {
|
2016-07-13 19:59:59 +02:00
|
|
|
use std::sync::Arc;
|
2016-07-08 20:01:20 +02:00
|
|
|
use std::time::Duration;
|
|
|
|
use util::log::init_log;
|
2016-07-13 19:59:59 +02:00
|
|
|
use util::{Condvar, Mutex};
|
|
|
|
|
2016-07-08 20:01:20 +02:00
|
|
|
init_log();
|
|
|
|
let done = Arc::new((Mutex::new(PriceInfo { ethusd: 0f32 }), Condvar::new()));
|
|
|
|
let rdone = done.clone();
|
2016-07-14 12:16:53 +02:00
|
|
|
|
2016-07-13 19:59:59 +02:00
|
|
|
PriceInfo::get(move |price| { let mut p = rdone.0.lock(); *p = price; rdone.1.notify_one(); }).unwrap();
|
|
|
|
let mut p = done.0.lock();
|
|
|
|
let t = done.1.wait_for(&mut p, Duration::from_millis(10000));
|
|
|
|
assert!(!t.timed_out());
|
|
|
|
assert!(p.ethusd != 0f32);
|
2016-07-14 12:16:53 +02:00
|
|
|
}
|