pdbget/
request.rs

1use reqwest;
2use std::io;
3
4/// Transform reqwest::Error to std::io::Error
5pub fn handled_reqwest_errors(uri: &str, e: reqwest::Error) -> io::Error {
6    io::Error::new(
7        io::ErrorKind::ConnectionAborted,
8        format!("[Warning] when connecting to {}\n{}", uri, e.to_string()),
9    )
10}
11
12/// Function to performed a GET request on the url and write the return
13/// in the writer buffer
14pub fn get(uri: &str, writer: &mut Vec<u8>) -> Result<(), io::Error> {
15    let mut result = match reqwest::Client::new().get(uri).send() {
16        Ok(res) => res,
17        Err(e) => return Err(handled_reqwest_errors(uri, e)),
18    };
19    if result.status() != 200 {
20        return Err(io::Error::new(
21            io::ErrorKind::ConnectionAborted,
22            format!(
23                "[Warning] when connecting to {}\nResponse Statut : {}",
24                uri,
25                result.status()
26            ),
27        ));
28    }
29    match result.copy_to(writer) {
30        Ok(_) => Ok(()),
31        Err(e) => Err(handled_reqwest_errors(uri, e)),
32    }
33}