From 10dfd43af97ac5368b6a1c9340441df048d32f68 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 8 Apr 2024 10:45:26 -0400 Subject: [PATCH] DRY up HTTP request builder in source database (#2902) --- .../src/distribution_database.rs | 7 ++-- crates/uv-distribution/src/source/mod.rs | 41 ++++++++----------- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/crates/uv-distribution/src/distribution_database.rs b/crates/uv-distribution/src/distribution_database.rs index ab1475153..d8c0a0cf4 100644 --- a/crates/uv-distribution/src/distribution_database.rs +++ b/crates/uv-distribution/src/distribution_database.rs @@ -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, diff --git a/crates/uv-distribution/src/source/mod.rs b/crates/uv-distribution/src/source/mod.rs index 4be6443d4..f968b94ea 100644 --- a/crates/uv-distribution/src/source/mod.rs +++ b/crates/uv-distribution/src/source/mod.rs @@ -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 { + 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)]