diff options
| author | 2020-12-29 19:47:42 +0200 | |
|---|---|---|
| committer | 2020-12-29 19:47:42 +0200 | |
| commit | b51ef608658ab7026d3bbd38ed5c6f6615f5fe31 (patch) | |
| tree | 2cad92882f1b188b766dbfa090bc4133ee24529b /backend | |
| parent | Fix too long line (diff) | |
Handle ratelimits for role assigning
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/routes/forms/submit.py | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/backend/routes/forms/submit.py b/backend/routes/forms/submit.py index 0ed248d..37f76e0 100644 --- a/backend/routes/forms/submit.py +++ b/backend/routes/forms/submit.py @@ -2,6 +2,7 @@ Submit a form. """ +import asyncio import binascii import hashlib import uuid @@ -26,6 +27,10 @@ HCAPTCHA_HEADERS = { "Content-Type": "application/x-www-form-urlencoded" } +DISCORD_HEADERS = { + "Authorization": f"Bot {constants.DISCORD_BOT_TOKEN}" +} + class SubmissionResponse(BaseModel): form: Form @@ -234,10 +239,17 @@ class SubmitForm(Route): ) async with httpx.AsyncClient() as client: - resp = await client.put( - url, - headers={ - "Authorization": f"Bot {constants.DISCORD_BOT_TOKEN}" - } - ) - resp.raise_for_status() + resp = await client.put(url, headers=DISCORD_HEADERS) + if resp.status_code == 429: # We are rate limited + status = resp.status_code + retry_after = float(resp.headers["X-Ratelimit-Reset-After"]) + while status == 429: + await asyncio.sleep(retry_after) + r = await client.put(url, headers=DISCORD_HEADERS) + status = r.status_code + if status == 429: + retry_after = float(r.headers["X-Ratelimit-Reset-After"]) + else: + r.raise_for_status() + else: # For any other unexpected status, raise error. + resp.raise_for_status() |