Skip to content

Commit

Permalink
🔀: merge pull request #25 (rework)
Browse files Browse the repository at this point in the history
  • Loading branch information
frissyn authored Sep 21, 2021
2 parents 8ce3e20 + 509f7ed commit 973d6ee
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 160 deletions.
3 changes: 2 additions & 1 deletion docs/endpoints.rst
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
Endpoints
=========

``valorant.py``, as of ``v0.4.1``, covers 4 endpoints:
``valorant.py``, as of ``v0.5.0``, covers 4 endpoints:

- **ACCOUNT-V1**
- **VAL-CONTENT-V1**
- **VAL-RANKED-V1**
- **VAL-STATUS-V1**
- **VAL-MATCH-V1** (Unimplemented)

ACCOUNT-V1
----------
Expand Down
58 changes: 0 additions & 58 deletions docs/requests.rst

This file was deleted.

2 changes: 1 addition & 1 deletion riot.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
198cea51-4528-4853-991e-acae35781b74
198cea51-4528-4853-991e-acae35781b74
8 changes: 1 addition & 7 deletions valorant/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import urllib3

urllib3.disable_warnings()

del urllib3

from .client import Client
from .local import LocalClient
from .threads import AsyncClient

__all__ = ["Client", "AsyncClient", "LocalClient"]
__author__ = "frissyn"
__version__ = "0.4.3"
__version__ = "0.5.0"
65 changes: 65 additions & 0 deletions valorant/caller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import requests

from .values import ROUTES
from .values import LOCALES
from .values import REGIONS
from .values import ENDPOINTS


def value_check(*args):
KEYS = ROUTES + LOCALES + REGIONS

for arg in args:
if arg not in KEYS:
raise ValueError
else:
return True


class WebCaller(object):
def __init__(self, token: str, locale: str, region: str, route: str):
self.base = "https://{root}.api.riotgames.com/"
self.eps = ENDPOINTS["web"]
self.sess = requests.Session()
self.sess.params.update({"locale": locale})
self.sess.headers.update(
{
"Accept-Charset": "application/x-www-form-urlencoded; charset=UTF-8",
"User-Agent": "Mozilla/5.0",
"X-Riot-Token": token,
}
)

if value_check(locale, region, route):
self.locale = locale
self.region = region
self.route = route

def call(self, m: str, ep: str, params=None, route=False, **kw):
if ep not in list(self.eps.keys()):
raise ValueError
else:
pass

prefix = self.base.format(root=self.route if route else self.region)
url = prefix + self.eps[ep].format(**kw)

r = self.sess.request(m, url, params=params)
r.raise_for_status()

return r.json()


class ClientCaller(object):
def __init__(self, token: str):
self.base = "https://pd.{code}.a.pvp.net/"
self.token = token

self.sess = requests.Session()
self.sess.headers.update(
{
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
"X-Riot-Entitlements-JWT": "riot_entitlement",
}
)
108 changes: 20 additions & 88 deletions valorant/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import requests
import urllib.parse

from .caller import WebCaller

from .objects import ActDTO
from .objects import AccountDTO
from .objects import ContentItemDTO
Expand All @@ -10,14 +11,7 @@
from .objects import ContentList

from .values import SAFES
from .values import ROUTES
from .values import LOCALE
from .values import LOCALES
from .values import REGIONS
from .values import HEADERS
from .values import WEB_API
from .values import ENDPOINTS
from .values import CLIENT_API


def update(stale: dict, latest: dict) -> dict:
Expand All @@ -31,16 +25,9 @@ class Client(object):
def __init__(self, key, locale=LOCALE, region="na", route="americas", reload=True):
self.key = key
self.route = route
self.locale = locale
self.region = region
self.fetch = requests.get

if locale not in LOCALES:
raise ValueError(
f"The given locale '{locale}' is invalid. See "
+ "`valorant.values.LOCALES` for a list of valid locales."
)
else:
self.locale = locale
self.handle = WebCaller(key, locale, region, route)

if reload:
self.reload()
Expand All @@ -58,79 +45,30 @@ def set_attributes(self, attrs) -> None:

def reload(self) -> None:
"""Reload the current cached response for the VAL-CONTENT endpoints."""

url = self.build_url(code=self.region, endpoint="content")
heads = self.build_header({"X-Riot-Token": self.key})
params = {"locale": self.locale}

r = self.fetch(url, params=params, headers=heads)
r.raise_for_status()

self.set_attributes(r.json())
r = self.handle.call("GET", "content")
self.set_attributes(r)

return

def build_header(self, mixin: dict, base: str = "web") -> dict:
"""Create a header dictionary from the default request headers."""

c = HEADERS[base].copy()

for n, v in mixin.items():
c[n] = v

return c

def build_url(
self, code: str = "na", endpoint: str = "content", base: str = "web"
) -> str:
"""Create a request URL with the given code and endpoint."""

if code not in REGIONS and code not in ROUTES:
raise ValueError(f"Invalid Route Code: '{code}'")
else:
url = WEB_API if base == "web" else CLIENT_API
end = ENDPOINTS[base][endpoint]
url = url.format(code=code) + end

return url

def get_user_by_puuid(self, puuid: str) -> AccountDTO:
"""Get a Riot account by the given puuid."""

heads = self.build_header({"X-Riot-Token": self.key})

url = self.build_url(code=self.route, endpoint="puuid")
url = url.format(puuid=puuid)

r = self.fetch(url, headers=heads)
r.raise_for_status()
"""Get a Riot account by the given PUUID."""
r = self.handle.call("GET", "puuid", puuid=puuid)

return AccountDTO(r.json())
return AccountDTO(r)

def get_user_by_name(self, name: str, delim: str = "#") -> AccountDTO:
"""Get a Riot account by a given name split by a delimiter."""
heads = self.build_header({"X-Riot-Token": self.key})
values = name.split(delim)
values = [urllib.parse.quote(v, safe=SAFES) for v in values]
def get_user_by_name(self, name: str) -> AccountDTO:
"""Get a Riot account by a given name and tag."""
vals = name.split("#")
vals = [urllib.parse.quote(v, safe=SAFES) for v in vals]
r = self.handle.call("GET", "game-name", route=True, name=vals[0], tag=vals[1])

url = self.build_url(code=self.route, endpoint="game-name")
url = url.format(name=values[0], tag=values[1])

r = self.fetch(url, headers=heads)
r.raise_for_status()

return AccountDTO(r.json())
return AccountDTO(r)

def get_platform_status(self) -> PlatformDataDTO:
"""Get the current platform status for Valorant."""
url = self.build_url(code=self.region, endpoint="status")
heads = self.build_header({"X-Riot-Token": self.key})
params = {"locale": self.locale}

r = self.fetch(url, headers=heads, params=params)
r.raise_for_status()
r = self.handle.call("GET", "status")

return PlatformDataDTO(r.json())
return PlatformDataDTO(r)

def get_acts(self) -> ContentList:
"""Get a ContentList of Acts from Valorant."""
Expand Down Expand Up @@ -179,17 +117,11 @@ def get_equips(self) -> ContentList:
def get_leaderboard(self, size: int = 100, page: int = 0, actID: str = ""):
"""Get the top user's in your client's region during a given Act."""
actID = self.get_current_act().id if not actID else actID
params = {"size": size, "startIndex": size * page}

url = self.build_url(self.region, endpoint="leaderboard")
url = url.format(actID=actID)

heads = self.build_header({"X-Riot-Token": self.key})
params = {"locale": self.locale, "size": size, "startIndex": size * page}

r = self.fetch(url, headers=heads, params=params)
r.raise_for_status()
r = self.handle.call("GET", "leaderboard", params=params, actID=actID)

return LeaderboardDTO(r.json())
return LeaderboardDTO(r)

def get_maps(self) -> ContentList:
"""Get a ContentList of Maps from Valorant."""
Expand Down
10 changes: 5 additions & 5 deletions valorant/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"tr-TR",
"vi-VN",
"zh-CN",
"zh-TW"
"zh-TW",
]

REGIONS = [
Expand All @@ -44,7 +44,7 @@
"ru",
"tr",
"latam",
"ap"
"ap",
]

ENDPOINTS = {
Expand All @@ -60,7 +60,7 @@
},
"client": {
"mmr": "mmr/v1/players/{playerID}/competitiveupdates",
}
},
}

HEADERS = {
Expand All @@ -69,5 +69,5 @@
"Authorization": "Bearer {token}",
"Content-Type": "application/json",
"X-Riot-Entitlements-JWT": "riot_entitlement",
}
}
},
}

0 comments on commit 973d6ee

Please sign in to comment.