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> {
index_urls: IndexUrls,
index_strategy: IndexStrategy,
keyring: KeyringProviderType,
native_tls: bool,
retries: u32,
connectivity: Connectivity,
cache: Cache,
client: Option<Client>,
markers: Option<&'a MarkerEnvironment>,
platform: Option<&'a Platform>,
base_client_builder: BaseClientBuilder<'a>,
}
impl RegistryClientBuilder<'_> {
@ -52,14 +46,8 @@ impl RegistryClientBuilder<'_> {
Self {
index_urls: IndexUrls::default(),
index_strategy: IndexStrategy::default(),
keyring: KeyringProviderType::default(),
native_tls: false,
cache,
connectivity: Connectivity::Online,
retries: 3,
client: None,
markers: None,
platform: None,
base_client_builder: BaseClientBuilder::new(),
}
}
}
@ -79,25 +67,25 @@ impl<'a> RegistryClientBuilder<'a> {
#[must_use]
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
}
#[must_use]
pub fn connectivity(mut self, connectivity: Connectivity) -> Self {
self.connectivity = connectivity;
self.base_client_builder = self.base_client_builder.connectivity(connectivity);
self
}
#[must_use]
pub fn retries(mut self, retries: u32) -> Self {
self.retries = retries;
self.base_client_builder = self.base_client_builder.retries(retries);
self
}
#[must_use]
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
}
@ -109,44 +97,27 @@ impl<'a> RegistryClientBuilder<'a> {
#[must_use]
pub fn client(mut self, client: Client) -> Self {
self.client = Some(client);
self.base_client_builder = self.base_client_builder.client(client);
self
}
#[must_use]
pub fn markers(mut self, markers: &'a MarkerEnvironment) -> Self {
self.markers = Some(markers);
self.base_client_builder = self.base_client_builder.markers(markers);
self
}
#[must_use]
pub fn platform(mut self, platform: &'a Platform) -> Self {
self.platform = Some(platform);
self.base_client_builder = self.base_client_builder.platform(platform);
self
}
pub fn build(self) -> RegistryClient {
// Build a base client
let mut builder = BaseClientBuilder::new();
let builder = self.base_client_builder;
if let Some(client) = self.client {
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 client = builder.build();
let timeout = client.timeout();
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.
#[derive(Debug, Clone)]
pub struct RegistryClient {

View file

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

View file

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

View file

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

View file

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

View file

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