Skip to content

Commit

Permalink
updated League endpoint stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
jjmaldonis committed Jul 26, 2019
1 parent 5217099 commit cc5731f
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 141 deletions.
9 changes: 4 additions & 5 deletions cassiopeia/core/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __call__(self, **kwargs):
class LeagueSummonerEntriesData(CoreDataList):
"""League entries for a single summoner."""
_dto_type = LeagueSummonerEntriesDto
_renamed = {}
_renamed = {"summonerId": "summoner_id"}


class LeagueEntriesData(CoreDataList):
Expand Down Expand Up @@ -274,7 +274,6 @@ def division(self) -> Division:
class LeagueSummonerEntries(CassiopeiaLazyList):
_data_types = {LeagueSummonerEntriesData}

@provide_default_region
def __init__(self, *, summoner: Summoner):
self.__summoner = summoner
kwargs = {"region": summoner.region}
Expand All @@ -296,7 +295,7 @@ def __get_query_from_kwargs__(cls, *, summoner: Union[Summoner, str]) -> dict:

@lazy_property
def region(self) -> Region:
return Region(self._data[LeagueEntriesData].region)
return Region(self._data[LeagueSummonerEntriesData].region)

@lazy_property
def platform(self) -> Platform:
Expand Down Expand Up @@ -328,12 +327,12 @@ def threes(self):
class League(CassiopeiaGhost):
_data_types = {LeagueData}

def __init__(self, id: str = None, region: Union[Region, str] = None):
def __init__(self, id: str = None, queue: Queue = None, region: Union[Region, str] = None):
if region is None:
region = configuration.settings.default_region
if region is not None and not isinstance(region, Region):
region = Region(region)
kwargs = {"id": id, "region": region}
kwargs = {"id": id, "region": region, "queue": queue}
super().__init__(**kwargs)

