From 55fe04a1d73625600c87e41fe45e5a713bda0aba Mon Sep 17 00:00:00 2001 From: mjk134 <57556877+mjk134@users.noreply.github.com> Date: Sat, 9 Jul 2022 17:54:58 +0000 Subject: feat(rest): Added a class with methods to access api, with a single session --- discord/utils/rest.py | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) (limited to 'discord/utils') diff --git a/discord/utils/rest.py b/discord/utils/rest.py index f7f6ab8..919d54d 100644 --- a/discord/utils/rest.py +++ b/discord/utils/rest.py @@ -1,15 +1,14 @@ import aiohttp -import asyncio from discord.utils.exceptions import APIException +class RESTClient: + def __init__(self, token: str, session: aiohttp.ClientSession): + self.token = token + self.session = session -async def get(token, url): - async with aiohttp.ClientSession(headers={ - "Authorization": f"Bot {token}", - "User-Agent": f"DiscordBot (https://github.com/mounderfod/discobra 0.0.1)" - }) as session: - async with session.get(url='https://discord.com/api/v10' + url) as r: + async def get(self, url: str): + async with self.session.get(url='https://discord.com/api/v10' + url) as r: data = await r.json() match r.status: case 200: @@ -18,12 +17,8 @@ async def get(token, url): raise APIException(data['message']) -async def post(token, url, data): - async with aiohttp.ClientSession(headers={ - "Authorization": f"Bot {token}", - "User-Agent": f"DiscordBot (https://github.com/mounderfod/discobra 0.0.1)" - }) as session: - async with session.post(url='https://discord.com/api/v10' + url, data=data) as r: + 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() match r.status: case 200 | 204: @@ -32,26 +27,18 @@ async def post(token, url, data): raise APIException(data['message']) -async def patch(token, url, data): - async with aiohttp.ClientSession(headers={ - "Authorization": f"Bot {token}", - "User-Agent": f"DiscordBot (https://github.com/mounderfod/discobra 0.0.1)" - }) as session: - async with session.patch(url='https://discord.com/api/v10' + url, data=data) as r: - data = await r.json() - match r.status: + 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() + match res.status: case 200 | 204: return data case other: raise APIException(data['message']) -async def delete(token, url): - async with aiohttp.ClientSession(headers={ - "Authorization": f"Bot {token}", - "User-Agent": f"DiscordBot (https://github.com/mounderfod/discobra 0.0.1)" - }) as session: - async with session.delete(url='https://discord.com/api/v10' + url) as r: + async def delete(self, url): + async with self.session.delete(url='https://discord.com/api/v10' + url) as r: data = await r.json() match r.status: case 200: -- cgit 1.4.1-2-gfad0