Skip to content

Commit 973d6ee

Browse files
authored
🔀: merge pull request #25 (rework)
2 parents 8ce3e20 + 509f7ed commit 973d6ee

File tree

7 files changed

+94
-160
lines changed

7 files changed

+94
-160
lines changed

docs/endpoints.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
Endpoints
22
=========
33

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

66
- **ACCOUNT-V1**
77
- **VAL-CONTENT-V1**
88
- **VAL-RANKED-V1**
99
- **VAL-STATUS-V1**
10+
- **VAL-MATCH-V1** (Unimplemented)
1011

1112
ACCOUNT-V1
1213
----------

docs/requests.rst

Lines changed: 0 additions & 58 deletions
This file was deleted.

riot.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
198cea51-4528-4853-991e-acae35781b74
1+
198cea51-4528-4853-991e-acae35781b74

valorant/__init__.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
import urllib3
2-
3-
urllib3.disable_warnings()
4-
5-
del urllib3
6-
71
from .client import Client
82
from .local import LocalClient
93
from .threads import AsyncClient
104

115
__all__ = ["Client", "AsyncClient", "LocalClient"]
126
__author__ = "frissyn"
13-
__version__ = "0.4.3"
7+
__version__ = "0.5.0"

valorant/caller.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import requests
2+
3+
from .values import ROUTES
4+
from .values import LOCALES
5+
from .values import REGIONS
6+
from .values import ENDPOINTS
7+
8+
9+
def value_check(*args):
10+
KEYS = ROUTES + LOCALES + REGIONS
11+
12+
for arg in args:
13+
if arg not in KEYS:
14+
raise ValueError
15+
else:
16+
return True
17+
18+
19+
class WebCaller(object):
20+
def __init__(self, token: str, locale: str, region: str, route: str):
21+
self.base = "https://{root}.api.riotgames.com/"
22+
self.eps = ENDPOINTS["web"]
23+
self.sess = requests.Session()
24+
self.sess.params.update({"locale": locale})
25+
self.sess.headers.update(
26+
{
27+
"Accept-Charset": "application/x-www-form-urlencoded; charset=UTF-8",
28+
"User-Agent": "Mozilla/5.0",
29+
"X-Riot-Token": token,
30+
}
31+
)
32+
33+
if value_check(locale, region, route):
34+
self.locale = locale
35+
self.region = region
36+
self.route = route
37+
38+
def call(self, m: str, ep: str, params=None, route=False, **kw):
39+
if ep not in list(self.eps.keys()):
40+
raise ValueError
41+
else:
42+
pass
43+
44+
prefix = self.base.format(root=self.route if route else self.region)
45+
url = prefix + self.eps[ep].format(**kw)
46+
47+
r = self.sess.request(m, url, params=params)
48+
r.raise_for_status()
49+
50+
return r.json()
51+
52+
53+
class ClientCaller(object):
54+
def __init__(self, token: str):
55+
self.base = "https://pd.{code}.a.pvp.net/"
56+
self.token = token
57+
58+
self.sess = requests.Session()
59+
self.sess.headers.update(
60+
{
61+
"Authorization": f"Bearer {token}",
62+
"Content-Type": "application/json",
63+
"X-Riot-Entitlements-JWT": "riot_entitlement",
64+
}
65+
)

valorant/client.py

Lines changed: 20 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import requests
21
import urllib.parse
32

3+
from .caller import WebCaller
4+
45
from .objects import ActDTO
56
from .objects import AccountDTO
67
from .objects import ContentItemDTO
@@ -10,14 +11,7 @@
1011
from .objects import ContentList
1112

1213
from .values import SAFES
13-
from .values import ROUTES
1414
from .values import LOCALE
15-
from .values import LOCALES
16-
from .values import REGIONS
17-
from .values import HEADERS
18-
from .values import WEB_API
19-
from .values import ENDPOINTS
20-
from .values import CLIENT_API
2115

2216

2317
def update(stale: dict, latest: dict) -> dict:
@@ -31,16 +25,9 @@ class Client(object):
3125
def __init__(self, key, locale=LOCALE, region="na", route="americas", reload=True):
3226
self.key = key
3327
self.route = route
28+
self.locale = locale
3429
self.region = region
35-
self.fetch = requests.get
36-
37-
if locale not in LOCALES:
38-
raise ValueError(
39-
f"The given locale '{locale}' is invalid. See "
40-
+ "`valorant.values.LOCALES` for a list of valid locales."
41-
)
42-
else:
43-
self.locale = locale
30+
self.handle = WebCaller(key, locale, region, route)
4431

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

