middlewaring
parent
d747d1a6fa
commit
2d1aad888d
|
@ -466,6 +466,7 @@ dependencies = [
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"reqwest-middleware",
|
"reqwest-middleware",
|
||||||
"riven",
|
"riven",
|
||||||
|
"task-local-extensions",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ async-trait = "0.1.77"
|
||||||
dotenvy = "0.15.7"
|
dotenvy = "0.15.7"
|
||||||
redis = "0.24.0"
|
redis = "0.24.0"
|
||||||
reqwest = "0.11.24"
|
reqwest = "0.11.24"
|
||||||
|
task-local-extensions = "0.1.4"
|
||||||
reqwest-middleware = { git = "https://git.zynh.me/Zynh0722/reqwest-middleware.git" }
|
reqwest-middleware = { git = "https://git.zynh.me/Zynh0722/reqwest-middleware.git" }
|
||||||
riven = { git = "https://git.zynh.me/Zynh0722/Riven.git", branch = "feat-middleware" }
|
riven = { git = "https://git.zynh.me/Zynh0722/Riven.git", branch = "feat-middleware" }
|
||||||
tokio = { version = "1.36.0", features = ["full"] }
|
tokio = { version = "1.36.0", features = ["full"] }
|
||||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -1,7 +1,26 @@
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
use reqwest_middleware::ClientBuilder;
|
use reqwest::Response;
|
||||||
|
use reqwest_middleware::{ClientBuilder, Middleware, Next};
|
||||||
use riven::{RiotApi, RiotApiConfig};
|
use riven::{RiotApi, RiotApiConfig};
|
||||||
|
use task_local_extensions::Extensions;
|
||||||
|
|
||||||
|
struct LoggingMiddleware;
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl Middleware for LoggingMiddleware {
|
||||||
|
async fn handle(
|
||||||
|
&self,
|
||||||
|
req: reqwest::Request,
|
||||||
|
extensions: &mut Extensions,
|
||||||
|
next: Next<'_>,
|
||||||
|
) -> reqwest_middleware::Result<Response> {
|
||||||
|
println!("Request started {:#?}", req);
|
||||||
|
let res = next.run(req, extensions).await;
|
||||||
|
println!("Result: {:#?}", res);
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
@ -31,7 +50,8 @@ fn fetch_api() -> Result<RiotApi, Box<dyn Error>> {
|
||||||
reqwest::ClientBuilder::new()
|
reqwest::ClientBuilder::new()
|
||||||
.default_headers(default_headers)
|
.default_headers(default_headers)
|
||||||
.build()?,
|
.build()?,
|
||||||
);
|
)
|
||||||
|
.with(LoggingMiddleware);
|
||||||
let config = RiotApiConfig::with_client_builder(c_builder);
|
let config = RiotApiConfig::with_client_builder(c_builder);
|
||||||
|
|
||||||
Ok(RiotApi::new(config))
|
Ok(RiotApi::new(config))
|
||||||
|
|
Loading…
Reference in New Issue