From 9d7eb9f875bfc923b569003f9e19d8416dc53707 Mon Sep 17 00:00:00 2001 From: nyyu Date: Wed, 17 Mar 2021 09:07:01 +0100 Subject: [PATCH] remove init data source --- src/cgg_data_source.rs | 2 - src/data_source.rs | 2 - src/kb_data_source.rs | 93 +++++++++++++++++++++--------------------- src/main.rs | 8 ++-- src/pb_data_source.rs | 2 - 5 files changed, 49 insertions(+), 58 deletions(-) diff --git a/src/cgg_data_source.rs b/src/cgg_data_source.rs index 7f43d39..2398047 100644 --- a/src/cgg_data_source.rs +++ b/src/cgg_data_source.rs @@ -95,8 +95,6 @@ fn extract_json(pattern: &str, page: &str) -> String { } impl DataSource for CGGDataSource { - fn init(&self, _client: &ureq::Agent) {} - fn get_alias(&self) -> &str { "CGG" } diff --git a/src/data_source.rs b/src/data_source.rs index 48a8615..ce37aa8 100644 --- a/src/data_source.rs +++ b/src/data_source.rs @@ -39,8 +39,6 @@ pub struct Stat { } pub trait DataSource { - fn init(&self, client: &ureq::Agent); - fn get_alias(&self) -> &str; fn get_timeout(&self) -> u64; diff --git a/src/kb_data_source.rs b/src/kb_data_source.rs index 4cfe5bc..bd9907d 100644 --- a/src/kb_data_source.rs +++ b/src/kb_data_source.rs @@ -1,14 +1,15 @@ use crate::data_source::{Build, DataSource, Item, Stat}; -#[cfg(test)] -use crate::time::Duration; use crate::ChampInfo; use indexmap::IndexMap; use lazy_static::lazy_static; use serde_derive::Deserialize; use serde_json::{json, Value}; -use std::sync::Mutex; +use std::time::Duration; +use crate::USER_AGENT_VALUE; -pub struct KBDataSource; +pub struct KBDataSource { + token: Option, +} #[derive(Deserialize, Debug)] struct ChampionResponse { @@ -90,38 +91,47 @@ struct Summoner { name: String, } -lazy_static! { - static ref TOKEN: Mutex> = Mutex::new(None); +// It will be better to use Result... +fn get_auth_token() -> Option { + let client = ureq::AgentBuilder::new() + .user_agent(USER_AGENT_VALUE) + .timeout(Duration::from_secs(10)) + .build(); + let mut bundle = match client.get("https://koreanbuilds.net/bundle.js").call() { + Ok(resp) => match resp.into_string() { + Ok(val) => val, + Err(_) => return None, + }, + Err(_) => return None, + }; + let auth_position = match bundle.find("Authorization") { + Some(position) => position, + None => return None, + }; + bundle = (&bundle[(auth_position + 13)..]).to_string(); + let q_position = match bundle.find('"') { + Some(position) => position, + None => return None, + }; + bundle = (&bundle[(q_position + 1)..]).to_string(); + match bundle.find('"') { + Some(position) => Some((&bundle[..position]).to_string()), + None => None, + } } impl KBDataSource { - // It will be better to use Result... - fn get_auth_token(&self, client: &ureq::Agent) -> Option { - let mut bundle = match client.get("https://koreanbuilds.net/bundle.js").call() { - Ok(resp) => match resp.into_string() { - Ok(val) => val, - Err(_) => return None, - }, - Err(_) => return None, - }; - let auth_position = match bundle.find("Authorization") { - Some(position) => position, - None => return None, - }; - bundle = (&bundle[(auth_position + 13)..]).to_string(); - let q_position = match bundle.find('"') { - Some(position) => position, - None => return None, - }; - bundle = (&bundle[(q_position + 1)..]).to_string(); - match bundle.find('"') { - Some(position) => Some((&bundle[..position]).to_string()), - None => None, + pub fn new() -> &'static KBDataSource { + lazy_static! { + static ref DATASOURCE: KBDataSource = KBDataSource { + token: get_auth_token(), + }; } + &DATASOURCE } fn get_champion_response(&self, client: &ureq::Agent) -> Option { - if let Some(token) = TOKEN.lock().unwrap().as_ref() { + if let Some(token) = &self.token { return match client .get("https://api.koreanbuilds.net/champions?patchid=-1") .set("Accept", "application/json") @@ -278,12 +288,6 @@ impl KBDataSource { } impl DataSource for KBDataSource { - fn init(&self, client: &ureq::Agent) { - if let Some(t) = self.get_auth_token(client) { - TOKEN.lock().unwrap().replace(t); - } - } - fn get_alias(&self) -> &str { "KB" } @@ -313,7 +317,7 @@ impl DataSource for KBDataSource { client: &ureq::Agent, ) -> Vec<(String, Vec, Stat)> { let mut champ_data = vec![]; - if let Some(token) = TOKEN.lock().unwrap().as_ref() { + if let Some(token) = &self.token { let data: BuildResponse = match client .get(&format!( "https://api.koreanbuilds.net/builds?chmpname={}&patchid=-2&position=COMPOSITE", @@ -359,11 +363,7 @@ mod tests { #[test] fn test_get_auth_token() { - let datasource = KBDataSource; - let client = ureq::AgentBuilder::new() - .timeout(Duration::from_secs(10)) - .build(); - match datasource.get_auth_token(&client) { + match get_auth_token() { Some(token) => assert!(token.len() > 0), None => assert!(false), }; @@ -372,22 +372,21 @@ mod tests { #[test] fn test_get_champs_with_positions_and_patch() { let client = ureq::AgentBuilder::new() + .user_agent(USER_AGENT_VALUE) .timeout(Duration::from_secs(10)) .build(); - let datasource = KBDataSource; - datasource.init(&client); - let champs_with_positions = - datasource.get_champs_with_positions(&client); + let datasource = KBDataSource::new(); + let champs_with_positions = datasource.get_champs_with_positions(&client); assert!(champs_with_positions.len() > 0); } #[test] fn test_get_champ_data_with_win_pourcentage() { let client = ureq::AgentBuilder::new() + .user_agent(USER_AGENT_VALUE) .timeout(Duration::from_secs(10)) .build(); - let datasource = KBDataSource; - datasource.init(&client); + let datasource = KBDataSource::new(); let champ = ChampInfo { id: String::from("Annie"), name: String::from("Annie"), diff --git a/src/main.rs b/src/main.rs index e5f82a7..0156b28 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,12 +100,12 @@ fn main() -> Result<(), Box> { .into_json()?; info!("LoL numbers of champs: {}", champion.data.len()); - static DATA_SOURCES: [&'static (dyn DataSource + Sync + Send); 3] = [ + let data_sources: [&'static (dyn DataSource + Sync + Send); 3] = [ &PBDataSource, &CGGDataSource, - &KBDataSource, + KBDataSource::new(), ]; - DATA_SOURCES.par_iter().for_each(|data_source| { + data_sources.par_iter().for_each(|data_source| { let init = Instant::now(); execute_data_source(*data_source, &client, &champion, &lol_champs_dir); @@ -133,8 +133,6 @@ fn execute_data_source( champion: &Champion, lol_champs_dir: &PathBuf, ) { - data_source.init(client); - let champs = data_source.get_champs_with_positions(&client); info!( diff --git a/src/pb_data_source.rs b/src/pb_data_source.rs index a8893d7..8e2c639 100644 --- a/src/pb_data_source.rs +++ b/src/pb_data_source.rs @@ -19,8 +19,6 @@ struct Champion { } impl DataSource for PBDataSource { - fn init(&self, _client: &ureq::Agent) {} - fn get_alias(&self) -> &str { "PB" }