pub static MAX_BLOCK_SIZE: u32 = 1024*1024; //Please do not exceed this value, or the webserver will deny your request.
pub static ENABLE_DEBUG: bool = false; //Enable temporarily if you're encountering problems with fetching the data
pub static MAX_RETRY_COUNT: u8 = 10;
fn get_raw_data_async(remote: Remote, length: usize, mut retry_count: usize) -> impl Future<Item=String, Error=reqwest::Error>{
futures::lazy(move || {
if length as u32 > MAX_BLOCK_SIZE {
return Ok(None)
}
let (length, size) = {
if length <= 1024 {
(1, length)
} else {
let val = (length as f64)/ (1024 as f64);
let len = math::round::ceil(val, 0) as usize;
println!("We want {}, and are using {} x 1024 to get it", length, len);
(len, 1024)
}
};
let mut url = format!("https://qrng.anu.edu.au/API/jsonI.php?length={}&type=hex16&size={}", length, size);
remote.clone().execute(reqwest::get(url.as_str()).and_then(|mut resp| {Ok(Some(resp.text().unwrap()))}).or_else(|err| {
//DC?
retry_count += 1;
if retry_count > MAX_RETRY_COUNT as usize {
return Ok(None)
}
//try again
get_raw_data_async(remote.clone(), length, retry_count)
}));
})
}
Consider the code above. If you input an arbitrary remote, some length, and use "0" for retry-count, the program is suppose to return the string which is downloaded using reqwest.
I want it to download information, then once the information is done downloading, return it to the calling function outside of this function's closure. If, in the case the info can't download, I want it to try again (up to a certain number of times, determined by MAX_RETRY_COUNT).
The idea is that this entire function is called and stored into a futures-array. Then the futures are all joined() to eventually concatenate the outputs of however many times this function was called.
Obviously, there will be a compiler error. It keeps telling me that the function in the execute() closure is not valid. To those who are experiences, you'll know why of course. So, knowledgeable one, I ask you: how might I craft this function successfully?