Skip to content

Commit 84fa3e3

Browse files
authored
Merge pull request #105 from kurrent-io/user-agent
Add user agent string so that backend can identity
2 parents c643644 + 9972710 commit 84fa3e3

8 files changed

Lines changed: 50 additions & 15 deletions

File tree

Justfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ clean:
1313

1414
# builds esc
1515
build:
16-
cargo build --locked
16+
ESC_CLIENT_VERSION={{`cargo metadata --no-deps --format-version 1 | jq -r '.packages[] | select(.name == "esc") | .version'` }} cargo build --locked
1717

1818
# builds in release mode
1919
build-release:
20-
cargo build --locked --release
20+
ESC_CLIENT_VERSION={{`cargo metadata --no-deps --format-version 1 | jq -r '.packages[] | select(.name == "esc") | .version'` }} cargo build --locked --release
2121

2222
# Formats the code base
2323
fmt:

base/src/client/builder.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use crate::identity::Token;
2-
use crate::sender::RequestSender;
3-
use super::client::Client;
4-
use super::authorization::StaticTokenAuthorizer;
5-
6-
fn static_token_client(token: Token) -> Client {
7-
let authorizer = StaticTokenAut
8-
}
1+
pub fn build_http_client() -> reqwest::Client {
2+
reqwest::Client::builder()
3+
.user_agent(crate::version::user_agent())
4+
.build()
5+
.expect("Failed to build HTTP client")
6+
}

base/src/client/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#![allow(clippy::module_inception)]
22
mod authorization;
3+
mod builder;
34
mod client;
45

56
pub use authorization::Authorization;
67
pub use authorization::StaticTokenAuthorizer;
8+
pub use builder::build_http_client;
79
pub use client::Client;

base/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ pub mod errors;
88
pub mod identity;
99
pub mod requests;
1010
pub mod utils;
11+
pub mod version;
1112

13+
pub use client::build_http_client;
1214
pub use client::Authorization;
1315
pub use client::Client;
1416
pub use errors::EscError as Error;
@@ -17,3 +19,4 @@ pub use identity::tokens::Token;
1719
pub use requests::RequestObserver;
1820
pub use requests::RequestSender;
1921
pub use utils::urlencode;
22+
pub use version::user_agent;

base/src/requests/sender.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ impl Sender {
4444
.request(method.clone(), url.as_str())
4545
.header("Authorization", authorization_header)
4646
.header("Content-Type", "application/json")
47-
.header("Accept", "application/json");
47+
.header("Accept", "application/json")
48+
.header("User-Agent", &crate::version::user_agent());
4849
let req = match body {
4950
Some(b) => req.json(b),
5051
None => req,

base/src/sender.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ impl RequestSender {
5151
format!("{} {}", token.token_type, token.access_token),
5252
)
5353
.header("Content-Type", "application/json")
54-
.header("Accept", "application/json");
54+
.header("Accept", "application/json")
55+
.header("User-Agent", &crate::version::user_agent());
5556
let req = match body {
5657
Some(b) => req.json(b),
5758
None => req,

base/src/version.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// The version of the ESC client, set at build time
2+
pub const CLIENT_VERSION: &str = match option_env!("ESC_CLIENT_VERSION") {
3+
Some(v) => v,
4+
None => env!("CARGO_PKG_VERSION"), // fallback to base crate version
5+
};
6+
7+
/// Creates the user agent string for HTTP requests
8+
pub fn user_agent() -> String {
9+
format!("esc-client/{}", CLIENT_VERSION)
10+
}
11+
12+
#[cfg(test)]
13+
mod tests {
14+
use super::*;
15+
16+
#[test]
17+
fn test_user_agent_format() {
18+
let ua = user_agent();
19+
assert!(ua.starts_with("esc-client/"));
20+
// Should contain either the environment variable version or fallback version
21+
assert!(ua.len() > "esc-client/".len());
22+
}
23+
24+
#[test]
25+
fn test_client_version_has_valid_format() {
26+
// CLIENT_VERSION should contain version-like content (digits and dots)
27+
assert!(CLIENT_VERSION.chars().any(|c| c.is_ascii_digit()));
28+
}
29+
}

cli/src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod v1;
1414

1515
use cidr::Cidr;
1616
use esc_api::resources::MfaStatus;
17+
use esc_client_base::build_http_client;
1718

1819
use esc_api::{GroupId, MemberId, OrgId};
1920
use output::OutputFormat;
@@ -1902,7 +1903,7 @@ async fn get_token(
19021903
refresh_token: Option<String>,
19031904
noninteractive: bool,
19041905
) -> Result<esc_api::Token, Box<dyn std::error::Error>> {
1905-
let client = reqwest::Client::new();
1906+
let client = build_http_client();
19061907
match refresh_token {
19071908
Some(refresh_token) => {
19081909
let otp_prompt: Option<esc_client_base::identity::operations::OtpPrompt> =
@@ -1966,7 +1967,7 @@ impl ClientBuilder {
19661967
authorization_header: token.authorization_header(),
19671968
};
19681969
let sender = esc_api::RequestSender {
1969-
client: reqwest::Client::new(),
1970+
client: build_http_client(),
19701971
observer: self.observer,
19711972
};
19721973
let client = esc_api::Client {
@@ -2166,7 +2167,7 @@ async fn call_api<'a, 'b>(
21662167

21672168
AccessCommand::Tokens(tokens) => match tokens.tokens_command {
21682169
TokensCommand::Create(params) => {
2169-
let client = reqwest::Client::new();
2170+
let client = build_http_client();
21702171
let mut store = esc_client_store::token_store(token_config).await?;
21712172

21722173
match client_builder.noninteractive {

0 commit comments

Comments
 (0)