Enable Registry Client Builder to be created from Base Client Builder (#4729)

<!--
Thank you for contributing to uv! To help us out with reviewing, please
consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->
Addresses https://github.com/astral-sh/uv/issues/4330, to reduce
duplication in the client creation logic.
## Test Plan

<!-- How was it tested? -->
https://github.com/astral-sh/uv/pull/4729#issuecomment-2204681655
This commit is contained in:
Danny 2024-07-04 11:53:05 -04:00 committed by GitHub
parent 0a336dacab
commit 35afcfd053
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 34 additions and 59 deletions

View file

@ -37,14 +37,8 @@ use crate::{CachedClient, CachedClientError, Error, ErrorKind};
pub struct RegistryClientBuilder<'a> { pub struct RegistryClientBuilder<'a> {
index_urls: IndexUrls, index_urls: IndexUrls,
index_strategy: IndexStrategy, index_strategy: IndexStrategy,
keyring: KeyringProviderType,
native_tls: bool,
retries: u32,
connectivity: Connectivity,
cache: Cache, cache: Cache,
client: Option<Client>, base_client_builder: BaseClientBuilder<'a>,
markers: Option<&'a MarkerEnvironment>,
platform: Option<&'a Platform>,
} }
impl RegistryClientBuilder<'_> { impl RegistryClientBuilder<'_> {
@ -52,14 +46,8 @@ impl RegistryClientBuilder<'_> {
Self { Self {
index_urls: IndexUrls::default(), index_urls: IndexUrls::default(),
index_strategy: IndexStrategy::default(), index_strategy: IndexStrategy::default(),
keyring: KeyringProviderType::default(),
native_tls: false,
cache, cache,
connectivity: Connectivity::Online, base_client_builder: BaseClientBuilder::new(),
retries: 3,
client: None,
markers: None,
platform: None,
} }
} }
} }
@ -79,25 +67,25 @@ impl<'a> RegistryClientBuilder<'a> {
#[must_use] #[must_use]
pub fn keyring(mut self, keyring_type: KeyringProviderType) -> Self { pub fn keyring(mut self, keyring_type: KeyringProviderType) -> Self {
self.keyring = keyring_type; self.base_client_builder = self.base_client_builder.keyring(keyring_type);
self self
} }
#[must_use] #[must_use]
pub fn connectivity(mut self, connectivity: Connectivity) -> Self { pub fn connectivity(mut self, connectivity: Connectivity) -> Self {
self.connectivity = connectivity; self.base_client_builder = self.base_client_builder.connectivity(connectivity);
self self
} }
#[must_use] #[must_use]
pub fn retries(mut self, retries: u32) -> Self { pub fn retries(mut self, retries: u32) -> Self {
self.retries = retries; self.base_client_builder = self.base_client_builder.retries(retries);
self self
} }
#[must_use] #[must_use]
pub fn native_tls(mut self, native_tls: bool) -> Self { pub fn native_tls(mut self, native_tls: bool) -> Self {
self.native_tls = native_tls; self.base_client_builder = self.base_client_builder.native_tls(native_tls);
self self
} }
@ -109,44 +97,27 @@ impl<'a> RegistryClientBuilder<'a> {
#[must_use] #[must_use]
pub fn client(mut self, client: Client) -> Self { pub fn client(mut self, client: Client) -> Self {
self.client = Some(client); self.base_client_builder = self.base_client_builder.client(client);
self self
} }
#[must_use] #[must_use]
pub fn markers(mut self, markers: &'a MarkerEnvironment) -> Self { pub fn markers(mut self, markers: &'a MarkerEnvironment) -> Self {
self.markers = Some(markers); self.base_client_builder = self.base_client_builder.markers(markers);
self self
} }
#[must_use] #[must_use]
pub fn platform(mut self, platform: &'a Platform) -> Self { pub fn platform(mut self, platform: &'a Platform) -> Self {
self.platform = Some(platform); self.base_client_builder = self.base_client_builder.platform(platform);
self self
} }
pub fn build(self) -> RegistryClient { pub fn build(self) -> RegistryClient {
// Build a base client // Build a base client
let mut builder = BaseClientBuilder::new(); let builder = self.base_client_builder;
if let Some(client) = self.client { let client = builder.build();
builder = builder.client(client);
}
if let Some(markers) = self.markers {
builder = builder.markers(markers);
}
if let Some(platform) = self.platform {
builder = builder.platform(platform);
}
let client = builder
.retries(self.retries)
.connectivity(self.connectivity)
.native_tls(self.native_tls)
.keyring(self.keyring)
.build();
let timeout = client.timeout(); let timeout = client.timeout();
let connectivity = client.connectivity(); let connectivity = client.connectivity();
@ -165,6 +136,17 @@ impl<'a> RegistryClientBuilder<'a> {
} }
} }
impl<'a> From<BaseClientBuilder<'a>> for RegistryClientBuilder<'a> {
fn from(value: BaseClientBuilder<'a>) -> Self {
Self {
index_urls: IndexUrls::default(),
index_strategy: IndexStrategy::default(),
cache: Cache::temp().unwrap(),
base_client_builder: value,
}
}
}
/// A client for fetching packages from a `PyPI`-compatible index. /// A client for fetching packages from a `PyPI`-compatible index.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct RegistryClient { pub struct RegistryClient {

