Introduce retry for both ygo/mtg searchers.
This commit is contained in:
parent
3b592365a7
commit
04195ca697
@ -16,3 +16,4 @@ select = "0.3.0"
|
||||
#reqwest = "0.4.0"
|
||||
hyper = "0.9.18"
|
||||
serde_json = "0.9"
|
||||
retry = { git = "https://github.com/jimmycuadra/retry", rev = "3fa812e650d64ede61ea243fb83ef1a222ff0f84" }
|
||||
|
@ -1,6 +1,7 @@
|
||||
extern crate hyper;
|
||||
extern crate select;
|
||||
extern crate serde_json;
|
||||
extern crate retry;
|
||||
|
||||
pub mod searchers;
|
||||
|
||||
|
@ -7,11 +7,18 @@ use hyper::Client;
|
||||
use serde_json;
|
||||
use serde_json::Value;
|
||||
|
||||
use retry;
|
||||
use retry::retry;
|
||||
use retry::delay::Fixed;
|
||||
|
||||
use std;
|
||||
use std::io::Read;
|
||||
|
||||
use std::any::Any;
|
||||
|
||||
const NUM_RETRIES: usize = 10;
|
||||
const RETRY_WAIT_MILLIS: u64 = 500;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MtgCard {
|
||||
name: String,
|
||||
@ -53,7 +60,10 @@ impl MtgSearcher {
|
||||
fn do_search (&self, name: &str) -> Result<String, Error> {
|
||||
let mut contents = String::new();
|
||||
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)
|
||||
}
|
||||
}
|
||||
@ -114,3 +124,13 @@ impl From<serde_json::Error> for 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,11 +7,18 @@ use hyper::Client;
|
||||
use serde_json;
|
||||
use serde_json::Value;
|
||||
|
||||
use retry;
|
||||
use retry::retry;
|
||||
use retry::delay::Fixed;
|
||||
|
||||
use std;
|
||||
use std::io::Read;
|
||||
|
||||
use std::any::Any;
|
||||
|
||||
const NUM_RETRIES: usize = 10;
|
||||
const RETRY_WAIT_MILLIS: u64 = 500;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct YugiohCard {
|
||||
name: String,
|
||||
@ -54,7 +61,10 @@ impl YugiohSearcher {
|
||||
fn do_search (&self, name: &str) -> Result<String, Error> {
|
||||
let mut contents = String::new();
|
||||
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)
|
||||
}
|
||||
}
|
||||
@ -114,3 +124,13 @@ impl From<serde_json::Error> for 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user