middlewaring

main
Zynh0722 2024-02-09 02:20:03 -08:00
parent d747d1a6fa
commit 2d1aad888d
3 changed files with 24 additions and 2 deletions

1
Cargo.lock generated
View File

@ -466,6 +466,7 @@ dependencies = [
"reqwest",
"reqwest-middleware",
"riven",
"task-local-extensions",
"tokio",
]

View File

@ -10,6 +10,7 @@ async-trait = "0.1.77"
dotenvy = "0.15.7"
redis = "0.24.0"
reqwest = "0.11.24"
task-local-extensions = "0.1.4"
reqwest-middleware = { git = "https://git.zynh.me/Zynh0722/reqwest-middleware.git" }
riven = { git = "https://git.zynh.me/Zynh0722/Riven.git", branch = "feat-middleware" }
tokio = { version = "1.36.0", features = ["full"] }

View File

@ -1,7 +1,26 @@
use std::error::Error;
use reqwest_middleware::ClientBuilder;
use reqwest::Response;
use reqwest_middleware::{ClientBuilder, Middleware, Next};
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]
async fn main() -> Result<(), Box<dyn Error>> {
@ -31,7 +50,8 @@ fn fetch_api() -> Result<RiotApi, Box<dyn Error>> {
reqwest::ClientBuilder::new()
.default_headers(default_headers)
.build()?,
);
)
.with(LoggingMiddleware);
let config = RiotApiConfig::with_client_builder(c_builder);
Ok(RiotApi::new(config))