From 73e24fdf19f2a5b76aff1f76d13ece329e4fb0a3 Mon Sep 17 00:00:00 2001 From: Max Howell Date: Mon, 5 May 2025 09:39:38 -0400 Subject: [PATCH] Add User-Agent Allow differentiation for our CI/CD. --- crates/lib/src/client.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/crates/lib/src/client.rs b/crates/lib/src/client.rs index f8dd7d3c8..696ca3da3 100644 --- a/crates/lib/src/client.rs +++ b/crates/lib/src/client.rs @@ -1,3 +1,5 @@ +use std::env; + use reqwest::{Client, ClientBuilder}; #[cfg(not(any(target_os = "macos", target_os = "windows")))] @@ -12,10 +14,25 @@ pub fn build_client() -> Result> { builder = builder.add_root_certificate(cert); } + builder = builder.user_agent(get_user_agent()); + Ok(builder.build()?) } #[cfg(any(target_os = "macos", target_os = "windows"))] pub fn build_client() -> Result> { - Ok(ClientBuilder::new().build()?) + Ok(ClientBuilder::new().user_agent(get_user_agent()).build()?) +} + +fn get_user_agent() -> String { + let version = env!("CARGO_PKG_VERSION"); + let os = std::env::consts::OS; + let arch = std::env::consts::ARCH; + let group = env::var("PKGX_USER_AGENT_GROUP"); + let name = if group.is_ok() { + format!("pkgx[{}]", group.unwrap()) + } else { + "pkgx".to_string() + }; + format!("{name}/{version} ({os}; {arch})") }