DRY up request builer in wheel download (#2857)

This commit is contained in:
Charlie Marsh 2024-04-06 22:12:03 -04:00 committed by GitHub
parent a3de0e8bc6
commit d4a258422b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -413,18 +413,6 @@ impl<'a, Context: BuildContext + Send + Sync> DistributionDatabase<'a, Context>
.instrument(info_span!("wheel", wheel = %dist))
};
let req = self
.client
.uncached_client()
.get(url)
.header(
// `reqwest` defaults to accepting compressed responses.
// Specify identity encoding to get consistent .whl downloading
// behavior from servers. ref: https://github.com/pypa/pip/pull/1688
"accept-encoding",
reqwest::header::HeaderValue::from_static("identity"),
)
.build()?;
let cache_control = match self.client.connectivity() {
Connectivity::Online => CacheControl::from(
self.build_context
@ -438,7 +426,7 @@ impl<'a, Context: BuildContext + Send + Sync> DistributionDatabase<'a, Context>
let archive = self
.client
.cached_client()
.get_serde(req, &http_entry, cache_control, download)
.get_serde(self.request(url)?, &http_entry, cache_control, download)
.await
.map_err(|err| match err {
CachedClientError::Callback(err) => err,
@ -495,18 +483,6 @@ impl<'a, Context: BuildContext + Send + Sync> DistributionDatabase<'a, Context>
.instrument(info_span!("wheel", wheel = %dist))
};
let req = self
.client
.uncached_client()
.get(url)
.header(
// `reqwest` defaults to accepting compressed responses.
// Specify identity encoding to get consistent .whl downloading
// behavior from servers. ref: https://github.com/pypa/pip/pull/1688
"accept-encoding",
reqwest::header::HeaderValue::from_static("identity"),
)
.build()?;
let cache_control = match self.client.connectivity() {
Connectivity::Online => CacheControl::from(
self.build_context
@ -520,7 +496,7 @@ impl<'a, Context: BuildContext + Send + Sync> DistributionDatabase<'a, Context>
let archive = self
.client
.cached_client()
.get_serde(req, &http_entry, cache_control, download)
.get_serde(self.request(url)?, &http_entry, cache_control, download)
.await
.map_err(|err| match err {
CachedClientError::Callback(err) => err,
@ -530,6 +506,21 @@ impl<'a, Context: BuildContext + Send + Sync> DistributionDatabase<'a, Context>
Ok(archive)
}
/// Returns a GET [`reqwest::Request`] for the given URL.
fn request(&self, url: Url) -> Result<reqwest::Request, reqwest::Error> {
self.client
.uncached_client()
.get(url)
.header(
// `reqwest` defaults to accepting compressed responses.
// Specify identity encoding to get consistent .whl downloading
// behavior from servers. ref: https://github.com/pypa/pip/pull/1688
"accept-encoding",
reqwest::header::HeaderValue::from_static("identity"),
)
.build()
}
/// Return the [`IndexLocations`] used by this resolver.
pub fn index_locations(&self) -> &IndexLocations {
self.build_context.index_locations()