a bit more idiomatic price-info

This commit is contained in:
debris 2017-08-04 13:39:57 +02:00
parent 63f8cc3503
commit f72196f1bb
2 changed files with 21 additions and 21 deletions

View File

@ -30,7 +30,6 @@ use std::cmp;
use std::fmt; use std::fmt;
use std::io; use std::io;
use std::io::Read; use std::io::Read;
use std::str::FromStr;
use fetch::{Client as FetchClient, Fetch}; use fetch::{Client as FetchClient, Fetch};
use futures::Future; use futures::Future;
@ -46,8 +45,10 @@ pub struct PriceInfo {
/// Price info error. /// Price info error.
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
/// The API returned an unexpected status code or content. /// The API returned an unexpected status code.
UnexpectedResponse(&'static str, String), StatusCode(&'static str),
/// The API returned an unexpected status content.
UnexpectedResponse(String),
/// There was an error when trying to reach the API. /// There was an error when trying to reach the API.
Fetch(fetch::Error), Fetch(fetch::Error),
/// IO error when reading API response. /// IO error when reading API response.
@ -94,28 +95,27 @@ impl<F: Fetch> Client<F> {
self.fetch.forget(self.fetch.fetch(&self.api_endpoint) self.fetch.forget(self.fetch.fetch(&self.api_endpoint)
.map_err(|err| Error::Fetch(err)) .map_err(|err| Error::Fetch(err))
.and_then(move |mut response| { .and_then(move |mut response| {
if !response.is_success() {
return Err(Error::StatusCode(response.status().canonical_reason().unwrap_or("unknown")));
}
let mut result = String::new(); let mut result = String::new();
response.read_to_string(&mut result)?; response.read_to_string(&mut result)?;
if response.is_success() { let value: Option<Value> = serde_json::from_str(&result).ok();
let value: Result<Value, _> = serde_json::from_str(&result);
if let Ok(v) = value {
let obj = v.pointer("/result/ethusd").and_then(|obj| {
match *obj {
Value::String(ref s) => FromStr::from_str(s).ok(),
_ => None,
}
});
if let Some(ethusd) = obj { let ethusd = value
.as_ref()
.and_then(|value| value.pointer("/result/ethusd"))
.and_then(|obj| obj.as_str())
.and_then(|s| s.parse().ok());
match ethusd {
Some(ethusd) => {
set_price(PriceInfo { ethusd }); set_price(PriceInfo { ethusd });
return Ok(()); Ok(())
},
None => Err(Error::UnexpectedResponse(result)),
} }
}
}
let status = response.status().canonical_reason().unwrap_or("unknown");
Err(Error::UnexpectedResponse(status, result))
}) })
.map_err(|err| { .map_err(|err| {
warn!("Failed to auto-update latest ETH price: {:?}", err); warn!("Failed to auto-update latest ETH price: {:?}", err);

View File

@ -297,7 +297,7 @@ impl Response {
/// Returns `true` if response status code is successful. /// Returns `true` if response status code is successful.
pub fn is_success(&self) -> bool { pub fn is_success(&self) -> bool {
self.status() == reqwest::StatusCode::Ok self.status().is_success()
} }
/// Returns `true` if content type of this response is `text/html` /// Returns `true` if content type of this response is `text/html`