From 8a79e44d3d4201266d9578bb81de0c795afc0e1a Mon Sep 17 00:00:00 2001 From: Zynh0722 Date: Fri, 9 Feb 2024 04:01:47 -0800 Subject: [PATCH] moving things to modules --- src/api_setup.rs | 27 +++++++++++++++++++++++++++ src/cache.rs | 20 ++++++++++++++++++++ src/main.rs | 46 +++------------------------------------------- 3 files changed, 50 insertions(+), 43 deletions(-) create mode 100644 src/api_setup.rs create mode 100644 src/cache.rs diff --git a/src/api_setup.rs b/src/api_setup.rs new file mode 100644 index 0000000..bddc730 --- /dev/null +++ b/src/api_setup.rs @@ -0,0 +1,27 @@ +use reqwest_middleware::ClientBuilder; +use riven::{RiotApi, RiotApiConfig}; + +use std::error::Error; + +use crate::cache::LoggingMiddleware; + +pub(crate) fn fetch_api() -> Result> { + dotenvy::dotenv()?; + + let rg_api_key: String = std::env::var("RG_API_KEY")?; + let mut default_headers = reqwest::header::HeaderMap::new(); + default_headers.insert( + RiotApiConfig::RIOT_KEY_HEADER, + reqwest::header::HeaderValue::from_bytes(rg_api_key.as_ref()).unwrap(), + ); + + let c_builder = ClientBuilder::new( + reqwest::ClientBuilder::new() + .default_headers(default_headers) + .build()?, + ) + .with(LoggingMiddleware); + let config = RiotApiConfig::with_client_builder(c_builder); + + Ok(RiotApi::new(config)) +} diff --git a/src/cache.rs b/src/cache.rs new file mode 100644 index 0000000..11d919a --- /dev/null +++ b/src/cache.rs @@ -0,0 +1,20 @@ +use reqwest::{Request, Response}; +use reqwest_middleware::{Middleware, Next, Result}; +use task_local_extensions::Extensions; + +pub(crate) struct LoggingMiddleware; + +#[async_trait::async_trait] +impl Middleware for LoggingMiddleware { + async fn handle( + &self, + req: Request, + extensions: &mut Extensions, + next: Next<'_>, + ) -> Result { + println!("Request started {:#?}", req); + let res = next.run(req, extensions).await; + println!("Result: {:#?}", res); + res + } +} diff --git a/src/main.rs b/src/main.rs index 56fef9e..059d32d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,30 +1,11 @@ use std::error::Error; -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 - } -} +mod api_setup; +mod cache; #[tokio::main] async fn main() -> Result<(), Box> { - let r_api = fetch_api()?; + let r_api = api_setup::fetch_api()?; let summoner = r_api .summoner_v4() @@ -35,24 +16,3 @@ async fn main() -> Result<(), Box> { Ok(()) } - -fn fetch_api() -> Result> { - dotenvy::dotenv()?; - - let rg_api_key: String = std::env::var("RG_API_KEY")?; - let mut default_headers = reqwest::header::HeaderMap::new(); - default_headers.insert( - RiotApiConfig::RIOT_KEY_HEADER, - reqwest::header::HeaderValue::from_bytes(rg_api_key.as_ref()).unwrap(), - ); - - let c_builder = ClientBuilder::new( - reqwest::ClientBuilder::new() - .default_headers(default_headers) - .build()?, - ) - .with(LoggingMiddleware); - let config = RiotApiConfig::with_client_builder(c_builder); - - Ok(RiotApi::new(config)) -}