View file

@ -257,12 +257,11 @@ pub(crate) async fn pip_compile(
} }
// Initialize the registry client. // Initialize the registry client.
let client = RegistryClientBuilder::new(cache.clone())
.native_tls(native_tls) let client = RegistryClientBuilder::from(client_builder)
.connectivity(connectivity) .cache(cache.clone())
.index_urls(index_locations.index_urls()) .index_urls(index_locations.index_urls())
.index_strategy(index_strategy) .index_strategy(index_strategy)
.keyring(keyring_provider)
.markers(interpreter.markers()) .markers(interpreter.markers())
.platform(interpreter.platform()) .platform(interpreter.platform())
.build(); .build();

View file

@ -256,12 +256,10 @@ pub(crate) async fn pip_install(
} }
// Initialize the registry client. // Initialize the registry client.
let client = RegistryClientBuilder::new(cache.clone()) let client = RegistryClientBuilder::from(client_builder)
.native_tls(native_tls) .cache(cache.clone())
.connectivity(connectivity)
.index_urls(index_locations.index_urls()) .index_urls(index_locations.index_urls())
.index_strategy(index_strategy) .index_strategy(index_strategy)
.keyring(keyring_provider)
.markers(&markers) .markers(&markers)
.platform(interpreter.platform()) .platform(interpreter.platform())
.build(); .build();

View file

@ -203,12 +203,10 @@ pub(crate) async fn pip_sync(
} }
// Initialize the registry client. // Initialize the registry client.
let client = RegistryClientBuilder::new(cache.clone()) let client = RegistryClientBuilder::from(client_builder)
.native_tls(native_tls) .cache(cache.clone())
.connectivity(connectivity)
.index_urls(index_locations.index_urls()) .index_urls(index_locations.index_urls())
.index_strategy(index_strategy) .index_strategy(index_strategy)
.keyring(keyring_provider)
.markers(&markers) .markers(&markers)
.platform(interpreter.platform()) .platform(interpreter.platform())
.build(); .build();

View file

@ -94,12 +94,9 @@ pub(crate) async fn add(
resolution_environment(python_version, python_platform, venv.interpreter())?; resolution_environment(python_version, python_platform, venv.interpreter())?;
// Initialize the registry client. // Initialize the registry client.
let client = RegistryClientBuilder::new(cache.clone()) let client = RegistryClientBuilder::from(client_builder)
.native_tls(native_tls)
.connectivity(connectivity)
.index_urls(settings.index_locations.index_urls()) .index_urls(settings.index_locations.index_urls())
.index_strategy(settings.index_strategy) .index_strategy(settings.index_strategy)
.keyring(settings.keyring_provider)
.markers(&markers) .markers(&markers)
.platform(venv.interpreter().platform()) .platform(venv.interpreter().platform())
.build(); .build();

View file

@ -129,6 +129,8 @@ async fn venv_impl(
.connectivity(connectivity) .connectivity(connectivity)
.native_tls(native_tls); .native_tls(native_tls);
let client_builder_clone = client_builder.clone();
let mut interpreter_request = python_request.map(PythonRequest::parse); let mut interpreter_request = python_request.map(PythonRequest::parse);
if preview.is_enabled() && interpreter_request.is_none() { if preview.is_enabled() && interpreter_request.is_none() {
interpreter_request = request_from_version_file().await.into_diagnostic()?; interpreter_request = request_from_version_file().await.into_diagnostic()?;
@ -184,12 +186,11 @@ async fn venv_impl(
let interpreter = venv.interpreter(); let interpreter = venv.interpreter();
// Instantiate a client. // Instantiate a client.
let client = RegistryClientBuilder::new(cache.clone()) let client = RegistryClientBuilder::from(client_builder_clone)
.native_tls(native_tls) .cache(cache.clone())
.index_urls(index_locations.index_urls()) .index_urls(index_locations.index_urls())
.index_strategy(index_strategy) .index_strategy(index_strategy)
.keyring(keyring_provider) .keyring(keyring_provider)
.connectivity(connectivity)
.markers(interpreter.markers()) .markers(interpreter.markers())
.platform(interpreter.platform()) .platform(interpreter.platform())
.build(); .build();