Skip to main content

repro_env/
http.rs

1use crate::errors::*;
2use std::time::Duration;
3
4static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
5pub const CONNECT_TIMEOUT: Duration = Duration::from_secs(15);
6pub const READ_TIMEOUT: Duration = Duration::from_secs(240);
7
8pub struct Client {
9    http: reqwest::Client,
10}
11
12impl Client {
13    pub fn new() -> Result<Self> {
14        let http = reqwest::Client::builder()
15            .user_agent(APP_USER_AGENT)
16            .connect_timeout(CONNECT_TIMEOUT)
17            .read_timeout(READ_TIMEOUT)
18            .build()?;
19        Ok(Client { http })
20    }
21
22    pub async fn request(&self, url: &str) -> Result<reqwest::Response> {
23        info!("Downloading {url:?}...");
24        let response = self
25            .http
26            .get(url)
27            .send()
28            .await
29            .context("Failed to send http request")?
30            .error_for_status()
31            .context("Received http error")?;
32        Ok(response)
33    }
34
35    pub async fn fetch(&self, url: &str) -> Result<bytes::Bytes> {
36        let response = self.request(url).await?;
37        let buf = response.bytes().await.context("Failed to read http body")?;
38        Ok(buf)
39    }
40}