From 2d1aad888d1491f703eb76f054ad9402f2517c68 Mon Sep 17 00:00:00 2001 From: Zynh0722 Date: Fri, 9 Feb 2024 02:20:03 -0800 Subject: [PATCH] middlewaring --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 24 ++++++++++++++++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dc51259..03b7274 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -466,6 +466,7 @@ dependencies = [ "reqwest", "reqwest-middleware", "riven", + "task-local-extensions", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index d6471bc..9ff4beb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/src/main.rs b/src/main.rs index e4c4aa0..56fef9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 { + println!("Request started {:#?}", req); + let res = next.run(req, extensions).await; + println!("Result: {:#?}", res); + res + } +} #[tokio::main] async fn main() -> Result<(), Box> { @@ -31,7 +50,8 @@ fn fetch_api() -> Result> { reqwest::ClientBuilder::new() .default_headers(default_headers) .build()?, - ); + ) + .with(LoggingMiddleware); let config = RiotApiConfig::with_client_builder(c_builder); Ok(RiotApi::new(config))