Change region to be an enum

pull/5/head
Mingwei Samuel 2019-11-04 17:00:59 -08:00
parent 3d0b42e31c
commit 914ce35259
8 changed files with 93 additions and 117 deletions

View File

@ -23,7 +23,7 @@ Riven currently uses nightly Rust.
use riven::RiotApi; use riven::RiotApi;
use riven::consts::Region; use riven::consts::Region;
// Enter tokio async runtime. // Riven Enter tokio async runtime.
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { rt.block_on(async {
// Create RiotApi instance from key. // Create RiotApi instance from key.

View File

@ -1,64 +1,43 @@
use strum_macros::{ EnumString, Display, AsRefStr, IntoStaticStr };
/// A region served by a single game server. /// A region served by a single game server.
/// Each Riot Games API request is directed at a particular region, /// Each Riot Games API request is directed at a particular region,
/// with tournament API requests directed at the AMERICAS "global" region. /// with tournament API requests directed at the AMERICAS "global" region.
#[derive(Debug)] #[derive(Debug)]
#[derive(PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(EnumString, Display, AsRefStr, IntoStaticStr)]
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Region { pub enum Region {
pub key: &'static str, #[strum(to_string="BR1", serialize="BR")]
pub platform: &'static str, BR,
} #[strum(to_string="EUN1", serialize="EUNE")]
EUNE,
macro_rules! regions { #[strum(to_string="EUW1", serialize="EUW")]
( EUW,
$( #[strum(to_string="NA1", serialize="NA")]
$key:ident => $plat:expr ; NA,
)* #[strum(to_string="KR", serialize="KR")]
) => { KR,
$( #[strum(to_string="LA1", serialize="LAN")]
pub const $key: Region = Region { LAN,
key: stringify!($key), #[strum(to_string="LA2", serialize="LAS")]
platform: $plat, LAS,
}; #[strum(to_string="OC1", serialize="OCE")]
)* OCE,
#[strum(to_string="RU", serialize="RU")]
#[doc="Get region by name."] RU,
#[doc="# Arguments"] #[strum(to_string="TR1", serialize="TR")]
#[doc="* `name` - Case-insensitive ASCII string to match Regions' `key` or `playform`."] TR,
#[doc="# Returns"] #[strum(to_string="JP1", serialize="JP")]
#[doc="`Some(&Region)` if match found, `None` if no match found."] JP,
#[allow(unreachable_patterns)] #[strum(to_string="PBE1", serialize="PBE")]
pub fn get(name: &str) -> Option<Region> { PBE,
match &*name.to_ascii_uppercase() { #[strum(to_string="AMERICAS", serialize="AMERICAS")]
$( AMERICAS,
stringify!($key) | $plat => Some(Self::$key), #[strum(to_string="EUROPE", serialize="EUROPE")]
)* EUROPE,
_ => None #[strum(to_string="ASIA", serialize="ASIA")]
} ASIA,
}
}
}
impl Region {
// Is this stupid?
regions! {
BR => "BR1";
EUNE => "EUN1";
EUW => "EUW1";
NA => "NA1";
KR => "KR";
LAN => "LA1";
LAS => "LA2";
OCE => "OC1";
RU => "RU";
TR => "TR1";
JP => "JP1";
PBE => "PBE1";
AMERICAS => "AMERICAS";
EUROPE => "EUROPE";
ASIA => "ASIA";
}
} }
#[cfg(test)] #[cfg(test)]
@ -67,13 +46,13 @@ mod tests {
#[test] #[test]
fn test_basic() { fn test_basic() {
assert_eq!("BR1", Region::BR.platform); assert_eq!("BR1", Region::BR.to_string());
} }
#[test] #[test]
fn test_get() { fn test_get() {
assert_eq!(Some(Region::AMERICAS), Region::get("amEricAs")); assert_eq!(Ok(Region::JP), "JP".parse());
assert_eq!(Some(Region::NA), Region::get("na1")); assert_eq!(Ok(Region::NA), "NA1".parse());
assert_eq!(None, Region::get("LA")); assert!("LA".parse::<Region>().is_err());
} }
} }

View File

@ -140,7 +140,7 @@ impl<'a> ChampionMasteryV4<'a> {
-> impl Future<Output = Result<Option<Vec<champion_mastery_v4::ChampionMastery>>>> + 'a -> impl Future<Output = Result<Option<Vec<champion_mastery_v4::ChampionMastery>>>> + 'a
{ {
let path_string = format!("/lol/champion-mastery/v4/champion-masteries/by-summoner/{}", encrypted_summoner_id); let path_string = format!("/lol/champion-mastery/v4/champion-masteries/by-summoner/{}", encrypted_summoner_id);
self.base.get::<Vec<champion_mastery_v4::ChampionMastery>>("champion-mastery-v4.getAllChampionMasteries", region, path_string, None) self.base.get::<Vec<champion_mastery_v4::ChampionMastery>>("champion-mastery-v4.getAllChampionMasteries", region.into(), path_string, None)
} }
/// Get a champion mastery by player ID and champion ID. /// Get a champion mastery by player ID and champion ID.
@ -154,7 +154,7 @@ impl<'a> ChampionMasteryV4<'a> {
-> impl Future<Output = Result<Option<champion_mastery_v4::ChampionMastery>>> + 'a -> impl Future<Output = Result<Option<champion_mastery_v4::ChampionMastery>>> + 'a
{ {
let path_string = format!("/lol/champion-mastery/v4/champion-masteries/by-summoner/{}/by-champion/{}", encrypted_summoner_id, champion_id); let path_string = format!("/lol/champion-mastery/v4/champion-masteries/by-summoner/{}/by-champion/{}", encrypted_summoner_id, champion_id);
self.base.get::<champion_mastery_v4::ChampionMastery>("champion-mastery-v4.getChampionMastery", region, path_string, None) self.base.get::<champion_mastery_v4::ChampionMastery>("champion-mastery-v4.getChampionMastery", region.into(), path_string, None)
} }
/// Get a player's total champion mastery score, which is the sum of individual champion mastery levels. /// Get a player's total champion mastery score, which is the sum of individual champion mastery levels.
@ -167,7 +167,7 @@ impl<'a> ChampionMasteryV4<'a> {
-> impl Future<Output = Result<Option<i32>>> + 'a -> impl Future<Output = Result<Option<i32>>> + 'a
{ {
let path_string = format!("/lol/champion-mastery/v4/scores/by-summoner/{}", encrypted_summoner_id); let path_string = format!("/lol/champion-mastery/v4/scores/by-summoner/{}", encrypted_summoner_id);
self.base.get::<i32>("champion-mastery-v4.getChampionMasteryScore", region, path_string, None) self.base.get::<i32>("champion-mastery-v4.getChampionMasteryScore", region.into(), path_string, None)
} }
} }
@ -188,7 +188,7 @@ impl<'a> ChampionV3<'a> {
-> impl Future<Output = Result<Option<champion_v3::ChampionInfo>>> + 'a -> impl Future<Output = Result<Option<champion_v3::ChampionInfo>>> + 'a
{ {
let path_string = "/lol/platform/v3/champion-rotations".to_owned(); let path_string = "/lol/platform/v3/champion-rotations".to_owned();
self.base.get::<champion_v3::ChampionInfo>("champion-v3.getChampionInfo", region, path_string, None) self.base.get::<champion_v3::ChampionInfo>("champion-v3.getChampionInfo", region.into(), path_string, None)
} }
} }
@ -216,7 +216,7 @@ impl<'a> LeagueExpV4<'a> {
if let Some(page) = page { query_params.append_pair("page", &*page.to_string()); }; if let Some(page) = page { query_params.append_pair("page", &*page.to_string()); };
let query_string = query_params.finish(); let query_string = query_params.finish();
let path_string = format!("/lol/league-exp/v4/entries/{}/{}/{}", queue, tier, division); let path_string = format!("/lol/league-exp/v4/entries/{}/{}/{}", queue, tier, division);
self.base.get::<Vec<league_exp_v4::LeagueEntry>>("league-exp-v4.getLeagueEntries", region, path_string, Some(query_string)) self.base.get::<Vec<league_exp_v4::LeagueEntry>>("league-exp-v4.getLeagueEntries", region.into(), path_string, Some(query_string))
} }
} }
@ -238,7 +238,7 @@ impl<'a> LeagueV4<'a> {
-> impl Future<Output = Result<Option<league_v4::LeagueList>>> + 'a -> impl Future<Output = Result<Option<league_v4::LeagueList>>> + 'a
{ {
let path_string = format!("/lol/league/v4/challengerleagues/by-queue/{}", queue); let path_string = format!("/lol/league/v4/challengerleagues/by-queue/{}", queue);
self.base.get::<league_v4::LeagueList>("league-v4.getChallengerLeague", region, path_string, None) self.base.get::<league_v4::LeagueList>("league-v4.getChallengerLeague", region.into(), path_string, None)
} }
/// Get league entries in all queues for a given summoner ID. /// Get league entries in all queues for a given summoner ID.
@ -251,7 +251,7 @@ impl<'a> LeagueV4<'a> {
-> impl Future<Output = Result<Option<Vec<league_v4::LeagueEntry>>>> + 'a -> impl Future<Output = Result<Option<Vec<league_v4::LeagueEntry>>>> + 'a
{ {
let path_string = format!("/lol/league/v4/entries/by-summoner/{}", encrypted_summoner_id); let path_string = format!("/lol/league/v4/entries/by-summoner/{}", encrypted_summoner_id);
self.base.get::<Vec<league_v4::LeagueEntry>>("league-v4.getLeagueEntriesForSummoner", region, path_string, None) self.base.get::<Vec<league_v4::LeagueEntry>>("league-v4.getLeagueEntriesForSummoner", region.into(), path_string, None)
} }
/// Get all the league entries. /// Get all the league entries.
@ -270,7 +270,7 @@ impl<'a> LeagueV4<'a> {
if let Some(page) = page { query_params.append_pair("page", &*page.to_string()); }; if let Some(page) = page { query_params.append_pair("page", &*page.to_string()); };
let query_string = query_params.finish(); let query_string = query_params.finish();
let path_string = format!("/lol/league/v4/entries/{}/{}/{}", queue, tier, division); let path_string = format!("/lol/league/v4/entries/{}/{}/{}", queue, tier, division);
self.base.get::<Vec<league_v4::LeagueEntry>>("league-v4.getLeagueEntries", region, path_string, Some(query_string)) self.base.get::<Vec<league_v4::LeagueEntry>>("league-v4.getLeagueEntries", region.into(), path_string, Some(query_string))
} }
/// Get the grandmaster league of a specific queue. /// Get the grandmaster league of a specific queue.
@ -283,7 +283,7 @@ impl<'a> LeagueV4<'a> {
-> impl Future<Output = Result<Option<league_v4::LeagueList>>> + 'a -> impl Future<Output = Result<Option<league_v4::LeagueList>>> + 'a
{ {
let path_string = format!("/lol/league/v4/grandmasterleagues/by-queue/{}", queue); let path_string = format!("/lol/league/v4/grandmasterleagues/by-queue/{}", queue);
self.base.get::<league_v4::LeagueList>("league-v4.getGrandmasterLeague", region, path_string, None) self.base.get::<league_v4::LeagueList>("league-v4.getGrandmasterLeague", region.into(), path_string, None)
} }
/// Get league with given ID, including inactive entries. /// Get league with given ID, including inactive entries.
@ -296,7 +296,7 @@ impl<'a> LeagueV4<'a> {
-> impl Future<Output = Result<Option<league_v4::LeagueList>>> + 'a -> impl Future<Output = Result<Option<league_v4::LeagueList>>> + 'a
{ {
let path_string = format!("/lol/league/v4/leagues/{}", league_id); let path_string = format!("/lol/league/v4/leagues/{}", league_id);
self.base.get::<league_v4::LeagueList>("league-v4.getLeagueById", region, path_string, None) self.base.get::<league_v4::LeagueList>("league-v4.getLeagueById", region.into(), path_string, None)
} }
/// Get the master league for given queue. /// Get the master league for given queue.
@ -309,7 +309,7 @@ impl<'a> LeagueV4<'a> {
-> impl Future<Output = Result<Option<league_v4::LeagueList>>> + 'a -> impl Future<Output = Result<Option<league_v4::LeagueList>>> + 'a
{ {
let path_string = format!("/lol/league/v4/masterleagues/by-queue/{}", queue); let path_string = format!("/lol/league/v4/masterleagues/by-queue/{}", queue);
self.base.get::<league_v4::LeagueList>("league-v4.getMasterLeague", region, path_string, None) self.base.get::<league_v4::LeagueList>("league-v4.getMasterLeague", region.into(), path_string, None)
} }
} }
@ -332,7 +332,7 @@ impl<'a> LolStatusV3<'a> {
-> impl Future<Output = Result<Option<lol_status_v3::ShardStatus>>> + 'a -> impl Future<Output = Result<Option<lol_status_v3::ShardStatus>>> + 'a
{ {
let path_string = "/lol/status/v3/shard-data".to_owned(); let path_string = "/lol/status/v3/shard-data".to_owned();
self.base.get::<lol_status_v3::ShardStatus>("lol-status-v3.getShardData", region, path_string, None) self.base.get::<lol_status_v3::ShardStatus>("lol-status-v3.getShardData", region.into(), path_string, None)
} }
} }
@ -354,7 +354,7 @@ impl<'a> MatchV4<'a> {
-> impl Future<Output = Result<Option<Vec<i64>>>> + 'a -> impl Future<Output = Result<Option<Vec<i64>>>> + 'a
{ {
let path_string = format!("/lol/match/v4/matches/by-tournament-code/{}/ids", tournament_code); let path_string = format!("/lol/match/v4/matches/by-tournament-code/{}/ids", tournament_code);
self.base.get::<Vec<i64>>("match-v4.getMatchIdsByTournamentCode", region, path_string, None) self.base.get::<Vec<i64>>("match-v4.getMatchIdsByTournamentCode", region.into(), path_string, None)
} }
/// Get match by match ID. /// Get match by match ID.
@ -367,7 +367,7 @@ impl<'a> MatchV4<'a> {
-> impl Future<Output = Result<Option<match_v4::Match>>> + 'a -> impl Future<Output = Result<Option<match_v4::Match>>> + 'a
{ {
let path_string = format!("/lol/match/v4/matches/{}", match_id); let path_string = format!("/lol/match/v4/matches/{}", match_id);
self.base.get::<match_v4::Match>("match-v4.getMatch", region, path_string, None) self.base.get::<match_v4::Match>("match-v4.getMatch", region.into(), path_string, None)
} }
/// Get match by match ID and tournament code. /// Get match by match ID and tournament code.
@ -381,7 +381,7 @@ impl<'a> MatchV4<'a> {
-> impl Future<Output = Result<Option<match_v4::Match>>> + 'a -> impl Future<Output = Result<Option<match_v4::Match>>> + 'a
{ {
let path_string = format!("/lol/match/v4/matches/{}/by-tournament-code/{}", match_id, tournament_code); let path_string = format!("/lol/match/v4/matches/{}/by-tournament-code/{}", match_id, tournament_code);
self.base.get::<match_v4::Match>("match-v4.getMatchByTournamentCode", region, path_string, None) self.base.get::<match_v4::Match>("match-v4.getMatchByTournamentCode", region.into(), path_string, None)
} }
/// Get matchlist for games played on given account ID and platform ID and filtered using given filter parameters, if any. /// Get matchlist for games played on given account ID and platform ID and filtered using given filter parameters, if any.
@ -416,7 +416,7 @@ impl<'a> MatchV4<'a> {
if let Some(season) = season { query_params.extend_pairs(season.iter().map(|w| ("season", Into::<u8>::into(*w).to_string()))); }; if let Some(season) = season { query_params.extend_pairs(season.iter().map(|w| ("season", Into::<u8>::into(*w).to_string()))); };
let query_string = query_params.finish(); let query_string = query_params.finish();
let path_string = format!("/lol/match/v4/matchlists/by-account/{}", encrypted_account_id); let path_string = format!("/lol/match/v4/matchlists/by-account/{}", encrypted_account_id);
self.base.get::<match_v4::Matchlist>("match-v4.getMatchlist", region, path_string, Some(query_string)) self.base.get::<match_v4::Matchlist>("match-v4.getMatchlist", region.into(), path_string, Some(query_string))
} }
/// Get match timeline by match ID. /// Get match timeline by match ID.
@ -431,7 +431,7 @@ impl<'a> MatchV4<'a> {
-> impl Future<Output = Result<Option<match_v4::MatchTimeline>>> + 'a -> impl Future<Output = Result<Option<match_v4::MatchTimeline>>> + 'a
{ {
let path_string = format!("/lol/match/v4/timelines/by-match/{}", match_id); let path_string = format!("/lol/match/v4/timelines/by-match/{}", match_id);
self.base.get::<match_v4::MatchTimeline>("match-v4.getMatchTimeline", region, path_string, None) self.base.get::<match_v4::MatchTimeline>("match-v4.getMatchTimeline", region.into(), path_string, None)
} }
} }
@ -453,7 +453,7 @@ impl<'a> SpectatorV4<'a> {
-> impl Future<Output = Result<Option<spectator_v4::CurrentGameInfo>>> + 'a -> impl Future<Output = Result<Option<spectator_v4::CurrentGameInfo>>> + 'a
{ {
let path_string = format!("/lol/spectator/v4/active-games/by-summoner/{}", encrypted_summoner_id); let path_string = format!("/lol/spectator/v4/active-games/by-summoner/{}", encrypted_summoner_id);
self.base.get::<spectator_v4::CurrentGameInfo>("spectator-v4.getCurrentGameInfoBySummoner", region, path_string, None) self.base.get::<spectator_v4::CurrentGameInfo>("spectator-v4.getCurrentGameInfoBySummoner", region.into(), path_string, None)
} }
/// Get list of featured games. /// Get list of featured games.
@ -465,7 +465,7 @@ impl<'a> SpectatorV4<'a> {
-> impl Future<Output = Result<Option<spectator_v4::FeaturedGames>>> + 'a -> impl Future<Output = Result<Option<spectator_v4::FeaturedGames>>> + 'a
{ {
let path_string = "/lol/spectator/v4/featured-games".to_owned(); let path_string = "/lol/spectator/v4/featured-games".to_owned();
self.base.get::<spectator_v4::FeaturedGames>("spectator-v4.getFeaturedGames", region, path_string, None) self.base.get::<spectator_v4::FeaturedGames>("spectator-v4.getFeaturedGames", region.into(), path_string, None)
} }
} }
@ -487,7 +487,7 @@ impl<'a> SummonerV4<'a> {
-> impl Future<Output = Result<Option<summoner_v4::Summoner>>> + 'a -> impl Future<Output = Result<Option<summoner_v4::Summoner>>> + 'a
{ {
let path_string = format!("/lol/summoner/v4/summoners/by-account/{}", encrypted_account_id); let path_string = format!("/lol/summoner/v4/summoners/by-account/{}", encrypted_account_id);
self.base.get::<summoner_v4::Summoner>("summoner-v4.getByAccountId", region, path_string, None) self.base.get::<summoner_v4::Summoner>("summoner-v4.getByAccountId", region.into(), path_string, None)
} }
/// Get a summoner by summoner name. /// Get a summoner by summoner name.
@ -500,7 +500,7 @@ impl<'a> SummonerV4<'a> {
-> impl Future<Output = Result<Option<summoner_v4::Summoner>>> + 'a -> impl Future<Output = Result<Option<summoner_v4::Summoner>>> + 'a
{ {
let path_string = format!("/lol/summoner/v4/summoners/by-name/{}", summoner_name); let path_string = format!("/lol/summoner/v4/summoners/by-name/{}", summoner_name);
self.base.get::<summoner_v4::Summoner>("summoner-v4.getBySummonerName", region, path_string, None) self.base.get::<summoner_v4::Summoner>("summoner-v4.getBySummonerName", region.into(), path_string, None)
} }
/// Get a summoner by PUUID. /// Get a summoner by PUUID.
@ -513,7 +513,7 @@ impl<'a> SummonerV4<'a> {
-> impl Future<Output = Result<Option<summoner_v4::Summoner>>> + 'a -> impl Future<Output = Result<Option<summoner_v4::Summoner>>> + 'a
{ {
let path_string = format!("/lol/summoner/v4/summoners/by-puuid/{}", encrypted_puuid); let path_string = format!("/lol/summoner/v4/summoners/by-puuid/{}", encrypted_puuid);
self.base.get::<summoner_v4::Summoner>("summoner-v4.getByPUUID", region, path_string, None) self.base.get::<summoner_v4::Summoner>("summoner-v4.getByPUUID", region.into(), path_string, None)
} }
/// Get a summoner by summoner ID. /// Get a summoner by summoner ID.
@ -526,7 +526,7 @@ impl<'a> SummonerV4<'a> {
-> impl Future<Output = Result<Option<summoner_v4::Summoner>>> + 'a -> impl Future<Output = Result<Option<summoner_v4::Summoner>>> + 'a
{ {
let path_string = format!("/lol/summoner/v4/summoners/{}", encrypted_summoner_id); let path_string = format!("/lol/summoner/v4/summoners/{}", encrypted_summoner_id);
self.base.get::<summoner_v4::Summoner>("summoner-v4.getBySummonerId", region, path_string, None) self.base.get::<summoner_v4::Summoner>("summoner-v4.getBySummonerId", region.into(), path_string, None)
} }
} }
@ -547,7 +547,7 @@ impl<'a> TftLeagueV1<'a> {
-> impl Future<Output = Result<Option<tft_league_v1::LeagueList>>> + 'a -> impl Future<Output = Result<Option<tft_league_v1::LeagueList>>> + 'a
{ {
let path_string = "/tft/league/v1/challenger".to_owned(); let path_string = "/tft/league/v1/challenger".to_owned();
self.base.get::<tft_league_v1::LeagueList>("tft-league-v1.getChallengerLeague", region, path_string, None) self.base.get::<tft_league_v1::LeagueList>("tft-league-v1.getChallengerLeague", region.into(), path_string, None)
} }
/// Get league entries for a given summoner ID. /// Get league entries for a given summoner ID.
@ -560,7 +560,7 @@ impl<'a> TftLeagueV1<'a> {
-> impl Future<Output = Result<Option<Vec<tft_league_v1::LeagueEntry>>>> + 'a -> impl Future<Output = Result<Option<Vec<tft_league_v1::LeagueEntry>>>> + 'a
{ {
let path_string = format!("/tft/league/v1/entries/by-summoner/{}", encrypted_summoner_id); let path_string = format!("/tft/league/v1/entries/by-summoner/{}", encrypted_summoner_id);
self.base.get::<Vec<tft_league_v1::LeagueEntry>>("tft-league-v1.getLeagueEntriesForSummoner", region, path_string, None) self.base.get::<Vec<tft_league_v1::LeagueEntry>>("tft-league-v1.getLeagueEntriesForSummoner", region.into(), path_string, None)
} }
/// Get all the league entries. /// Get all the league entries.
@ -578,7 +578,7 @@ impl<'a> TftLeagueV1<'a> {
if let Some(page) = page { query_params.append_pair("page", &*page.to_string()); }; if let Some(page) = page { query_params.append_pair("page", &*page.to_string()); };
let query_string = query_params.finish(); let query_string = query_params.finish();
let path_string = format!("/tft/league/v1/entries/{}/{}", tier, division); let path_string = format!("/tft/league/v1/entries/{}/{}", tier, division);
self.base.get::<Vec<tft_league_v1::LeagueEntry>>("tft-league-v1.getLeagueEntries", region, path_string, Some(query_string)) self.base.get::<Vec<tft_league_v1::LeagueEntry>>("tft-league-v1.getLeagueEntries", region.into(), path_string, Some(query_string))
} }
/// Get the grandmaster league. /// Get the grandmaster league.
@ -590,7 +590,7 @@ impl<'a> TftLeagueV1<'a> {
-> impl Future<Output = Result<Option<tft_league_v1::LeagueList>>> + 'a -> impl Future<Output = Result<Option<tft_league_v1::LeagueList>>> + 'a
{ {
let path_string = "/tft/league/v1/grandmaster".to_owned(); let path_string = "/tft/league/v1/grandmaster".to_owned();
self.base.get::<tft_league_v1::LeagueList>("tft-league-v1.getGrandmasterLeague", region, path_string, None) self.base.get::<tft_league_v1::LeagueList>("tft-league-v1.getGrandmasterLeague", region.into(), path_string, None)
} }
/// Get league with given ID, including inactive entries. /// Get league with given ID, including inactive entries.
@ -603,7 +603,7 @@ impl<'a> TftLeagueV1<'a> {
-> impl Future<Output = Result<Option<tft_league_v1::LeagueList>>> + 'a -> impl Future<Output = Result<Option<tft_league_v1::LeagueList>>> + 'a
{ {
let path_string = format!("/tft/league/v1/leagues/{}", league_id); let path_string = format!("/tft/league/v1/leagues/{}", league_id);
self.base.get::<tft_league_v1::LeagueList>("tft-league-v1.getLeagueById", region, path_string, None) self.base.get::<tft_league_v1::LeagueList>("tft-league-v1.getLeagueById", region.into(), path_string, None)
} }
/// Get the master league. /// Get the master league.
@ -615,7 +615,7 @@ impl<'a> TftLeagueV1<'a> {
-> impl Future<Output = Result<Option<tft_league_v1::LeagueList>>> + 'a -> impl Future<Output = Result<Option<tft_league_v1::LeagueList>>> + 'a
{ {
let path_string = "/tft/league/v1/master".to_owned(); let path_string = "/tft/league/v1/master".to_owned();
self.base.get::<tft_league_v1::LeagueList>("tft-league-v1.getMasterLeague", region, path_string, None) self.base.get::<tft_league_v1::LeagueList>("tft-league-v1.getMasterLeague", region.into(), path_string, None)
} }
} }
@ -637,7 +637,7 @@ impl<'a> TftMatchV1<'a> {
-> impl Future<Output = Result<Option<Vec<String>>>> + 'a -> impl Future<Output = Result<Option<Vec<String>>>> + 'a
{ {
let path_string = format!("/tft/match/v1/matches/by-puuid/{}/ids", encrypted_puuid); let path_string = format!("/tft/match/v1/matches/by-puuid/{}/ids", encrypted_puuid);
self.base.get::<Vec<String>>("tft-match-v1.getMatchIdsByPUUID", region, path_string, None) self.base.get::<Vec<String>>("tft-match-v1.getMatchIdsByPUUID", region.into(), path_string, None)
} }
/// Get a match by match id. /// Get a match by match id.
@ -650,7 +650,7 @@ impl<'a> TftMatchV1<'a> {
-> impl Future<Output = Result<Option<tft_match_v1::Match>>> + 'a -> impl Future<Output = Result<Option<tft_match_v1::Match>>> + 'a
{ {
let path_string = format!("/tft/match/v1/matches/{}", match_id); let path_string = format!("/tft/match/v1/matches/{}", match_id);
self.base.get::<tft_match_v1::Match>("tft-match-v1.getMatch", region, path_string, None) self.base.get::<tft_match_v1::Match>("tft-match-v1.getMatch", region.into(), path_string, None)
} }
} }
@ -672,7 +672,7 @@ impl<'a> TftSummonerV1<'a> {
-> impl Future<Output = Result<Option<tft_summoner_v1::Summoner>>> + 'a -> impl Future<Output = Result<Option<tft_summoner_v1::Summoner>>> + 'a
{ {
let path_string = format!("/tft/summoner/v1/summoners/by-account/{}", encrypted_account_id); let path_string = format!("/tft/summoner/v1/summoners/by-account/{}", encrypted_account_id);
self.base.get::<tft_summoner_v1::Summoner>("tft-summoner-v1.getByAccountId", region, path_string, None) self.base.get::<tft_summoner_v1::Summoner>("tft-summoner-v1.getByAccountId", region.into(), path_string, None)
} }
/// Get a summoner by summoner name. /// Get a summoner by summoner name.
@ -685,7 +685,7 @@ impl<'a> TftSummonerV1<'a> {
-> impl Future<Output = Result<Option<tft_summoner_v1::Summoner>>> + 'a -> impl Future<Output = Result<Option<tft_summoner_v1::Summoner>>> + 'a
{ {
let path_string = format!("/tft/summoner/v1/summoners/by-name/{}", summoner_name); let path_string = format!("/tft/summoner/v1/summoners/by-name/{}", summoner_name);
self.base.get::<tft_summoner_v1::Summoner>("tft-summoner-v1.getBySummonerName", region, path_string, None) self.base.get::<tft_summoner_v1::Summoner>("tft-summoner-v1.getBySummonerName", region.into(), path_string, None)
} }
/// Get a summoner by PUUID. /// Get a summoner by PUUID.
@ -698,7 +698,7 @@ impl<'a> TftSummonerV1<'a> {
-> impl Future<Output = Result<Option<tft_summoner_v1::Summoner>>> + 'a -> impl Future<Output = Result<Option<tft_summoner_v1::Summoner>>> + 'a
{ {
let path_string = format!("/tft/summoner/v1/summoners/by-puuid/{}", encrypted_puuid); let path_string = format!("/tft/summoner/v1/summoners/by-puuid/{}", encrypted_puuid);
self.base.get::<tft_summoner_v1::Summoner>("tft-summoner-v1.getByPUUID", region, path_string, None) self.base.get::<tft_summoner_v1::Summoner>("tft-summoner-v1.getByPUUID", region.into(), path_string, None)
} }
/// Get a summoner by summoner ID. /// Get a summoner by summoner ID.
@ -711,7 +711,7 @@ impl<'a> TftSummonerV1<'a> {
-> impl Future<Output = Result<Option<tft_summoner_v1::Summoner>>> + 'a -> impl Future<Output = Result<Option<tft_summoner_v1::Summoner>>> + 'a
{ {
let path_string = format!("/tft/summoner/v1/summoners/{}", encrypted_summoner_id); let path_string = format!("/tft/summoner/v1/summoners/{}", encrypted_summoner_id);
self.base.get::<tft_summoner_v1::Summoner>("tft-summoner-v1.getBySummonerId", region, path_string, None) self.base.get::<tft_summoner_v1::Summoner>("tft-summoner-v1.getBySummonerId", region.into(), path_string, None)
} }
} }
@ -733,7 +733,7 @@ impl<'a> ThirdPartyCodeV4<'a> {
-> impl Future<Output = Result<Option<String>>> + 'a -> impl Future<Output = Result<Option<String>>> + 'a
{ {
let path_string = format!("/lol/platform/v4/third-party-code/by-summoner/{}", encrypted_summoner_id); let path_string = format!("/lol/platform/v4/third-party-code/by-summoner/{}", encrypted_summoner_id);
self.base.get::<String>("third-party-code-v4.getThirdPartyCodeBySummonerId", region, path_string, None) self.base.get::<String>("third-party-code-v4.getThirdPartyCodeBySummonerId", region.into(), path_string, None)
} }
} }
@ -755,7 +755,7 @@ impl<'a> TournamentStubV4<'a> {
-> impl Future<Output = Result<Option<tournament_stub_v4::LobbyEventWrapper>>> + 'a -> impl Future<Output = Result<Option<tournament_stub_v4::LobbyEventWrapper>>> + 'a
{ {
let path_string = format!("/lol/tournament-stub/v4/lobby-events/by-code/{}", tournament_code); let path_string = format!("/lol/tournament-stub/v4/lobby-events/by-code/{}", tournament_code);
self.base.get::<tournament_stub_v4::LobbyEventWrapper>("tournament-stub-v4.getLobbyEventsByCode", region, path_string, None) self.base.get::<tournament_stub_v4::LobbyEventWrapper>("tournament-stub-v4.getLobbyEventsByCode", region.into(), path_string, None)
} }
} }
@ -777,7 +777,7 @@ impl<'a> TournamentV4<'a> {
-> impl Future<Output = Result<Option<tournament_v4::TournamentCode>>> + 'a -> impl Future<Output = Result<Option<tournament_v4::TournamentCode>>> + 'a
{ {
let path_string = format!("/lol/tournament/v4/codes/{}", tournament_code); let path_string = format!("/lol/tournament/v4/codes/{}", tournament_code);
self.base.get::<tournament_v4::TournamentCode>("tournament-v4.getTournamentCode", region, path_string, None) self.base.get::<tournament_v4::TournamentCode>("tournament-v4.getTournamentCode", region.into(), path_string, None)
} }
/// Gets a list of lobby events by tournament code. /// Gets a list of lobby events by tournament code.
@ -790,7 +790,7 @@ impl<'a> TournamentV4<'a> {
-> impl Future<Output = Result<Option<tournament_v4::LobbyEventWrapper>>> + 'a -> impl Future<Output = Result<Option<tournament_v4::LobbyEventWrapper>>> + 'a
{ {
let path_string = format!("/lol/tournament/v4/lobby-events/by-code/{}", tournament_code); let path_string = format!("/lol/tournament/v4/lobby-events/by-code/{}", tournament_code);
self.base.get::<tournament_v4::LobbyEventWrapper>("tournament-v4.getLobbyEventsByCode", region, path_string, None) self.base.get::<tournament_v4::LobbyEventWrapper>("tournament-v4.getLobbyEventsByCode", region.into(), path_string, None)
} }
} }

View File

@ -8,7 +8,6 @@ use tokio::timer::delay_for;
use crate::Result; use crate::Result;
use crate::RiotApiError; use crate::RiotApiError;
use crate::RiotApiConfig; use crate::RiotApiConfig;
use crate::consts::Region;
use crate::util::InsertOnlyCHashMap; use crate::util::InsertOnlyCHashMap;
use super::RateLimit; use super::RateLimit;
@ -38,7 +37,7 @@ impl RegionalRequester {
pub fn get<'a, T: serde::de::DeserializeOwned>(self: Arc<Self>, pub fn get<'a, T: serde::de::DeserializeOwned>(self: Arc<Self>,
config: &'a RiotApiConfig, client: &'a Client, config: &'a RiotApiConfig, client: &'a Client,
method_id: &'static str, region: Region, path: String, query: Option<String>) method_id: &'static str, region_platform: &'a str, path: String, query: Option<String>)
-> impl Future<Output = Result<Option<T>>> + 'a -> impl Future<Output = Result<Option<T>>> + 'a
{ {
async move { async move {
@ -55,7 +54,7 @@ impl RegionalRequester {
} }
// Send request. // Send request.
let url_base = format!("https://{}.api.riotgames.com", region.platform); let url_base = format!("https://{}.api.riotgames.com", region_platform);
let mut url = Url::parse(&*url_base) let mut url = Url::parse(&*url_base)
.unwrap_or_else(|_| panic!("Failed to parse url_base: \"{}\".", url_base)); .unwrap_or_else(|_| panic!("Failed to parse url_base: \"{}\".", url_base));
url.set_path(&*path); url.set_path(&*path);

View File

@ -5,7 +5,6 @@ use reqwest::Client;
use crate::Result; use crate::Result;
use crate::RiotApiConfig; use crate::RiotApiConfig;
use crate::consts::Region;
use crate::req::RegionalRequester; use crate::req::RegionalRequester;
use crate::util::InsertOnlyCHashMap; use crate::util::InsertOnlyCHashMap;
@ -27,7 +26,7 @@ pub struct RiotApi {
client: Client, client: Client,
/// Per-region requesters. /// Per-region requesters.
regional_requesters: InsertOnlyCHashMap<Region, RegionalRequester>, regional_requesters: InsertOnlyCHashMap<&'static str, RegionalRequester>,
} }
impl RiotApi { impl RiotApi {
@ -46,15 +45,14 @@ impl RiotApi {
} }
pub fn get<'a, T: serde::de::DeserializeOwned + 'a>(&'a self, pub fn get<'a, T: serde::de::DeserializeOwned + 'a>(&'a self,
method_id: &'static str, region: Region, path: String, query: Option<String>) method_id: &'static str, region_platform: &'static str, path: String, query: Option<String>)
-> impl Future<Output = Result<Option<T>>> + 'a -> impl Future<Output = Result<Option<T>>> + 'a
{ {
// TODO: max concurrent requests? Or can configure client?
self.regional_requesters self.regional_requesters
.get_or_insert_with(region, || { .get_or_insert_with(region_platform, || {
log::debug!("Creating requester for region {}.", region.platform); log::debug!("Creating requester for region platform {}.", region_platform);
RegionalRequester::new() RegionalRequester::new()
}) })
.get(&self.config, &self.client, method_id, region, path, query) .get(&self.config, &self.client, method_id, region_platform, path, query)
} }
} }

View File

@ -146,7 +146,7 @@ impl<'a> {{= endpoint }}<'a> {
let query_string = query_params.finish(); let query_string = query_params.finish();
{{?}} {{?}}
let path_string = {{= routeArgument }}; let path_string = {{= routeArgument }};
self.base.get::<{{= returnType }}>("{{= operationId }}", region, path_string, {{= queryParams.length ? 'Some(query_string)' : 'None' }}) self.base.get::<{{= returnType }}>("{{= operationId }}", region.into(), path_string, {{= queryParams.length ? 'Some(query_string)' : 'None' }})
} }
{{ {{

View File

@ -20,7 +20,7 @@ async_tests!{
// let p = future_start(p); // let p = future_start(p);
let ll = p.await.map_err(|e| e.to_string())?.ok_or("Failed to get challenger league".to_owned())?; let ll = p.await.map_err(|e| e.to_string())?.ok_or("Failed to get challenger league".to_owned())?;
println!("{} Challenger {} entries.", REGION.key, ll.entries.len()); println!("{:?} Challenger {} entries.", REGION, ll.entries.len());
let sl = ll.entries[..50].iter() let sl = ll.entries[..50].iter()
.map(|entry| RIOT_API.summoner_v4().get_by_summoner_id(REGION, &entry.summoner_id)) .map(|entry| RIOT_API.summoner_v4().get_by_summoner_id(REGION, &entry.summoner_id))