tame_index/index/local/
builder.rs

1//! Adds functionality for retrieving crate files from a remote registry
2
3use crate::Error;
4
5/// Wrapper around a [`reqwest::blocking::Client`] to condition it correctly
6/// for making requests to a remote registry
7#[derive(Clone)]
8pub struct Client {
9    inner: reqwest::blocking::Client,
10}
11
12impl Client {
13    /// Creates a client from the specified builder
14    pub fn build(builder: reqwest::blocking::ClientBuilder) -> Result<Self, Error> {
15        // Crates are _usually_ just stored as content-type: application/gzip
16        // without a content-encoding, but _just in case_ we disable gzip so that
17        // they aren't automatically decompressed by reqwest, screwing up the
18        // checksum computation
19        let inner = builder.no_gzip().build()?;
20        Ok(Self { inner })
21    }
22}
23
24impl<'iv> super::ValidKrate<'iv> {
25    /// Downloads and validates a .crate from the specified index
26    pub fn download(
27        client: &Client,
28        config: &crate::index::IndexConfig,
29        version: &'iv crate::IndexVersion,
30    ) -> Result<Self, Error> {
31        let url = config.download_url(version.name.as_str().try_into()?, version.version.as_ref());
32
33        let res = client.inner.get(url).send()?.error_for_status()?;
34        let body = res.bytes()?;
35        Self::validate(body, version)
36    }
37}