Skip to content

Commit

Permalink
Add a more descriptive user agent to http client(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Aug 10, 2024
1 parent 2b76578 commit 8dfa4db
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
19 changes: 17 additions & 2 deletions lib/sources/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::time::Duration;

use reqwest::{header::HeaderMap, Client, Error};
use reqwest::{
header::{HeaderMap, USER_AGENT},
Client, Error,
};

use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use reqwest_tracing::TracingMiddleware;
Expand All @@ -26,8 +30,18 @@ fn add_client_middleware(client: Client) -> ClientWithMiddleware {
- HTTPS only
- Timeouts for connection and response
- All common compression algorithms enabled
- User agent set to `<crate_name>/<crate_version> (<repository_url>)`
*/
pub fn create_client(default_headers: HeaderMap) -> Result<ClientWithMiddleware, Error> {
pub fn create_client(mut default_headers: HeaderMap) -> Result<ClientWithMiddleware, Error> {
let user_agent = format!(
"{}/{} ({})",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
env!("CARGO_PKG_REPOSITORY"),
);

default_headers.insert(USER_AGENT, user_agent.parse().unwrap());

let client = Client::builder()
.default_headers(default_headers)
.https_only(true)
Expand All @@ -37,5 +51,6 @@ pub fn create_client(default_headers: HeaderMap) -> Result<ClientWithMiddleware,
.brotli(true)
.deflate(true)
.build()?;

Ok(add_client_middleware(client))
}
3 changes: 1 addition & 2 deletions lib/sources/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::de::DeserializeOwned;
use tracing::{debug, instrument};

use reqwest::{
header::{HeaderMap, HeaderName, HeaderValue, ACCEPT, AUTHORIZATION, USER_AGENT},
header::{HeaderMap, HeaderName, HeaderValue, ACCEPT, AUTHORIZATION},
StatusCode,
};

Expand Down Expand Up @@ -32,7 +32,6 @@ impl GithubProvider {
let has_auth = pat.is_some();
let headers = {
let mut headers = HeaderMap::new();
headers.insert(USER_AGENT, HeaderValue::from_static("rokit"));
headers.insert(
HeaderName::from_static("x-github-api-version"),
HeaderValue::from_static("2022-11-28"),
Expand Down

0 comments on commit 8dfa4db

Please sign in to comment.