diff options
author | mjk134 <57556877+mjk134@users.noreply.github.com> | 2022-07-11 20:03:24 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-11 20:03:24 +0000 |
commit | de4b806a7e5b823680acae4627607198cf1f59a6 (patch) | |
tree | e1db0047b5db098eafd930a39091645232edce8a /discord/utils | |
parent | b271f3b37c9a28ba9254f2a24c98cfcd65cb3c24 (diff) | |
parent | 6576d95f3601613fc03232b106f1cdeda1d99e06 (diff) | |
download | discobra-de4b806a7e5b823680acae4627607198cf1f59a6.tar.gz |
Merge branch 'master' of https://github.com/mounderfod/discobra
Diffstat (limited to 'discord/utils')
-rw-r--r-- | discord/utils/event_emitter.py | 9 | ||||
-rw-r--r-- | discord/utils/exceptions.py | 1 | ||||
-rw-r--r-- | discord/utils/rest.py | 37 |
3 files changed, 38 insertions, 9 deletions
diff --git a/discord/utils/event_emitter.py b/discord/utils/event_emitter.py index 08b6060..7911326 100644 --- a/discord/utils/event_emitter.py +++ b/discord/utils/event_emitter.py @@ -1,18 +1,19 @@ import asyncio from typing import Optional, Coroutine, Any, Callable, Dict -class EventEmitter(): - def __init__(self, loop: Optional[asyncio.AbstractEventLoop]=None): + +class EventEmitter: + def __init__(self, loop: Optional[asyncio.AbstractEventLoop] = None): self.listeners: Dict[str, Optional[Callable[..., Coroutine[Any, Any, Any]]]] = {} self.loop = loop if loop else asyncio.get_event_loop() - def add_listener(self, event_name: str, func: Optional[Callable[..., Coroutine[Any, Any, Any]]]=None): + def add_listener(self, event_name: str, func: Optional[Callable[..., Coroutine[Any, Any, Any]]] = None): if not self.listeners.get(event_name, None): self.listeners[event_name] = {func} else: self.listeners[event_name].add(func) - def remove_listener(self, event_name: str, func: Optional[Callable[..., Coroutine[Any, Any, Any]]]=None): + def remove_listener(self, event_name: str, func: Optional[Callable[..., Coroutine[Any, Any, Any]]] = None): self.listeners[event_name].remove(func) if len(self.listeners[event_name]) == 0: del self.listeners[event_name] diff --git a/discord/utils/exceptions.py b/discord/utils/exceptions.py index a7f035c..d3e5748 100644 --- a/discord/utils/exceptions.py +++ b/discord/utils/exceptions.py @@ -1,3 +1,2 @@ class APIException(Exception): """Raised when the Discord API returns an error.""" - diff --git a/discord/utils/rest.py b/discord/utils/rest.py index 919d54d..652de5d 100644 --- a/discord/utils/rest.py +++ b/discord/utils/rest.py @@ -2,12 +2,24 @@ import aiohttp 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: @@ -16,18 +28,30 @@ class RESTClient: case other: 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,8 +60,13 @@ class RESTClient: case other: 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: |