5946
def reload(self) -> None:
6047
"""Reload the current cached response for the VAL-CONTENT endpoints."""
61-
62-
url = self.build_url(code=self.region, endpoint="content")
63-
heads = self.build_header({"X-Riot-Token": self.key})
64-
params = {"locale": self.locale}
65-
66-
r = self.fetch(url, params=params, headers=heads)
67-
r.raise_for_status()
68-
69-
self.set_attributes(r.json())
48+
r = self.handle.call("GET", "content")
49+
self.set_attributes(r)
7050

7151
return
7252

73-
def build_header(self, mixin: dict, base: str = "web") -> dict:
74-
"""Create a header dictionary from the default request headers."""
75-
76-
c = HEADERS[base].copy()
77-
78-
for n, v in mixin.items():
79-
c[n] = v
80-
81-
return c
82-
83-
def build_url(
84-
self, code: str = "na", endpoint: str = "content", base: str = "web"
85-
) -> str:
86-
"""Create a request URL with the given code and endpoint."""
87-
88-
if code not in REGIONS and code not in ROUTES:
89-
raise ValueError(f"Invalid Route Code: '{code}'")
90-
else:
91-
url = WEB_API if base == "web" else CLIENT_API
92-
end = ENDPOINTS[base][endpoint]
93-
url = url.format(code=code) + end
94-
95-
return url
96-
9753
def get_user_by_puuid(self, puuid: str) -> AccountDTO:
98-
"""Get a Riot account by the given puuid."""
99-
100-
heads = self.build_header({"X-Riot-Token": self.key})
101-
102-
url = self.build_url(code=self.route, endpoint="puuid")
103-
url = url.format(puuid=puuid)
104-
105-
r = self.fetch(url, headers=heads)
106-
r.raise_for_status()
54+
"""Get a Riot account by the given PUUID."""
55+
r = self.handle.call("GET", "puuid", puuid=puuid)
10756

108-
return AccountDTO(r.json())
57+
return AccountDTO(r)
10958

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

116-
url = self.build_url(code=self.route, endpoint="game-name")
117-
url = url.format(name=values[0], tag=values[1])
118-
119-
r = self.fetch(url, headers=heads)
120-
r.raise_for_status()
121-
122-
return AccountDTO(r.json())
65+
return AccountDTO(r)
12366

12467
def get_platform_status(self) -> PlatformDataDTO:
12568
"""Get the current platform status for Valorant."""
126-
url = self.build_url(code=self.region, endpoint="status")
127-
heads = self.build_header({"X-Riot-Token": self.key})
128-
params = {"locale": self.locale}
129-
130-
r = self.fetch(url, headers=heads, params=params)
131-
r.raise_for_status()
69+
r = self.handle.call("GET", "status")
13270

133-
return PlatformDataDTO(r.json())
71+
return PlatformDataDTO(r)
13472

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

183-
url = self.build_url(self.region, endpoint="leaderboard")
184-
url = url.format(actID=actID)
185-
186-
heads = self.build_header({"X-Riot-Token": self.key})
187-
params = {"locale": self.locale, "size": size, "startIndex": size * page}
188-
189-
r = self.fetch(url, headers=heads, params=params)
190-
r.raise_for_status()
122+
r = self.handle.call("GET", "leaderboard", params=params, actID=actID)
191123

192-
return LeaderboardDTO(r.json())
124+
return LeaderboardDTO(r)
193125

194126
def get_maps(self) -> ContentList:
195127
"""Get a ContentList of Maps from Valorant."""

valorant/values.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"tr-TR",
2828
"vi-VN",
2929
"zh-CN",
30-
"zh-TW"
30+
"zh-TW",
3131
]
3232

3333
REGIONS = [
@@ -44,7 +44,7 @@
4444
"ru",
4545
"tr",
4646
"latam",
47-
"ap"
47+
"ap",
4848
]
4949

5050
ENDPOINTS = {
@@ -60,7 +60,7 @@
6060
},
6161
"client": {
6262
"mmr": "mmr/v1/players/{playerID}/competitiveupdates",
63-
}
63+
},
6464
}
6565

6666
HEADERS = {
@@ -69,5 +69,5 @@
6969
"Authorization": "Bearer {token}",
7070
"Content-Type": "application/json",
7171
"X-Riot-Entitlements-JWT": "riot_entitlement",
72-
}
73-
}
72+
},
73+
}

0 commit comments

Comments
 (0)