Skip to content

Commit b0554f3

Browse files
committed
chore: add crate name and version to all http clients
Signed-off-by: JP-Ellis <josh@jpellis.me>
1 parent 31dee39 commit b0554f3

5 files changed

Lines changed: 27 additions & 7 deletions

File tree

rust/pact_matching/src/metrics.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,16 @@ pub async fn send_metrics_async(event: MetricEvent) {
190190
"ev" => value.as_str() // Value
191191
};
192192
debug!("Sending event to GA - {:?}", event_payload);
193-
let result = Client::new().post(GA_URL)
193+
let client = match Client::builder()
194+
.user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")))
195+
.build() {
196+
Ok(c) => c,
197+
Err(err) => {
198+
debug!("Failed to build HTTP client: {}", err);
199+
return;
200+
}
201+
};
202+
let result = client.post(GA_URL)
194203
.form(&event_payload)
195204
.send()
196205
.await;

rust/pact_models/src/http_utils.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ impl HttpAuth {
3030

3131
/// Fetches the JSON from a URL
3232
pub fn fetch_json_from_url(url: &String, auth: &Option<HttpAuth>) -> anyhow::Result<(String, Value)> {
33-
let client = Client::new();
33+
let client = Client::builder()
34+
.user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")))
35+
.build()
36+
.map_err(|e| anyhow::anyhow!("Failed to build HTTP client: {}", e))?;
3437
let request = match auth {
3538
&Some(ref auth) => {
3639
match auth {

rust/pact_verifier/src/callback_executors.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22
33
use std::collections::HashMap;
44
use std::fmt::{Debug, Display, Formatter};
5-
use std::sync::Arc;
5+
use std::sync::{Arc, LazyLock};
66

77
use async_trait::async_trait;
88
use bytes::Bytes;
99
use itertools::Either;
1010
use maplit::*;
1111
use serde_json::{json, Value};
1212

13+
static DEFAULT_CLIENT: LazyLock<reqwest::Client> = LazyLock::new(|| {
14+
reqwest::Client::builder()
15+
.user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")))
16+
.build()
17+
.expect("Failed to build default HTTP client")
18+
});
19+
1320
use pact_models::bodies::OptionalBody;
1421
use pact_models::content_types::JSON;
1522
use pact_models::provider_states::ProviderState;
@@ -153,7 +160,7 @@ impl ProviderStateExecutor for HttpRequestProviderStateExecutor {
153160
}
154161
state_change_request.query = Some(query);
155162
}
156-
make_state_change_request(client.unwrap_or(&reqwest::Client::default()), &state_change_url, &state_change_request, self.retries).await
163+
make_state_change_request(client.unwrap_or(&DEFAULT_CLIENT), &state_change_url, &state_change_request, self.retries).await
157164
.map_err(|err| ProviderStateError { description: err.to_string(), interaction_id }.into())
158165
},
159166
None => {

rust/pact_verifier/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ pub(crate) fn configure_http_client<F: RequestFilterExecutor>(
701701
options: &VerificationOptions<F>
702702
) -> anyhow::Result<Client> {
703703
let mut client_builder = reqwest::Client::builder()
704+
.user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")))
704705
.danger_accept_invalid_certs(options.disable_ssl_verification)
705706
.timeout(Duration::from_millis(options.request_timeout));
706707

rust/pact_verifier/src/pact_broker.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl HALClientBuilder {
231231
pub fn build(&self) -> HALClient {
232232
HALClient {
233233
client: self.client.clone().unwrap_or_else(|| reqwest::ClientBuilder::new()
234-
.user_agent(format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")))
234+
.user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")))
235235
.tcp_keepalive(None)
236236
.build()
237237
.unwrap()),
@@ -603,7 +603,7 @@ impl Default for HALClient {
603603
fn default() -> Self {
604604
HALClient {
605605
client: reqwest::ClientBuilder::new()
606-
.user_agent(format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")))
606+
.user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")))
607607
.tcp_keepalive(None)
608608
.build()
609609
.unwrap(),
@@ -1316,7 +1316,7 @@ mod tests {
13161316
.interaction("a request to the broker includes a user-agent", "", |mut i| {
13171317
i.request
13181318
.path("/user-agent")
1319-
.header("user-agent", format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")));
1319+
.header("user-agent", concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")));
13201320

13211321
i.response
13221322
.status(200)

0 commit comments

Comments
 (0)