diff options
author | mjk134 <57556877+mjk134@users.noreply.github.com> | 2022-07-09 17:54:58 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-09 17:54:58 +0000 |
commit | 55fe04a1d73625600c87e41fe45e5a713bda0aba (patch) | |
tree | 8915e25ef062197f2ef1173e940a587c800b399c /discord | |
parent | e6a2a2270d34a6915b5728d34ea69814b5cd785c (diff) | |
download | discobra-55fe04a1d73625600c87e41fe45e5a713bda0aba.tar.gz |
feat(rest): Added a class with methods to access api, with a single session
Diffstat (limited to 'discord')
-rw-r--r-- | discord/client.py | 12 | ||||
-rw-r--r-- | discord/utils/rest.py | 41 |
2 files changed, 23 insertions, 30 deletions
diff --git a/discord/client.py b/discord/client.py index 01e7fdd..3384657 100644 --- a/discord/client.py +++ b/discord/client.py @@ -5,10 +5,11 @@ import sys import threading from typing import Optional, Coroutine, Any, Callable import zlib +import aiohttp import websockets from .utils import EventEmitter -from .utils.rest import get +from .utils.rest import RESTClient from .intents import Intents, get_number from .user import User @@ -26,12 +27,13 @@ class GatewayEvents(IntEnum): HELLO = 10 HEARTBEAT_ACK = 11 GUILD_SYNC = 12 + class Client: _token: str @property async def user(self): - data = await get(self._token, '/users/@me') + data = await self.rest_client.get('/users/@me') return User(data) def __init__(self, intents: list[Intents]): @@ -43,6 +45,10 @@ class Client: self.inflator = zlib.decompressobj() self.heartbeat_interval: int = None self.ready: bool = False + self.rest_client = RESTClient(self._token, aiohttp.ClientSession(headers={ + "Authorization": f"Bot {self._token}", + "User-Agent": "DiscordBot (https://github.com/mounderfod/discobra 0.0.1)" + })) async def connect(self): async with websockets.connect("wss://gateway.discord.gg/?v=10&encoding=json") as gateway: @@ -92,7 +98,7 @@ class Client: event = msg['t'] if event == 'READY': - self.user = User(data['user']) + print(data) self.event_emitter.emit('on_' + event.lower()) 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: |