Skip to content

Commit cfbf821

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

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

crates/lib/src/client.rs

Lines changed: 20 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,27 @@ 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()
25+
.user_agent(get_user_agent())
26+
.build()?)
27+
}
28+
29+
fn get_user_agent() -> String {
30+
let version = env!("CARGO_PKG_VERSION");
31+
let os = std::env::consts::OS;
32+
let arch = std::env::consts::ARCH;
33+
let group = env::var("PKGX_USER_AGENT_GROUP");
34+
let name = if group.is_ok() {
35+
format!("pkgx[{}]", group.unwrap())
36+
} else {
37+
"pkgx".to_string()
38+
};
39+
format!("{name}/{version} ({os}; {arch})")
2140
}

0 commit comments

Comments
 (0)