From d4a258422bd13abbb647e7e443036d0d3193f0cc Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 6 Apr 2024 22:12:03 -0400 Subject: [PATCH] DRY up request builer in wheel download (#2857) --- .../src/distribution_database.rs | 43 ++++++++----------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/crates/uv-distribution/src/distribution_database.rs b/crates/uv-distribution/src/distribution_database.rs index dbe968067..3b22dfbaa 100644 --- a/crates/uv-distribution/src/distribution_database.rs +++ b/crates/uv-distribution/src/distribution_database.rs @@ -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 { + 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()