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