Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

updated price-info to edition 2018 #10801

Merged
merged 1 commit into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions miner/price-info/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
69 changes: 16 additions & 53 deletions miner/price-info/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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,
Expand Down Expand Up @@ -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());
Expand All @@ -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| {
Expand All @@ -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)
Expand Down