def __get_query__(self):
Expand Down
58 changes: 15 additions & 43 deletions cassiopeia/datastores/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from ..core.staticdata.languagestrings import LanguageStringsData, LanguageStrings
from ..core.staticdata.version import VersionListData, Versions
from ..core.championmastery import ChampionMasteryData, ChampionMasteryListData, ChampionMastery, ChampionMasteries
from ..core.league import MasterLeagueListData, GrandmasterLeagueListData, ChallengerLeagueListData, LeagueEntries, League, ChallengerLeague, GrandmasterLeague, MasterLeague, LeagueEntries
from ..core.league import MasterLeagueListData, GrandmasterLeagueListData, ChallengerLeagueListData, LeagueSummonerEntries, League, ChallengerLeague, GrandmasterLeague, MasterLeague, LeagueEntries
from ..core.match import MatchData, TimelineData, Match, Timeline
from ..core.summoner import SummonerData, Summoner
from ..core.status import ShardStatusData, ShardStatus
Expand Down Expand Up @@ -46,7 +46,7 @@
ProfileIcons: datetime.timedelta(days=20),
ChampionMastery: datetime.timedelta(days=7),
ChampionMasteries: datetime.timedelta(days=7),
LeagueEntries: datetime.timedelta(hours=6),
LeagueSummonerEntries: datetime.timedelta(hours=6),
League: datetime.timedelta(hours=6),
ChallengerLeague: datetime.timedelta(hours=6),
GrandmasterLeague: datetime.timedelta(hours=6),
Expand Down Expand Up @@ -225,45 +225,25 @@ def put_many_champion_masteries(self, items: Iterable[ChampionMasteries], contex
# League API #
##############

# LeagueEntries
# LeagueSummonerEntries

@get.register(LeagueEntries)
@get.register(LeagueSummonerEntries)
@validate_query(uniquekeys.validate_league_entries_query, uniquekeys.convert_region_to_platform)
def get_league_position(self, query: Mapping[str, Any], context: PipelineContext = None) -> LeagueEntries:
return self._get(LeagueEntries, query, uniquekeys.for_league_entries_query, context)
def get_league_summoner_entries(self, query: Mapping[str, Any], context: PipelineContext = None) -> LeagueSummonerEntries:
return self._get(LeagueSummonerEntries, query, uniquekeys.for_league_summoner_entries_query, context)

@get_many.register(LeagueEntries)
@get_many.register(LeagueSummonerEntries)
@validate_query(uniquekeys.validate_many_league_entries_query, uniquekeys.convert_region_to_platform)
def get_many_league_position(self, query: Mapping[str, Any], context: PipelineContext = None) -> Generator[LeagueEntries, None, None]:
return self._get_many(LeagueEntries, query, uniquekeys.for_many_league_entries_query, context)
def get_many_league_summoner_entries(self, query: Mapping[str, Any], context: PipelineContext = None) -> Generator[LeagueSummonerEntries, None, None]:
return self._get_many(LeagueSummonerEntries, query, uniquekeys.for_many_league_summoner_entries_query, context)

@put.register(LeagueEntries)
def put_league_position(self, item: LeagueEntries, context: PipelineContext = None) -> None:
self._put(LeagueEntries, item, uniquekeys.for_league_entries, context=context)
@put.register(LeagueSummonerEntries)
def put_league_summoner_entries(self, item: LeagueSummonerEntries, context: PipelineContext = None) -> None:
self._put(LeagueSummonerEntries, item, uniquekeys.for_league_summoner_entries, context=context)

@put_many.register(LeagueEntries)
def put_many_league_position(self, items: Iterable[LeagueEntries], context: PipelineContext = None) -> None:
self._put_many(LeagueEntries, items, uniquekeys.for_league_entries, context=context)

# Leagues

@get.register(League)
@validate_query(uniquekeys.validate_league_query, uniquekeys.convert_region_to_platform)
def get_league_summoner(self, query: Mapping[str, Any], context: PipelineContext = None) -> League:
return self._get(League, query, uniquekeys.for_league_query, context)

@get_many.register(League)
@validate_query(uniquekeys.validate_many_league_query, uniquekeys.convert_region_to_platform)
def get_many_league_summoner(self, query: Mapping[str, Any], context: PipelineContext = None) -> Generator[League, None, None]:
return self._get_many(League, query, uniquekeys.for_many_league_query, context)

@put.register(League)
def put_league_summoner(self, item: League, context: PipelineContext = None) -> None:
self._put(League, item, uniquekeys.for_league, context=context)

@put_many.register(League)
def put_many_league_summoner(self, items: Iterable[League], context: PipelineContext = None) -> None:
self._put_many(League, items, uniquekeys.for_league, context=context)
@put_many.register(LeagueSummonerEntries)
def put_many_league_summoner_entries(self, items: Iterable[LeagueSummonerEntries], context: PipelineContext = None) -> None:
self._put_many(LeagueSummonerEntries, items, uniquekeys.for_league_summoner_entries, context=context)

# Challenger

Expand Down Expand Up @@ -325,14 +305,6 @@ def put_league_summoner(self, item: MasterLeague, context: PipelineContext = Non
def put_many_league_summoner(self, items: Iterable[MasterLeague], context: PipelineContext = None) -> None:
self._put_many(MasterLeague, items, uniquekeys.for_master_league, context=context)

@get.register(LeagueEntries)
@validate_query(uniquekeys.validate_league_entries_list_query, uniquekeys.convert_region_to_platform)
def get_league_entries_list(self, query: Mapping[str, Any], context: ProfileIcon = None) -> LeagueEntries:
return self._get(LeagueEntries, query, uniquekeys.for_league_entries_list_query, context)

@put.register(LeagueEntries)
def put_league_entries_list(self, item: LeagueEntries, context: PipelineContext = None) -> None:
self._put(LeagueEntries, item, uniquekeys.for_league_entries_list, context=context)

###################
# Static Data API #
Expand Down
114 changes: 43 additions & 71 deletions cassiopeia/datastores/kernel/leagues.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datapipelines import DataSource, PipelineContext, Query, NotFoundError, validate_query
from .common import KernelSource, APINotFoundError
from ...data import Platform, Queue, Tier, Division
from ...dto.league import LeaguesListDto, ChallengerLeagueListDto, MasterLeagueListDto,GrandmasterLeagueListDto, LeaguePositionsDto, LeagueListDto, PaginatedLeaguesListDto
from ...dto.league import LeagueEntriesDto, LeagueDto, LeagueSummonerEntriesDto, ChallengerLeagueListDto, MasterLeagueListDto, GrandmasterLeagueListDto
from ..uniquekeys import convert_region_to_platform

T = TypeVar("T")
Expand All @@ -18,18 +18,20 @@ def get(self, type: Type[T], query: MutableMapping[str, Any], context: PipelineC
def get_many(self, type: Type[T], query: MutableMapping[str, Any], context: PipelineContext = None) -> Iterable[T]:
pass

_validate_get_paginated_leagues_list_query = Query. \
# League Entries

_validate_get_league_entries_query = Query. \
has("queue").as_(Queue).also. \
has("tier").as_(Tier).also. \
has("division").as_(Division).also. \
has("page").as_(int).also. \
has("platform").as_(Platform)

@get.register(PaginatedLeaguesListDto)
@validate_query(_validate_get_paginated_leagues_list_query, convert_region_to_platform)
def get_league_entries_list(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> PaginatedLeaguesListDto:
parameters = {"platform": query["platform"].value}
endpoint = "lol/league/v4/entries/{queue}/{tier}/{division}/{page}".format(
@get.register(LeagueEntriesDto)
@validate_query(_validate_get_league_entries_query, convert_region_to_platform)
def get_league_entries_list(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> LeagueEntriesDto:
parameters = {"platform": query["platform"].value, "page": query["page"]}
endpoint = "lol/league/v4/entries/{queue}/{tier}/{division}".format(
queue=query["queue"].value,
tier=query["tier"].value,
division=query["division"].value,
Expand All @@ -42,85 +44,52 @@ def get_league_entries_list(self, query: MutableMapping[str, Any], context: Pipe
region = query["platform"].region.value
for entry in data:
entry["region"] = region
return PaginatedLeaguesListDto(entries=data, page=query["page"], region=query["region"].value, queue=query["queue"].value, tier=query["tier"].value, division=query["division"].value)

# Leagues
return LeagueEntriesDto(entries=data, page=query["page"], region=query["region"].value, queue=query["queue"].value, tier=query["tier"].value, division=query["division"].value)

_validate_get_leagues_query = Query. \
has("id").as_(str).also. \
_validate_get_league_summoner_entries_query = Query. \
has("summoner.id").as_(str).also. \
has("platform").as_(Platform)

@get.register(LeagueListDto)
@validate_query(_validate_get_leagues_query, convert_region_to_platform)
def get_leagues_list(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> LeagueListDto:
# League Summoner Entries

@get.register(LeagueSummonerEntriesDto)
@validate_query(_validate_get_league_summoner_entries_query, convert_region_to_platform)
def get_league_summoner_entries_list(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> LeagueSummonerEntriesDto:
parameters = {"platform": query["platform"].value}
endpoint = "lol/league/v4/leagues/{leagueId}".format(leagueId=query["id"])
endpoint = "lol/league/v4/entries/by-summoner/{id}".format(id=query["summoner.id"])
try:
data = self._get(endpoint=endpoint, parameters=parameters)
except APINotFoundError as error:
raise NotFoundError(str(error)) from error

data["region"] = query["platform"].region.value
for entry in data["entries"]:
entry["region"] = data["region"]
entry["tier"] = data["tier"]
return LeagueListDto(data)

_validate_get_many_leagues_by_summoner_query = Query. \
has("summoner.ids").as_(Iterable).also. \
has("platform").as_(Platform)

@get_many.register(LeaguesListDto)
@validate_query(_validate_get_many_leagues_by_summoner_query, convert_region_to_platform)
def get_many_leagues_list(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> Generator[LeaguesListDto, None, None]:
def generator():
parameters = {"platform": query["platform"].value}
for id in query["summoner.ids"]:
endpoint = "lol/league/v4/leagues/by-summoner/{summonerId}".format(summonerId=id)
try:
data = self._get(endpoint=endpoint, parameters=parameters)
except APINotFoundError as error:
raise NotFoundError(str(error)) from error

data["region"] = query["platform"].region.value
data["summonerId"] = id
for entry in data["entries"]:
entry["region"] = data["region"]
yield LeaguesListDto(data)
region = query["platform"].region.value
for entry in data:
entry["region"] = region
return LeagueSummonerEntriesDto(entries=data, region=region, summonerId=query["summoner.id"])

return generator()
# League by ID

_validate_get_many_leagues_query = Query. \
has("ids").as_(Iterable).also. \
_validate_get_league_query = Query. \
has("id").as_(str).also. \
has("platform").as_(Platform)

@get_many.register(LeagueListDto)
@validate_query(_validate_get_many_leagues_query, convert_region_to_platform)
def get_many_leagues_list(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> Generator[LeagueListDto, None, None]:
def generator():
parameters = {"platform": query["platform"].value}
for id in query["ids"]:
endpoint = "lol/league/v4/leagues/{leagueId}".format(leagueId=id)
try:
data = self._get(endpoint=endpoint, parameters=parameters)
except APINotFoundError as error:
raise NotFoundError(str(error)) from error

data = {"leagues": data}
data["region"] = query["platform"].region.value
for league in data["leagues"]:
league["region"] = data["region"]
for entry in league["entries"]:
entry["region"] = data["region"]
yield LeagueListDto(data)

return generator()

@get.register(LeagueDto)
@validate_query(_validate_get_league_query, convert_region_to_platform)
def get_leagues_list(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> LeagueDto:
parameters = {"platform": query["platform"].value}
endpoint = "leagues/{leagueId}".format(leagueId=query["id"])
try:
data = self._get(endpoint=endpoint, parameters=parameters)
except APINotFoundError as error:
raise NotFoundError(str(error)) from error
return LeagueDto(data)

_validate_get_challenger_league_query = Query. \
has("queue").as_(Queue).also. \
has("platform").as_(Platform)

# Challenger League

@get.register(ChallengerLeagueListDto)
@validate_query(_validate_get_challenger_league_query, convert_region_to_platform)
def get_challenger_league_list(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> ChallengerLeagueListDto:
Expand All @@ -143,7 +112,7 @@ def get_challenger_league_list(self, query: MutableMapping[str, Any], context: P

@get_many.register(ChallengerLeagueListDto)
@validate_query(_validate_get_many_challenger_league_query, convert_region_to_platform)
def get_challenger_leagues_list(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> Generator[ChallengerLeagueListDto, None, None]:
def get_many_challenger_leagues_list(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> Generator[ChallengerLeagueListDto, None, None]:
def generator():
parameters = {"platform": query["platform"].value}
for queue in query["queues"]:
Expand All @@ -162,6 +131,8 @@ def generator():

return generator()

# Grandmaster League

_validate_get_grandmaster_league_query = Query. \
has("queue").as_(Queue).also. \
has("platform").as_(Platform)
Expand All @@ -188,7 +159,7 @@ def get_grandmaster_league_list(self, query: MutableMapping[str, Any], context:

@get_many.register(GrandmasterLeagueListDto)
@validate_query(_validate_get_many_grandmaster_league_query, convert_region_to_platform)
def get_grandmaster_leagues_list(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> Generator[GrandmasterLeagueListDto, None, None]:
def get_many_grandmaster_leagues_list(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> Generator[GrandmasterLeagueListDto, None, None]:
def generator():
parameters = {"platform": query["platform"].value}
for queue in query["queues"]:
Expand All @@ -207,6 +178,7 @@ def generator():

return generator()

# Master League

_validate_get_master_league_query = Query. \
has("queue").as_(Queue).also. \
Expand Down Expand Up @@ -234,7 +206,7 @@ def get_master_league_list(self, query: MutableMapping[str, Any], context: Pipel

@get_many.register(MasterLeagueListDto)
@validate_query(_validate_get_many_master_league_query, convert_region_to_platform)
def get_master_leagues_list(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> Generator[MasterLeagueListDto, None, None]:
def get_many_master_leagues_list(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> Generator[MasterLeagueListDto, None, None]:
def generator():
parameters = {"platform": query["platform"].value}
for queue in query["queues"]:
Expand Down
Loading

0 comments on commit cc5731f

Please sign in to comment.