Use automatically generated user agent for http requests based on crate metadata
This commit is contained in:
parent
af8e480ef8
commit
91a7b4d2f1
1 changed files with 8 additions and 6 deletions
|
@ -2,13 +2,15 @@ use async_trait::async_trait;
|
||||||
use reqwest::{Request, Response};
|
use reqwest::{Request, Response};
|
||||||
use reqwest_middleware::{Middleware, Next, Result};
|
use reqwest_middleware::{Middleware, Next, Result};
|
||||||
|
|
||||||
pub const USER_AGENT: &str = "osrs-prometheus-exporter";
|
pub struct DefaultHeaderMiddleware {
|
||||||
|
user_agent: String,
|
||||||
pub struct DefaultHeaderMiddleware {}
|
}
|
||||||
|
|
||||||
impl DefaultHeaderMiddleware {
|
impl DefaultHeaderMiddleware {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
DefaultHeaderMiddleware {}
|
DefaultHeaderMiddleware {
|
||||||
|
user_agent: format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,8 +22,8 @@ impl Middleware for DefaultHeaderMiddleware {
|
||||||
extensions: &mut task_local_extensions::Extensions,
|
extensions: &mut task_local_extensions::Extensions,
|
||||||
next: Next<'_>,
|
next: Next<'_>,
|
||||||
) -> Result<Response> {
|
) -> Result<Response> {
|
||||||
req.headers_mut()
|
let ua = self.user_agent.as_str();
|
||||||
.insert("User-Agent", USER_AGENT.parse().unwrap());
|
req.headers_mut().insert("User-Agent", ua.parse().unwrap());
|
||||||
|
|
||||||
next.run(req, extensions).await
|
next.run(req, extensions).await
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue