Skip to content

Commit

Permalink
Remap some regions automatically; remove 3rd party code (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudonym117 authored Apr 25, 2022
1 parent e4e7329 commit 3f8cf01
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 174 deletions.
10 changes: 9 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Check for full (read: slightly better) documentation `here <http://riot-watcher.

RiotWatcher is a thin wrapper on top of the `Riot Games API for League
of Legends <https://developer.riotgames.com/>`__. All public methods as
of 4/4/20222 are supported in full.
of 4/25/20222 are supported in full.

RiotWatcher by default supports a naive rate limiter. This rate limiter will
try to stop you from making too many requests, and in a single threaded test
Expand Down Expand Up @@ -165,6 +165,14 @@ Rate limiter has some race conditions when used concurrently.

Changelog
---------
v3.2.2 = 4/25/2022
~~~~~~~~~~~~~~~~~~
Added support for remapping 'na1' -> 'americas' for LoL Matchv5 and TFT Match endpoints

Removed LoL 3rd Party Code API, as has been removed by riot

Updated some documentation

v3.2.1 - 4/4/2022
~~~~~~~~~~~~~~~~~
Added ddragon all versions method.
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Welcome to RiotWatcher's documentation!

RiotWatcher is a thin wrapper on top of the `Riot Games API for League
of Legends <https://developer.riotgames.com/>`__. All public methods as
of 4/4/2022 are supported in full.
of 4/25/2022 are supported in full.

RiotWatcher by default supports a naive rate limiter. This rate limiter will
try to stop you from making too many requests, and in a single threaded test
Expand Down
13 changes: 6 additions & 7 deletions src/riotwatcher/LolWatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
SpectatorApiV4,
SummonerApiV4,
MatchApiV5,
ThirdPartyCodeApiV4,
)

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -107,7 +106,6 @@ def __init__(
self._match = MatchApiV5(self._base_api)
self._spectator = SpectatorApiV4(self._base_api)
self._summoner = SummonerApiV4(self._base_api)
self._third_party_code = ThirdPartyCodeApiV4(self._base_api)

self._lol_status = (
self._lol_status_v4 if default_status_v4 else self._lol_status_v3
Expand Down Expand Up @@ -238,10 +236,11 @@ def summoner(self) -> SummonerApiV4:
return self._summoner

@property
def third_party_code(self) -> ThirdPartyCodeApiV4:
def third_party_code(self) -> None:
"""
Interface to the Third Party Code Endpoint
:rtype: league_of_legends.ThirdPartyCodeApiV4
DEPRECATED: API has been removed by Riot
"""
return self._third_party_code
raise NotImplementedError(
"API has been removed by Riot and no longer functions"
)

2 changes: 1 addition & 1 deletion src/riotwatcher/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "3.2.1"
__version__ = "3.2.2"
__author__ = "pseudonym117"
__title__ = "RiotWatcher"
36 changes: 36 additions & 0 deletions src/riotwatcher/_apis/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from functools import wraps


def remap_region_to_platform(argument_pos: int, argument_name="region"):
def _remap_region_to_platform(api_call):
remap = {
"br1": "americas",
"la1": "americas",
"la2": "americas",
"na1": "americas",
"oc1": "americas",
"eun1": "europe",
"euw1": "europe",
"ru": "europe",
"tr1": "europe",
"jp1": "asia",
"kr": "asia",
}

@wraps(api_call)
def remapper(*args, **kwargs):
nargs = args
if argument_name in kwargs:
lower_region = kwargs[argument_name].lower()
if lower_region in remap:
kwargs[argument_name] = remap[lower_region]
if len(args) > argument_pos and isinstance(args[argument_pos], str):
region = args[1].lower()
if region in remap:
nargs = list(args)
nargs[1] = remap[region]
return api_call(*nargs, **kwargs)

return remapper

return _remap_region_to_platform
6 changes: 4 additions & 2 deletions src/riotwatcher/_apis/league_of_legends/MatchApiV5.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Set

from .. import BaseApi, NamedEndpoint
from ..helpers import remap_region_to_platform
from .urls import MatchApiV5Urls


Expand All @@ -20,6 +19,7 @@ def __init__(self, base_api: BaseApi):
"""
super().__init__(base_api, self.__class__.__name__)

@remap_region_to_platform(1)
def by_id(self, region: str, match_id: str):
"""
Get match by match ID
Expand All @@ -33,6 +33,7 @@ def by_id(self, region: str, match_id: str):
self.by_id.__name__, region, MatchApiV5Urls.by_id, match_id=match_id
)

@remap_region_to_platform(1)
def matchlist_by_puuid(
self,
region: str,
Expand Down Expand Up @@ -82,6 +83,7 @@ def matchlist_by_puuid(
endTime=end_time,
)

@remap_region_to_platform(1)
def timeline_by_match(self, region: str, match_id: int):
"""
Get match timeline by match ID.
Expand Down
38 changes: 0 additions & 38 deletions src/riotwatcher/_apis/league_of_legends/ThirdPartyCodeApiV4.py

This file was deleted.

1 change: 0 additions & 1 deletion src/riotwatcher/_apis/league_of_legends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
from .LolStatusApiV4 import LolStatusApiV4
from .SpectatorApiV4 import SpectatorApiV4
from .SummonerApiV4 import SummonerApiV4
from .ThirdPartyCodeApiV4 import ThirdPartyCodeApiV4
from .MatchApiV5 import MatchApiV5
3 changes: 3 additions & 0 deletions src/riotwatcher/_apis/team_fight_tactics/MatchApi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .. import BaseApi, NamedEndpoint
from ..helpers import remap_region_to_platform
from .urls import MatchApiUrls


Expand All @@ -17,6 +18,7 @@ def __init__(self, base_api: BaseApi):
"""
super().__init__(base_api, self.__class__.__name__)

@remap_region_to_platform(1)
def by_puuid(self, region: str, puuid: str, count: int = 20):
"""
Get a list of match ids by PUUID.
Expand All @@ -31,6 +33,7 @@ def by_puuid(self, region: str, puuid: str, count: int = 20):
count=count,
)

@remap_region_to_platform(1)
def by_id(self, region: str, match_id: str):
"""
Get a match by match id.
Expand Down
119 changes: 81 additions & 38 deletions tests/_apis/league_of_legends/test_MatchApiV5.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,66 @@
from collections import namedtuple
from unittest.mock import MagicMock

import pytest

from riotwatcher._apis.league_of_legends import MatchApiV5


MockMatchApi = namedtuple("MockMatchApi", ["base_api", "api", "expected_return"])


@pytest.fixture
def mock_match_api():
mock_base_api = MagicMock()
api = MatchApiV5(mock_base_api)
expected_return = object()
mock_base_api.raw_request.return_value = expected_return

return MockMatchApi(mock_base_api, api, expected_return)


@pytest.mark.lol
@pytest.mark.unit
class TestMatchApiV5:
def test_by_id(self):
mock_base_api = MagicMock()
expected_return = object()
mock_base_api.raw_request.return_value = expected_return

matchv5 = MatchApiV5(mock_base_api)
def test_by_id(self, mock_match_api):
region = "afaaas"
match_id = 54321

ret = matchv5.by_id(region, match_id)
ret = mock_match_api.api.by_id(region, match_id)

mock_base_api.raw_request.assert_called_once_with(
mock_match_api.base_api.raw_request.assert_called_once_with(
MatchApiV5.__name__,
matchv5.by_id.__name__,
mock_match_api.api.by_id.__name__,
region,
f"https://{region}.api.riotgames.com/lol/match/v5/matches/{match_id}",
{},
)

assert ret is expected_return
assert ret is mock_match_api.expected_return

def test_ml_by_puuid_defaults(self):
mock_base_api = MagicMock()
expected_return = object()
mock_base_api.raw_request.return_value = expected_return
def test_by_id_remaps_region(
self, mock_match_api, region_remap,
):
match_id = 54321
mock_match_api.api.by_id(region_remap.original, match_id)

matchv5 = MatchApiV5(mock_base_api)
mock_match_api.base_api.raw_request.assert_called_once_with(
MatchApiV5.__name__,
mock_match_api.api.by_id.__name__,
region_remap.to,
f"https://{region_remap.to}.api.riotgames.com/lol/match/v5/matches/{match_id}",
{},
)

def test_ml_by_puuid_defaults(self, mock_match_api):
region = "sfsfa"
puuid = "15357"

ret = matchv5.matchlist_by_puuid(region, puuid)
ret = mock_match_api.api.matchlist_by_puuid(region, puuid)

mock_base_api.raw_request.assert_called_once_with(
mock_match_api.base_api.raw_request.assert_called_once_with(
MatchApiV5.__name__,
matchv5.matchlist_by_puuid.__name__,
mock_match_api.api.matchlist_by_puuid.__name__,
region,
f"https://{region}.api.riotgames.com/lol/match/v5/matches/by-puuid/{puuid}/ids",
{
Expand All @@ -55,14 +73,30 @@ def test_ml_by_puuid_defaults(self):
},
)

assert ret is expected_return
assert ret is mock_match_api.expected_return

def test_ml_by_account_takes_param(self):
mock_base_api = MagicMock()
expected_return = object()
mock_base_api.raw_request.return_value = expected_return
def test_ml_by_puuid_remaps_region(
self, mock_match_api, region_remap,
):
puuid = "54321"
mock_match_api.api.matchlist_by_puuid(region_remap.original, puuid)

matchv5 = MatchApiV5(mock_base_api)
mock_match_api.base_api.raw_request.assert_called_once_with(
MatchApiV5.__name__,
mock_match_api.api.matchlist_by_puuid.__name__,
region_remap.to,
f"https://{region_remap.to}.api.riotgames.com/lol/match/v5/matches/by-puuid/{puuid}/ids",
{
"start": None,
"count": None,
"queue": None,
"type": None,
"startTime": None,
"endTime": None,
},
)

def test_ml_by_account_takes_param(self, mock_match_api):
region = "sfsfa"
puuid = "15357"

Expand All @@ -73,7 +107,7 @@ def test_ml_by_account_takes_param(self):
start_time = 1000
end_time = 2000

ret = matchv5.matchlist_by_puuid(
ret = mock_match_api.api.matchlist_by_puuid(
region,
puuid,
start=start,
Expand All @@ -84,9 +118,9 @@ def test_ml_by_account_takes_param(self):
end_time=end_time,
)

mock_base_api.raw_request.assert_called_once_with(
mock_match_api.base_api.raw_request.assert_called_once_with(
MatchApiV5.__name__,
matchv5.matchlist_by_puuid.__name__,
mock_match_api.api.matchlist_by_puuid.__name__,
region,
f"https://{region}.api.riotgames.com/lol/match/v5/matches/by-puuid/{puuid}/ids",
{
Expand All @@ -99,25 +133,34 @@ def test_ml_by_account_takes_param(self):
},
)

assert ret is expected_return
assert ret is mock_match_api.expected_return

def test_timeline_by_match(self):
mock_base_api = MagicMock()
expected_return = object()
mock_base_api.raw_request.return_value = expected_return

matchv5 = MatchApiV5(mock_base_api)
def test_timeline_by_match(self, mock_match_api):
region = "afaaas"
match_id = 262464

ret = matchv5.timeline_by_match(region, match_id)
ret = mock_match_api.api.timeline_by_match(region, match_id)

mock_base_api.raw_request.assert_called_once_with(
mock_match_api.base_api.raw_request.assert_called_once_with(
MatchApiV5.__name__,
matchv5.timeline_by_match.__name__,
mock_match_api.api.timeline_by_match.__name__,
region,
f"https://{region}.api.riotgames.com/lol/match/v5/matches/{match_id}/timeline",
{},
)

assert ret is expected_return
assert ret is mock_match_api.expected_return

def test_timeline_by_match_region(
self, mock_match_api, region_remap,
):
match_id = 54321
mock_match_api.api.timeline_by_match(region_remap.original, match_id)

mock_match_api.base_api.raw_request.assert_called_once_with(
MatchApiV5.__name__,
mock_match_api.api.timeline_by_match.__name__,
region_remap.to,
f"https://{region_remap.to}.api.riotgames.com/lol/match/v5/matches/{match_id}/timeline",
{},
)
Loading

0 comments on commit 3f8cf01

Please sign in to comment.