diff options
author | 2020-10-25 19:49:27 +0000 | |
---|---|---|
committer | 2020-10-25 19:49:27 +0000 | |
commit | 50a2a20dfadf40be3d6b787d1f3e36bc6c388374 (patch) | |
tree | 4c954022b198f1fbb1aa7b5e0007f4ac505e32c6 /backend/discord.py | |
parent | Add constants (diff) |
Add Discord API wrapper
Diffstat (limited to 'backend/discord.py')
-rw-r--r-- | backend/discord.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/backend/discord.py b/backend/discord.py new file mode 100644 index 0000000..d6310b7 --- /dev/null +++ b/backend/discord.py @@ -0,0 +1,38 @@ +"""Various utilities for working with the Discord API.""" +import httpx + +from backend.constants import ( + OAUTH2_CLIENT_ID, OAUTH2_CLIENT_SECRET, OAUTH2_REDIRECT_URI +) + +API_BASE_URL = "https://discord.com/api/v8" + + +async def fetch_bearer_token(access_code: str) -> dict: + async with httpx.AsyncClient() as client: + data = { + "client_id": OAUTH2_CLIENT_ID, + "client_secret": OAUTH2_CLIENT_SECRET, + "grant_type": "authorization_code", + "code": access_code, + "redirect_uri": OAUTH2_REDIRECT_URI + } + + r = await client.post(f"{API_BASE_URL}/oauth2/token", headers={ + "Content-Type": "application/x-www-form-urlencoded" + }, data=data) + + r.raise_for_status() + + return r.json() + + +async def fetch_user_details(bearer_token: str) -> dict: + async with httpx.AsyncClient() as client: + r = await client.get(f"{API_BASE_URL}/users/@me", headers={ + "Authorization": f"Bearer {bearer_token}" + }) + + r.raise_for_status() + + return r.json() |