1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! HTTP client and networking.
use async_trait::async_trait;
use reqwest::{
    self,
    header::{IF_MODIFIED_SINCE, LAST_MODIFIED},
    IntoUrl, StatusCode
};
use std::ops::Deref;

/// Implementation of the default HTTP client.  
/// A wrapper for [`reqwest`]
pub struct Client<T>(T);

#[allow(dead_code)]
impl<T> Client<T> {
    fn new(x: T) -> Client<T> {
        Client(x)
    }
}

impl<T> Deref for Client<T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.0
    }
}

/// Implementation of `HttpClient` for `reqwest`.
#[async_trait]
impl HttpClient for Client<reqwest::Client> {
    async fn get<U: IntoUrl + Send>(
        &self, url: U, last_modified: Option<&str>
    ) -> Result<(String, StatusCode, Vec<u8>), reqwest::Error> {
        // let url: &str = &url.into();
        match if let Some(lm) = last_modified {
            if lm.is_empty() {
                self.0.get(url)
            } else {
                self.0.get(url).header(IF_MODIFIED_SINCE, lm)
            }
        } else {
            self.0.get(url)
        }
        .send()
        .await
        {
            Ok(res) => {
                let lm = res
                    .headers()
                    .get(LAST_MODIFIED)
                    .map(|r| r.to_str().ok())
                    .flatten()
                    .unwrap_or("");

                Ok((lm.into(), res.status(), res.bytes().await.map(|b| b.to_vec())?))
            }
            Err(e) => Err(e)
        }
    }
}

#[async_trait]
pub trait HttpClient: Sync + Send {
    async fn get<U: IntoUrl + Send>(
        &self, url: U, last_modified: Option<&str>
    ) -> Result<(String, StatusCode, Vec<u8>), reqwest::Error>;
}

/// Implementation of `HttpClient` for `reqwest`.
#[async_trait]
impl HttpClient for reqwest::Client {
    async fn get<U: IntoUrl + Send>(
        &self, url: U, last_modified: Option<&str>
    ) -> Result<(String, StatusCode, Vec<u8>), reqwest::Error> {
        // let url: &str = &url.into();
        let res = {
            if let Some(lm) = last_modified {
                if lm.is_empty() {
                    self.get(url)
                } else {
                    self.get(url).header(IF_MODIFIED_SINCE, lm)
                }
            } else {
                self.get(url)
            }
            .send()
            .await
        }?;

        let lm = res.headers().get(LAST_MODIFIED).map(|r| r.to_str().ok()).flatten().unwrap_or("");

        Ok((lm.into(), res.status(), res.bytes().await.map(|b| b.to_vec())?))
    }
}