DRY up HTTP request builder in source database (#2902)

This commit is contained in:
Charlie Marsh 2024-04-08 10:45:26 -04:00 committed by GitHub
parent f11a5e2208
commit 10dfd43af9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 27 deletions

View file

@ -358,6 +358,7 @@ impl<'a, Context: BuildContext + Send + Sync> DistributionDatabase<'a, Context>
.instrument(info_span!("wheel", wheel = %dist))
};
let req = self.request(url.clone())?;
let cache_control = match self.client.connectivity() {
Connectivity::Online => CacheControl::from(
self.build_context
@ -367,11 +368,10 @@ impl<'a, Context: BuildContext + Send + Sync> DistributionDatabase<'a, Context>
),
Connectivity::Offline => CacheControl::AllowStale,
};
let archive = self
.client
.cached_client()
.get_serde(self.request(url)?, &http_entry, cache_control, download)
.get_serde(req, &http_entry, cache_control, download)
.await
.map_err(|err| match err {
CachedClientError::Callback(err) => err,
@ -428,6 +428,7 @@ impl<'a, Context: BuildContext + Send + Sync> DistributionDatabase<'a, Context>
.instrument(info_span!("wheel", wheel = %dist))
};
let req = self.request(url.clone())?;
let cache_control = match self.client.connectivity() {
Connectivity::Online => CacheControl::from(
self.build_context
@ -441,7 +442,7 @@ impl<'a, Context: BuildContext + Send + Sync> DistributionDatabase<'a, Context>
let archive = self
.client
.cached_client()
.get_serde(self.request(url)?, &http_entry, cache_control, download)
.get_serde(req, &http_entry, cache_control, download)
.await
.map_err(|err| match err {
CachedClientError::Callback(err) => err,

View file

@ -356,18 +356,7 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
.boxed()
.instrument(info_span!("download", source_dist = %source))
};
let req = self
.client
.uncached_client()
.get(url.clone())
.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 req = self.request(url.clone())?;
let manifest = self
.client
.cached_client()
@ -460,18 +449,7 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
.boxed()
.instrument(info_span!("download", source_dist = %source))
};
let req = self
.client
.uncached_client()
.get(url.clone())
.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 req = self.request(url.clone())?;
let manifest = self
.client
.cached_client()
@ -1103,6 +1081,21 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
debug!("Finished building (editable): {dist}");
Ok((dist, disk_filename, filename, metadata))
}
/// 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()
}
}
#[derive(Debug)]