updated price-info to edition 2018 (#10801)

This commit is contained in:
Marek Kotewicz 2019-06-27 17:29:24 +08:00 committed by GitHub
parent 5a561997cf
commit 16e0ad1288
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 56 deletions

1
Cargo.lock generated
View File

@ -3237,7 +3237,6 @@ dependencies = [
"futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-runtime 0.1.0",
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
]

View File

@ -5,14 +5,14 @@ license = "GPL-3.0"
name = "price-info"
version = "1.12.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
fetch = { path = "../../util/fetch" }
futures = "0.1"
parity-runtime = { path = "../../util/runtime" }
log = "0.4"
parity-runtime = { path = "../../util/runtime" }
serde_json = "1.0"
[dev-dependencies]
parking_lot = "0.7"
fake-fetch = { path = "../../util/fake-fetch" }

View File

@ -18,28 +18,14 @@
//! A simple client to get the current ETH price using an external API.
extern crate futures;
extern crate serde_json;
extern crate parity_runtime;
#[macro_use]
extern crate log;
#[cfg(test)]
extern crate fake_fetch;
pub extern crate fetch;
use std::cmp;
use std::fmt;
use std::io;
use std::str;
use std::{cmp, fmt, io, str};
use fetch::{Client as FetchClient, Fetch};
use futures::{Future, Stream};
use futures::future::{self, Either};
use serde_json::Value;
use log::warn;
use parity_runtime::Executor;
use serde_json::Value;
pub use fetch;
/// Current ETH price information.
#[derive(Debug)]
@ -48,27 +34,6 @@ pub struct PriceInfo {
pub ethusd: f32,
}
/// Price info error.
#[derive(Debug)]
pub enum Error {
/// The API returned an unexpected status code.
StatusCode(&'static str),
/// The API returned an unexpected status content.
UnexpectedResponse(Option<String>),
/// There was an error when trying to reach the API.
Fetch(fetch::Error),
/// IO error when reading API response.
Io(io::Error),
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self { Error::Io(err) }
}
impl From<fetch::Error> for Error {
fn from(err: fetch::Error) -> Self { Error::Fetch(err) }
}
/// A client to get the current ETH price using an external API.
pub struct Client<F = FetchClient> {
pool: Executor,
@ -100,14 +65,7 @@ impl<F: Fetch> Client<F> {
/// Gets the current ETH price and calls `set_price` with the result.
pub fn get<G: FnOnce(PriceInfo) + Sync + Send + 'static>(&self, set_price: G) {
let future = self.fetch.get(&self.api_endpoint, fetch::Abort::default())
.from_err()
.and_then(|response| {
if !response.is_success() {
let s = Error::StatusCode(response.status().canonical_reason().unwrap_or("unknown"));
return Either::A(future::err(s));
}
Either::B(response.concat2().from_err())
})
.and_then(|response| response.concat2())
.and_then(move |body| {
let body_str = str::from_utf8(&body).ok();
let value: Option<Value> = body_str.and_then(|s| serde_json::from_str(s).ok());
@ -123,7 +81,11 @@ impl<F: Fetch> Client<F> {
set_price(PriceInfo { ethusd });
Ok(())
},
None => Err(Error::UnexpectedResponse(body_str.map(From::from))),
None => {
let msg = format!("Unexpected response: {}", body_str.unwrap_or_default());
let err = io::Error::new(io::ErrorKind::Other, msg);
Err(fetch::Error::Io(err))
}
}
})
.map_err(|err| {
@ -135,11 +97,12 @@ impl<F: Fetch> Client<F> {
#[cfg(test)]
mod test {
use std::sync::Arc;
use parity_runtime::{Runtime, Executor};
use Client;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{
Arc, atomic::{AtomicBool, Ordering}
};
use fake_fetch::FakeFetch;
use parity_runtime::{Runtime, Executor};
use super::Client;
fn price_info_ok(response: &str, executor: Executor) -> Client<FakeFetch<String>> {
Client::new(FakeFetch::new(Some(response.to_owned())), executor)