moving things to modules
This commit is contained in:
parent
2d1aad888d
commit
8a79e44d3d
3 changed files with 50 additions and 43 deletions
27
src/api_setup.rs
Normal file
27
src/api_setup.rs
Normal file
|
@ -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<RiotApi, Box<dyn Error>> {
|
||||||
|
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))
|
||||||
|
}
|
20
src/cache.rs
Normal file
20
src/cache.rs
Normal file
|
@ -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<Response> {
|
||||||
|
println!("Request started {:#?}", req);
|
||||||
|
let res = next.run(req, extensions).await;
|
||||||
|
println!("Result: {:#?}", res);
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
46
src/main.rs
46
src/main.rs
|
@ -1,30 +1,11 @@
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
use reqwest::Response;
|
mod api_setup;
|
||||||
use reqwest_middleware::{ClientBuilder, Middleware, Next};
|
mod cache;
|
||||||
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>> {
|
||||||
let r_api = fetch_api()?;
|
let r_api = api_setup::fetch_api()?;
|
||||||
|
|
||||||
let summoner = r_api
|
let summoner = r_api
|
||||||
.summoner_v4()
|
.summoner_v4()
|
||||||
|
@ -35,24 +16,3 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fetch_api() -> Result<RiotApi, Box<dyn Error>> {
|
|
||||||
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))
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue