Move reqwest middle into its own folder
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
7e9232b3f3
commit
af8e480ef8
4 changed files with 34 additions and 27 deletions
|
@ -1,11 +1,8 @@
|
|||
use async_trait::async_trait;
|
||||
use reqwest::{Request, Response};
|
||||
use middleware::DefaultHeaderMiddleware;
|
||||
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
|
||||
use reqwest_middleware::{Middleware, Next, Result};
|
||||
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
|
||||
use reqwest_tracing::TracingMiddleware;
|
||||
|
||||
pub const USER_AGENT: &str = "osrs-prometheus-exporter";
|
||||
use crate::transport::middleware;
|
||||
|
||||
pub fn new() -> ClientWithMiddleware {
|
||||
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);
|
||||
|
@ -19,25 +16,3 @@ pub fn new() -> ClientWithMiddleware {
|
|||
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
|
||||
.build()
|
||||
}
|
||||
|
||||
struct DefaultHeaderMiddleware {}
|
||||
|
||||
impl DefaultHeaderMiddleware {
|
||||
fn new() -> Self {
|
||||
DefaultHeaderMiddleware {}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Middleware for DefaultHeaderMiddleware {
|
||||
async fn handle(
|
||||
&self,
|
||||
mut req: Request,
|
||||
extensions: &mut task_local_extensions::Extensions,
|
||||
next: Next<'_>,
|
||||
) -> Result<Response> {
|
||||
req.headers_mut()
|
||||
.insert("User-Agent", USER_AGENT.parse().unwrap());
|
||||
next.run(req, extensions).await
|
||||
}
|
||||
}
|
||||
|
|
28
src/transport/middleware/headers.rs
Normal file
28
src/transport/middleware/headers.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
use async_trait::async_trait;
|
||||
use reqwest::{Request, Response};
|
||||
use reqwest_middleware::{Middleware, Next, Result};
|
||||
|
||||
pub const USER_AGENT: &str = "osrs-prometheus-exporter";
|
||||
|
||||
pub struct DefaultHeaderMiddleware {}
|
||||
|
||||
impl DefaultHeaderMiddleware {
|
||||
pub fn new() -> Self {
|
||||
DefaultHeaderMiddleware {}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Middleware for DefaultHeaderMiddleware {
|
||||
async fn handle(
|
||||
&self,
|
||||
mut req: Request,
|
||||
extensions: &mut task_local_extensions::Extensions,
|
||||
next: Next<'_>,
|
||||
) -> Result<Response> {
|
||||
req.headers_mut()
|
||||
.insert("User-Agent", USER_AGENT.parse().unwrap());
|
||||
|
||||
next.run(req, extensions).await
|
||||
}
|
||||
}
|
3
src/transport/middleware/mod.rs
Normal file
3
src/transport/middleware/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
mod headers;
|
||||
|
||||
pub use headers::DefaultHeaderMiddleware;
|
|
@ -1 +1,2 @@
|
|||
mod middleware;
|
||||
pub mod http;
|
||||
|
|
Loading…
Reference in a new issue