Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add linehaul info to uv-client #2493

Merged
merged 6 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ owo-colors = { version = "4.0.0" }
pathdiff = { version = "0.2.1" }
petgraph = { version = "0.6.4" }
platform-info = { version = "2.0.2" }
plist = { version = "1.6.0" }
pubgrub = { git = "https://github.com/astral-sh/pubgrub", rev = "addbaf184891d66a2dfd93d241a66d13bfe5de86" }
pyo3 = { version = "0.20.3" }
pyo3-log = { version = "0.9.0" }
Expand All @@ -89,6 +90,7 @@ serde = { version = "1.0.197" }
serde_json = { version = "1.0.114" }
sha1 = { version = "0.10.6" }
sha2 = { version = "0.10.8" }
sys-info = { version = "0.9.1" }
target-lexicon = { version = "0.12.14" }
task-local-extensions = { version = "0.1.4" }
tempfile = { version = "3.9.0" }
Expand Down
4 changes: 4 additions & 0 deletions crates/uv-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ platform-tags = { path = "../platform-tags" }
uv-auth = { path = "../uv-auth" }
uv-cache = { path = "../uv-cache" }
uv-fs = { path = "../uv-fs", features = ["tokio"] }
uv-interpreter = { path = "../uv-interpreter" }
uv-normalize = { path = "../uv-normalize" }
uv-version = { path = "../uv-version" }
uv-warnings = { path = "../uv-warnings" }
Expand All @@ -28,6 +29,7 @@ fs-err = { workspace = true, features = ["tokio"] }
futures = { workspace = true }
html-escape = { workspace = true }
http = { workspace = true }
plist = { workspace = true }
reqwest = { workspace = true }
reqwest-middleware = { workspace = true }
reqwest-retry = { workspace = true }
Expand All @@ -37,6 +39,7 @@ rustc-hash = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
sha2 = { workspace = true }
sys-info = { workspace = true }
task-local-extensions = { workspace = true }
tempfile = { workspace = true }
thiserror = { workspace = true }
Expand All @@ -56,4 +59,5 @@ webpki-roots = { version = "0.25.4" }
anyhow = { workspace = true }
hyper = { version = "0.14.28", features = ["server", "http1"] }
insta = { version = "1.36.1" }
os_info = { version = "3.7.0", default-features = false }
tokio = { workspace = true, features = ["fs", "macros"] }
35 changes: 31 additions & 4 deletions crates/uv-client/src/base_client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use pep508_rs::MarkerEnvironment;
use platform_tags::Platform;
use reqwest::{Client, ClientBuilder};
use reqwest_middleware::ClientWithMiddleware;
use reqwest_retry::policies::ExponentialBackoff;
Expand All @@ -12,33 +14,38 @@ use uv_fs::Simplified;
use uv_version::version;
use uv_warnings::warn_user_once;

use crate::linehaul::LineHaul;
use crate::middleware::OfflineMiddleware;
use crate::tls::Roots;
use crate::{tls, Connectivity};

/// A builder for an [`RegistryClient`].
#[derive(Debug, Clone)]
pub struct BaseClientBuilder {
pub struct BaseClientBuilder<'a> {
keyring_provider: KeyringProvider,
native_tls: bool,
retries: u32,
connectivity: Connectivity,
client: Option<Client>,
markers: Option<&'a MarkerEnvironment>,
platform: Option<&'a Platform>,
}

impl BaseClientBuilder {
impl BaseClientBuilder<'_> {
pub fn new() -> Self {
Self {
keyring_provider: KeyringProvider::default(),
native_tls: false,
connectivity: Connectivity::Online,
retries: 3,
client: None,
markers: None,
platform: None,
}
}
}

impl BaseClientBuilder {
impl<'a> BaseClientBuilder<'a> {
#[must_use]
pub fn keyring_provider(mut self, keyring_provider: KeyringProvider) -> Self {
self.keyring_provider = keyring_provider;
Expand Down Expand Up @@ -69,9 +76,29 @@ impl BaseClientBuilder {
self
}

#[must_use]
pub fn markers(mut self, markers: &'a MarkerEnvironment) -> Self {
self.markers = Some(markers);
self
}

#[must_use]
pub fn platform(mut self, platform: &'a Platform) -> Self {
self.platform = Some(platform);
self
}

pub fn build(self) -> BaseClient {
// Create user agent.
let user_agent_string = format!("uv/{}", version());
let mut user_agent_string = format!("uv/{}", version());

// Add linehaul metadata.
if let Some(markers) = self.markers {
let linehaul = LineHaul::new(markers, self.platform);
if let Ok(output) = serde_json::to_string(&linehaul) {
user_agent_string += &format!(" {}", output);
}
}

// Timeout options, matching https://doc.rust-lang.org/nightly/cargo/reference/config.html#httptimeout
// `UV_REQUEST_TIMEOUT` is provided for backwards compatibility with v0.1.6
Expand Down
3 changes: 3 additions & 0 deletions crates/uv-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub use base_client::BaseClient;
pub use cached_client::{CacheControl, CachedClient, CachedClientError, DataWithCachePolicy};
pub use error::{BetterReqwestError, Error, ErrorKind};
pub use flat_index::{FlatDistributions, FlatIndex, FlatIndexClient, FlatIndexError};
pub use linehaul::LineHaul;
pub use registry_client::{
Connectivity, RegistryClient, RegistryClientBuilder, SimpleMetadata, SimpleMetadatum,
VersionFiles,
Expand All @@ -14,6 +15,8 @@ mod error;
mod flat_index;
mod html;
mod httpcache;
mod linehaul;
mod mac_version;
mod middleware;
mod registry_client;
mod remote_metadata;
Expand Down
Loading
Loading