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
use crate::errors::*;

static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));

pub struct Client {
    http: reqwest::Client,
}

impl Client {
    pub fn new() -> Result<Self> {
        let http = reqwest::Client::builder()
            .user_agent(APP_USER_AGENT)
            .build()?;
        Ok(Client { http })
    }

    pub async fn fetch(&self, url: &str) -> Result<bytes::Bytes> {
        info!("Downloading {url:?}...");
        let response = self
            .http
            .get(url)
            .send()
            .await
            .context("Failed to send http request")?
            .error_for_status()
            .context("Received http error")?;
        let buf = response.bytes().await.context("Failed to read http body")?;
        Ok(buf)
    }
}