Skip to content

Commit 73e24fd

Browse files
committed
Add User-Agent
Allow differentiation for our CI/CD.
1 parent 52375e5 commit 73e24fd

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

crates/lib/src/client.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::env;
2+
13
use reqwest::{Client, ClientBuilder};
24

35
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
@@ -12,10 +14,25 @@ pub fn build_client() -> Result<Client, Box<dyn std::error::Error>> {
1214
builder = builder.add_root_certificate(cert);
1315
}
1416

17+
builder = builder.user_agent(get_user_agent());
18+
1519
Ok(builder.build()?)
1620
}
1721

1822
#[cfg(any(target_os = "macos", target_os = "windows"))]
1923
pub fn build_client() -> Result<Client, Box<dyn std::error::Error>> {
20-
Ok(ClientBuilder::new().build()?)
24+
Ok(ClientBuilder::new().user_agent(get_user_agent()).build()?)
25+
}
26+
27+
fn get_user_agent() -> String {
28+
let version = env!("CARGO_PKG_VERSION");
29+
let os = std::env::consts::OS;
30+
let arch = std::env::consts::ARCH;
31+
let group = env::var("PKGX_USER_AGENT_GROUP");
32+
let name = if group.is_ok() {
33+
format!("pkgx[{}]", group.unwrap())
34+
} else {
35+
"pkgx".to_string()
36+
};
37+
format!("{name}/{version} ({os}; {arch})")
2138
}

0 commit comments

Comments
 (0)