Introduce retry for both ygo/mtg searchers.

This commit is contained in:
Adrian Malacoda 2017-03-12 16:27:59 -05:00
parent 3b592365a7
commit 04195ca697
4 changed files with 44 additions and 2 deletions

View File

@ -16,3 +16,4 @@ select = "0.3.0"
#reqwest = "0.4.0" #reqwest = "0.4.0"
hyper = "0.9.18" hyper = "0.9.18"
serde_json = "0.9" serde_json = "0.9"
retry = { git = "https://github.com/jimmycuadra/retry", rev = "3fa812e650d64ede61ea243fb83ef1a222ff0f84" }

View File

@ -1,6 +1,7 @@
extern crate hyper; extern crate hyper;
extern crate select; extern crate select;
extern crate serde_json; extern crate serde_json;
extern crate retry;
pub mod searchers; pub mod searchers;

View File

@ -7,11 +7,18 @@ use hyper::Client;
use serde_json; use serde_json;
use serde_json::Value; use serde_json::Value;
use retry;
use retry::retry;
use retry::delay::Fixed;
use std; use std;
use std::io::Read; use std::io::Read;
use std::any::Any; use std::any::Any;
const NUM_RETRIES: usize = 10;
const RETRY_WAIT_MILLIS: u64 = 500;
#[derive(Debug)] #[derive(Debug)]
pub struct MtgCard { pub struct MtgCard {
name: String, name: String,
@ -53,7 +60,10 @@ impl MtgSearcher {
fn do_search (&self, name: &str) -> Result<String, Error> { fn do_search (&self, name: &str) -> Result<String, Error> {
let mut contents = String::new(); let mut contents = String::new();
let api_url = &format!("https://api.magicthegathering.io/v1/cards?name={}", name); let api_url = &format!("https://api.magicthegathering.io/v1/cards?name={}", name);
self.client.get(api_url).send()?.read_to_string(&mut contents)?; let mut response = retry(Fixed::from_millis(RETRY_WAIT_MILLIS).take(NUM_RETRIES), || {
self.client.get(api_url).send()
})?;
response.read_to_string(&mut contents)?;
Result::Ok(contents) Result::Ok(contents)
} }
} }
@ -114,3 +124,13 @@ impl From<serde_json::Error> for Error {
Error::Json(error) Error::Json(error)
} }
} }
impl From<retry::Error<hyper::Error>> for Error {
fn from (err: retry::Error<hyper::Error>) -> Error {
match err {
retry::Error::Operation { error, total_delay, tries } => {
Error::Http(error)
}
}
}
}

View File

@ -7,11 +7,18 @@ use hyper::Client;
use serde_json; use serde_json;
use serde_json::Value; use serde_json::Value;
use retry;
use retry::retry;
use retry::delay::Fixed;
use std; use std;
use std::io::Read; use std::io::Read;
use std::any::Any; use std::any::Any;
const NUM_RETRIES: usize = 10;
const RETRY_WAIT_MILLIS: u64 = 500;
#[derive(Debug)] #[derive(Debug)]
pub struct YugiohCard { pub struct YugiohCard {
name: String, name: String,
@ -54,7 +61,10 @@ impl YugiohSearcher {
fn do_search (&self, name: &str) -> Result<String, Error> { fn do_search (&self, name: &str) -> Result<String, Error> {
let mut contents = String::new(); let mut contents = String::new();
let api_url = &format!("http://yugiohprices.com/api/card_data/{}", name); let api_url = &format!("http://yugiohprices.com/api/card_data/{}", name);
self.client.get(api_url).send()?.read_to_string(&mut contents)?; let mut response = retry(Fixed::from_millis(RETRY_WAIT_MILLIS).take(NUM_RETRIES), || {
self.client.get(api_url).send()
})?;
response.read_to_string(&mut contents)?;
Result::Ok(contents) Result::Ok(contents)
} }
} }
@ -114,3 +124,13 @@ impl From<serde_json::Error> for Error {
Error::Json(error) Error::Json(error)
} }
} }
impl From<retry::Error<hyper::Error>> for Error {
fn from (err: retry::Error<hyper::Error>) -> Error {
match err {
retry::Error::Operation { error, total_delay, tries } => {
Error::Http(error)
}
}
}
}