remove init data source
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
a29054520d
commit
9d7eb9f875
5 changed files with 49 additions and 58 deletions
|
@ -95,8 +95,6 @@ fn extract_json(pattern: &str, page: &str) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DataSource for CGGDataSource {
|
impl DataSource for CGGDataSource {
|
||||||
fn init(&self, _client: &ureq::Agent) {}
|
|
||||||
|
|
||||||
fn get_alias(&self) -> &str {
|
fn get_alias(&self) -> &str {
|
||||||
"CGG"
|
"CGG"
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,8 +39,6 @@ pub struct Stat {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait DataSource {
|
pub trait DataSource {
|
||||||
fn init(&self, client: &ureq::Agent);
|
|
||||||
|
|
||||||
fn get_alias(&self) -> &str;
|
fn get_alias(&self) -> &str;
|
||||||
|
|
||||||
fn get_timeout(&self) -> u64;
|
fn get_timeout(&self) -> u64;
|
||||||
|
|
|
@ -1,14 +1,15 @@
|
||||||
use crate::data_source::{Build, DataSource, Item, Stat};
|
use crate::data_source::{Build, DataSource, Item, Stat};
|
||||||
#[cfg(test)]
|
|
||||||
use crate::time::Duration;
|
|
||||||
use crate::ChampInfo;
|
use crate::ChampInfo;
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
use serde_json::{json, Value};
|
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<String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct ChampionResponse {
|
struct ChampionResponse {
|
||||||
|
@ -90,13 +91,12 @@ struct Summoner {
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref TOKEN: Mutex<Option<String>> = Mutex::new(None);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl KBDataSource {
|
|
||||||
// It will be better to use Result...
|
// It will be better to use Result...
|
||||||
fn get_auth_token(&self, client: &ureq::Agent) -> Option<String> {
|
fn get_auth_token() -> Option<String> {
|
||||||
|
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() {
|
let mut bundle = match client.get("https://koreanbuilds.net/bundle.js").call() {
|
||||||
Ok(resp) => match resp.into_string() {
|
Ok(resp) => match resp.into_string() {
|
||||||
Ok(val) => val,
|
Ok(val) => val,
|
||||||
|
@ -120,8 +120,18 @@ impl KBDataSource {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl KBDataSource {
|
||||||
|
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<ChampionResponse> {
|
fn get_champion_response(&self, client: &ureq::Agent) -> Option<ChampionResponse> {
|
||||||
if let Some(token) = TOKEN.lock().unwrap().as_ref() {
|
if let Some(token) = &self.token {
|
||||||
return match client
|
return match client
|
||||||
.get("https://api.koreanbuilds.net/champions?patchid=-1")
|
.get("https://api.koreanbuilds.net/champions?patchid=-1")
|
||||||
.set("Accept", "application/json")
|
.set("Accept", "application/json")
|
||||||
|
@ -278,12 +288,6 @@ impl KBDataSource {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DataSource for 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 {
|
fn get_alias(&self) -> &str {
|
||||||
"KB"
|
"KB"
|
||||||
}
|
}
|
||||||
|
@ -313,7 +317,7 @@ impl DataSource for KBDataSource {
|
||||||
client: &ureq::Agent,
|
client: &ureq::Agent,
|
||||||
) -> Vec<(String, Vec<Value>, Stat)> {
|
) -> Vec<(String, Vec<Value>, Stat)> {
|
||||||
let mut champ_data = vec![];
|
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
|
let data: BuildResponse = match client
|
||||||
.get(&format!(
|
.get(&format!(
|
||||||
"https://api.koreanbuilds.net/builds?chmpname={}&patchid=-2&position=COMPOSITE",
|
"https://api.koreanbuilds.net/builds?chmpname={}&patchid=-2&position=COMPOSITE",
|
||||||
|
@ -359,11 +363,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_auth_token() {
|
fn test_get_auth_token() {
|
||||||
let datasource = KBDataSource;
|
match get_auth_token() {
|
||||||
let client = ureq::AgentBuilder::new()
|
|
||||||
.timeout(Duration::from_secs(10))
|
|
||||||
.build();
|
|
||||||
match datasource.get_auth_token(&client) {
|
|
||||||
Some(token) => assert!(token.len() > 0),
|
Some(token) => assert!(token.len() > 0),
|
||||||
None => assert!(false),
|
None => assert!(false),
|
||||||
};
|
};
|
||||||
|
@ -372,22 +372,21 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_champs_with_positions_and_patch() {
|
fn test_get_champs_with_positions_and_patch() {
|
||||||
let client = ureq::AgentBuilder::new()
|
let client = ureq::AgentBuilder::new()
|
||||||
|
.user_agent(USER_AGENT_VALUE)
|
||||||
.timeout(Duration::from_secs(10))
|
.timeout(Duration::from_secs(10))
|
||||||
.build();
|
.build();
|
||||||
let datasource = KBDataSource;
|
let datasource = KBDataSource::new();
|
||||||
datasource.init(&client);
|
let champs_with_positions = datasource.get_champs_with_positions(&client);
|
||||||
let champs_with_positions =
|
|
||||||
datasource.get_champs_with_positions(&client);
|
|
||||||
assert!(champs_with_positions.len() > 0);
|
assert!(champs_with_positions.len() > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_champ_data_with_win_pourcentage() {
|
fn test_get_champ_data_with_win_pourcentage() {
|
||||||
let client = ureq::AgentBuilder::new()
|
let client = ureq::AgentBuilder::new()
|
||||||
|
.user_agent(USER_AGENT_VALUE)
|
||||||
.timeout(Duration::from_secs(10))
|
.timeout(Duration::from_secs(10))
|
||||||
.build();
|
.build();
|
||||||
let datasource = KBDataSource;
|
let datasource = KBDataSource::new();
|
||||||
datasource.init(&client);
|
|
||||||
let champ = ChampInfo {
|
let champ = ChampInfo {
|
||||||
id: String::from("Annie"),
|
id: String::from("Annie"),
|
||||||
name: String::from("Annie"),
|
name: String::from("Annie"),
|
||||||
|
|
|
@ -100,12 +100,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
.into_json()?;
|
.into_json()?;
|
||||||
info!("LoL numbers of champs: {}", champion.data.len());
|
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,
|
&PBDataSource,
|
||||||
&CGGDataSource,
|
&CGGDataSource,
|
||||||
&KBDataSource,
|
KBDataSource::new(),
|
||||||
];
|
];
|
||||||
DATA_SOURCES.par_iter().for_each(|data_source| {
|
data_sources.par_iter().for_each(|data_source| {
|
||||||
|
|
||||||
let init = Instant::now();
|
let init = Instant::now();
|
||||||
execute_data_source(*data_source, &client, &champion, &lol_champs_dir);
|
execute_data_source(*data_source, &client, &champion, &lol_champs_dir);
|
||||||
|
@ -133,8 +133,6 @@ fn execute_data_source(
|
||||||
champion: &Champion,
|
champion: &Champion,
|
||||||
lol_champs_dir: &PathBuf,
|
lol_champs_dir: &PathBuf,
|
||||||
) {
|
) {
|
||||||
data_source.init(client);
|
|
||||||
|
|
||||||
let champs = data_source.get_champs_with_positions(&client);
|
let champs = data_source.get_champs_with_positions(&client);
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
|
|
|
@ -19,8 +19,6 @@ struct Champion {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DataSource for PBDataSource {
|
impl DataSource for PBDataSource {
|
||||||
fn init(&self, _client: &ureq::Agent) {}
|
|
||||||
|
|
||||||
fn get_alias(&self) -> &str {
|
fn get_alias(&self) -> &str {
|
||||||
"PB"
|
"PB"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue