Skip to content

Commit

Permalink
feat: allow passing in a custom reqwest Client (#1745)
Browse files Browse the repository at this point in the history
## Summary

I am looking to instantiate a `RegistryClient`. However, when using the
`RegistryClientBuilder` a new reqwest client is always constructed. I
would like to pass in a custom `reqwest::Client` to be able to share the
http resources with other parts of my application.

## Test Plan

The uv codebase does not use my addition to the builder and all tests
still succeed. And in my code I can pass a custom Client.
  • Loading branch information
baszalmstra authored Feb 20, 2024
1 parent dd7d533 commit daf2800
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions crates/uv-client/src/registry_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct RegistryClientBuilder {
retries: u32,
connectivity: Connectivity,
cache: Cache,
client: Option<Client>,
}

impl RegistryClientBuilder {
Expand All @@ -49,6 +50,7 @@ impl RegistryClientBuilder {
cache,
connectivity: Connectivity::Online,
retries: 3,
client: None,
}
}
}
Expand Down Expand Up @@ -78,19 +80,25 @@ impl RegistryClientBuilder {
self
}

#[must_use]
pub fn client(mut self, client: Client) -> Self {
self.client = Some(client);
self
}

pub fn build(self) -> RegistryClient {
let client_raw = {
let client_raw = self.client.unwrap_or_else(|| {
// Get pip timeout from env var
let default_timeout = 5 * 60;
let timeout = env::var("UV_REQUEST_TIMEOUT")
.map_err(|_| default_timeout)
.and_then(|value| {
value.parse::<u64>()
.map_err(|_| {
warn_user_once!("Ignoring invalid value for UV_REQUEST_TIMEOUT. Expected integer number of seconds, got {value}.");
default_timeout
})
}).unwrap_or(default_timeout);
.map_err(|_| default_timeout)
.and_then(|value| {
value.parse::<u64>()
.map_err(|_| {
warn_user_once!("Ignoring invalid value for UV_REQUEST_TIMEOUT. Expected integer number of seconds, got {value}.");
default_timeout
})
}).unwrap_or(default_timeout);
debug!("Using registry request timeout of {}s", timeout);
// Disallow any connections.
let client_core = ClientBuilder::new()
Expand All @@ -99,7 +107,7 @@ impl RegistryClientBuilder {
.timeout(std::time::Duration::from_secs(timeout));

client_core.build().expect("Failed to build HTTP client.")
};
});

let uncached_client = match self.connectivity {
Connectivity::Online => {
Expand Down

0 comments on commit daf2800

Please sign in to comment.