updated price-info to edition 2018 (#10801)
This commit is contained in:
parent
5a561997cf
commit
16e0ad1288
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -3237,7 +3237,6 @@ dependencies = [
|
|||||||
"futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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)",
|
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"parity-runtime 0.1.0",
|
"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)",
|
"serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -5,14 +5,14 @@ license = "GPL-3.0"
|
|||||||
name = "price-info"
|
name = "price-info"
|
||||||
version = "1.12.0"
|
version = "1.12.0"
|
||||||
authors = ["Parity Technologies <admin@parity.io>"]
|
authors = ["Parity Technologies <admin@parity.io>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
fetch = { path = "../../util/fetch" }
|
fetch = { path = "../../util/fetch" }
|
||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
parity-runtime = { path = "../../util/runtime" }
|
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
parity-runtime = { path = "../../util/runtime" }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
parking_lot = "0.7"
|
|
||||||
fake-fetch = { path = "../../util/fake-fetch" }
|
fake-fetch = { path = "../../util/fake-fetch" }
|
||||||
|
@ -18,28 +18,14 @@
|
|||||||
|
|
||||||
//! A simple client to get the current ETH price using an external API.
|
//! A simple client to get the current ETH price using an external API.
|
||||||
|
|
||||||
extern crate futures;
|
use std::{cmp, fmt, io, str};
|
||||||
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 fetch::{Client as FetchClient, Fetch};
|
use fetch::{Client as FetchClient, Fetch};
|
||||||
use futures::{Future, Stream};
|
use futures::{Future, Stream};
|
||||||
use futures::future::{self, Either};
|
use log::warn;
|
||||||
use serde_json::Value;
|
|
||||||
use parity_runtime::Executor;
|
use parity_runtime::Executor;
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
pub use fetch;
|
||||||
|
|
||||||
/// Current ETH price information.
|
/// Current ETH price information.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -48,27 +34,6 @@ pub struct PriceInfo {
|
|||||||
pub ethusd: f32,
|
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.
|
/// A client to get the current ETH price using an external API.
|
||||||
pub struct Client<F = FetchClient> {
|
pub struct Client<F = FetchClient> {
|
||||||
pool: Executor,
|
pool: Executor,
|
||||||
@ -100,14 +65,7 @@ impl<F: Fetch> Client<F> {
|
|||||||
/// Gets the current ETH price and calls `set_price` with the result.
|
/// 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) {
|
pub fn get<G: FnOnce(PriceInfo) + Sync + Send + 'static>(&self, set_price: G) {
|
||||||
let future = self.fetch.get(&self.api_endpoint, fetch::Abort::default())
|
let future = self.fetch.get(&self.api_endpoint, fetch::Abort::default())
|
||||||
.from_err()
|
.and_then(|response| response.concat2())
|
||||||
.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(move |body| {
|
.and_then(move |body| {
|
||||||
let body_str = str::from_utf8(&body).ok();
|
let body_str = str::from_utf8(&body).ok();
|
||||||
let value: Option<Value> = body_str.and_then(|s| serde_json::from_str(s).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 });
|
set_price(PriceInfo { ethusd });
|
||||||
Ok(())
|
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| {
|
.map_err(|err| {
|
||||||
@ -135,11 +97,12 @@ impl<F: Fetch> Client<F> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use std::sync::Arc;
|
use std::sync::{
|
||||||
use parity_runtime::{Runtime, Executor};
|
Arc, atomic::{AtomicBool, Ordering}
|
||||||
use Client;
|
};
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
|
||||||
use fake_fetch::FakeFetch;
|
use fake_fetch::FakeFetch;
|
||||||
|
use parity_runtime::{Runtime, Executor};
|
||||||
|
use super::Client;
|
||||||
|
|
||||||
fn price_info_ok(response: &str, executor: Executor) -> Client<FakeFetch<String>> {
|
fn price_info_ok(response: &str, executor: Executor) -> Client<FakeFetch<String>> {
|
||||||
Client::new(FakeFetch::new(Some(response.to_owned())), executor)
|
Client::new(FakeFetch::new(Some(response.to_owned())), executor)
|
||||||
|
Loading…
Reference in New Issue
Block a user