From fabbc3ac7a6a39dbb6c44512ec4bf17ac2745126 Mon Sep 17 00:00:00 2001 From: Noah Date: Sat, 9 Jul 2022 19:03:31 +0100 Subject: chore: Reformat code --- discord/utils/rest.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'discord/utils/rest.py') diff --git a/discord/utils/rest.py b/discord/utils/rest.py index 919d54d..53853ed 100644 --- a/discord/utils/rest.py +++ b/discord/utils/rest.py @@ -2,6 +2,7 @@ import aiohttp from discord.utils.exceptions import APIException + class RESTClient: def __init__(self, token: str, session: aiohttp.ClientSession): self.token = token @@ -16,7 +17,6 @@ class RESTClient: case other: raise APIException(data['message']) - async def post(self, url: str, data): async with self.session.post(url='https://discord.com/api/v10' + url, data=data) as r: data = await r.json() @@ -26,7 +26,6 @@ class RESTClient: case other: raise APIException(data['message']) - async def patch(self, url, data): async with self.session.patch(url='https://discord.com/api/v10' + url, data=data) as res: data = await res.json() @@ -36,7 +35,6 @@ class RESTClient: case other: raise APIException(data['message']) - async def delete(self, url): async with self.session.delete(url='https://discord.com/api/v10' + url) as r: data = await r.json() -- cgit 1.4.1-2-gfad0 From 6576d95f3601613fc03232b106f1cdeda1d99e06 Mon Sep 17 00:00:00 2001 From: Noah Date: Sat, 9 Jul 2022 19:14:04 +0100 Subject: docs: Add more documentation where possible. --- discord/client.py | 14 ++++++++++++++ discord/utils/rest.py | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) (limited to 'discord/utils/rest.py') diff --git a/discord/client.py b/discord/client.py index 7ba1f96..fc9bcd5 100644 --- a/discord/client.py +++ b/discord/client.py @@ -16,18 +16,32 @@ from .user import User class GatewayEvents(IntEnum): + """ + Contains constants for the gateway opcodes. + """ DISPATCH = 0 + """An event was dispatched.""" HEARTBEAT = 1 + """Sent at regular intervals by the client to keep the gateway connection alive.""" IDENTIFY = 2 + """Used to identify yourself with the token during the initial handshake.""" PRESENCE = 3 + """Used to update the client's presence.""" VOICE_STATE = 4 + """Used to join and leave voice channels.""" VOICE_PING = 5 RESUME = 6 + """Used to resume a disconnected session.""" RECONNECT = 7 + """Used to reconnect to the session.""" REQUEST_MEMBERS = 8 + """Used to request information about guild members when there are too many for """ INVALIDATE_SESSION = 9 + """Means that the session is invalid. When this is received, you must reconnect and re-identify.""" HELLO = 10 + """Acknowledgement of gateway connection.""" HEARTBEAT_ACK = 11 + """Acknowledgement of gateway heartbeat.""" GUILD_SYNC = 12 diff --git a/discord/utils/rest.py b/discord/utils/rest.py index 53853ed..652de5d 100644 --- a/discord/utils/rest.py +++ b/discord/utils/rest.py @@ -4,11 +4,22 @@ from discord.utils.exceptions import APIException class RESTClient: + """ + Utility class to make it easier to make HTTP requests to Discord's API. This should not be used manually, + as it only works with Discord's API and the library should cover anything that can be requested from it. Any + requests to other APIs should use `aiohttp`. + """ def __init__(self, token: str, session: aiohttp.ClientSession): self.token = token self.session = session async def get(self, url: str): + """ + Makes a GET request to Discord's API. + + **Parameters:** + - url: The part of the request URL that goes after `https://discord.com/api/v10` + """ async with self.session.get(url='https://discord.com/api/v10' + url) as r: data = await r.json() match r.status: @@ -18,15 +29,29 @@ class RESTClient: raise APIException(data['message']) async def post(self, url: str, data): + """ + Makes a POST request to Discord's API. + + **Parameters:** + - url: The part of the request URL that goes after `https://discord.com/api/v10` + - data: The data to post. + """ async with self.session.post(url='https://discord.com/api/v10' + url, data=data) as r: data = await r.json() match r.status: - case 200 | 204: + case 200 | 204 | 201: return data case other: raise APIException(data['message']) async def patch(self, url, data): + """ + Makes a PATCH request to Discord's API. + + **Parameters:** + - url: The part of the request URL that goes after `https://discord.com/api/v10` + - data: The data to patch. + """ async with self.session.patch(url='https://discord.com/api/v10' + url, data=data) as res: data = await res.json() match res.status: @@ -36,6 +61,12 @@ class RESTClient: raise APIException(data['message']) async def delete(self, url): + """ + Makes a POST request to Discord's API. + + **Parameters:** + - url: The part of the request URL that goes after `https://discord.com/api/v10` + """ async with self.session.delete(url='https://discord.com/api/v10' + url) as r: data = await r.json() match r.status: -- cgit 1.4.1-2-gfad0