Skip to content

Commit

Permalink
feat: add tenacity to sigup and SignUpError exception
Browse files Browse the repository at this point in the history
  • Loading branch information
pradishb committed Oct 2, 2024
1 parent 1280068 commit 6f1f88a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions league_client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class ChatHostNotFound(RSOError):
pass


class SignUpError(RSOError):
pass


class LCUConnectionError(LCUError):
pass

Expand Down
14 changes: 10 additions & 4 deletions league_client/rso/sign_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

from league_client.constants import HEADERS
from league_client.constants import SSL_CONTEXT
from league_client.exceptions import SignUpError
from league_client.types import ProxyT
from league_client.utils import retry_on_read_timeout


def sign_up(
Expand All @@ -22,14 +24,16 @@ def sign_up(
headers={"User-Agent": HEADERS["User-Agent"]},
) as client:
# step 1: setup
res = client.get(
res = retry_on_read_timeout(client.get)(
r"https://xsso.leagueoflegends.com/login?uri=https://signup.leagueoflegends.com&prompt=signup&show_region=true&locale=en_US",
follow_redirects=True,
)
res.raise_for_status()

# step 2: get hcaptcha details (sitekey and rqdata)
res = client.get("https://authenticate.riotgames.com/api/v1/login")
res = retry_on_read_timeout(client.get)(
"https://authenticate.riotgames.com/api/v1/login"
)
res.raise_for_status()
data = res.json()
site_key = data["captcha"]["hcaptcha"]["key"]
Expand All @@ -50,15 +54,17 @@ def sign_up(
"birth_date": dob,
"captcha": f"hcaptcha {token}",
}
res = client.put(
res = retry_on_read_timeout(client.put)(
"https://authenticate.riotgames.com/api/v1/login", json=data
)
res.raise_for_status()
data = res.json()

# step 5(optional): account is already created at step 4
# this step is for parsing ssid and clid
res = client.get(
if "success" not in data:
raise SignUpError(res)
res = retry_on_read_timeout(client.get)(
data["success"]["redirect_url"],
headers=HEADERS,
follow_redirects=True,
Expand Down
9 changes: 9 additions & 0 deletions league_client/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import httpx
from tenacity import retry
from tenacity import retry_if_exception_type
from tenacity import stop_after_attempt

retry_on_read_timeout = retry(
stop=stop_after_attempt(5),
retry=retry_if_exception_type(httpx.ReadTimeout),
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "league-client"
version = "2.1.5"
dependencies = ["httpx"]
dependencies = ["httpx", "tenacity"]
requires-python = ">=3"
authors = [{ name = "Pradish Bijukchhe", email = "[email protected]" }]
description = "league-client is a python package to communicate with riot servers"
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
httpx
tenacity

0 comments on commit 6f1f88a

Please sign in to comment.