diff --git a/bloxlink/__init__.py b/bloxlink/__init__.py index e53768e..21fd79e 100644 --- a/bloxlink/__init__.py +++ b/bloxlink/__init__.py @@ -6,4 +6,4 @@ from .client import Bloxlink from .objects import * -from .exceptions import * \ No newline at end of file +from .exception import * \ No newline at end of file diff --git a/bloxlink/payload.py b/bloxlink/payload.py index e10f6a7..8fc9493 100644 --- a/bloxlink/payload.py +++ b/bloxlink/payload.py @@ -1,7 +1,7 @@ import aiohttp from .objects import * -from .exceptions import _raise_error_code, _raise_error_message +from .exception import _raise_error_code, _raise_error_message RESOLVED_ROBLOX_USER_URL = "https://api.blox.link/v4/public/guilds/{}/discord-to-roblox/{}" RESOLVED_DISCORD_IDS_URL = "https://api.blox.link/v4/public/guilds/{}/roblox-to-discord/{}" @@ -14,50 +14,48 @@ def _check_exceptions(resp: dict, payload: dict, method: str): return _raise_error_code(resp.status, payload.get("error", payload)) if payload.get("error"): - return _raise_error_message(f"{method} request failed: {payload.get("error")}") + return _raise_error_message(f"{method} request failed: {payload.get('error')}") -async def _make_post_request(headers: str, url: str, client: aiohttp.ClientSession): - async with client: - async with client.post(url, headers=headers, json={}) as resp: - resp.raise_for_status() - payload = await resp.json() - - _check_exceptions(resp, payload, "POST") +class Request: + def __init__(self, api_key): + self.headers = {"Authorization": api_key} - return payload + async def _make_post_request(self, url: str): + async with aiohttp.ClientSession() as client: + async with client.post(url, headers=self.headers, json={}) as resp: # for some reason it needs to recieve an empty payload + payload = await resp.json() -async def _make_get_request(headers: str, url: str, client: aiohttp.ClientSession): - async with client: - async with client.get(url, headers=headers) as resp: - payload = await resp.json() + _check_exceptions(resp, payload, "POST") - _check_exceptions(resp, payload, "GET") + return payload + + async def _make_get_request(self, url: str): + async with aiohttp.ClientSession() as client: + async with client.get(url, headers=self.headers) as resp: + payload = await resp.json() - return payload + _check_exceptions(resp, payload, "GET") -class Request: - def __init__(self, api_key): - self.client = aiohttp.ClientSession() - self.headers = {"Authorization": api_key} + return payload async def _get_roblox_user(self, discord_id: int, server_id: int = None): if server_id: - resp = await _make_get_request(self.headers, RESOLVED_ROBLOX_USER_URL.format(server_id, discord_id), self.client) + resp = await self._make_get_request(RESOLVED_ROBLOX_USER_URL.format(server_id, discord_id)) else: - resp = await _make_get_request(self.headers, GLOBAL_RESOLVED_ROBLOX_USER_URL.format(discord_id), self.client) + resp = await self._make_get_request(GLOBAL_RESOLVED_ROBLOX_USER_URL.format(discord_id)) return RobloxUserResponse(resp) async def _get_discord_user(self, roblox_id: int, server_id: int = None): if server_id: - resp = await _make_get_request(self.headers, RESOLVED_DISCORD_IDS_URL.format(server_id, roblox_id), self.client) + resp = await self._make_get_request(RESOLVED_DISCORD_IDS_URL.format(server_id, roblox_id)) else: - resp = await _make_get_request(self.headers, GLOBAL_RESOLVED_DISCORD_IDS_URL.format(roblox_id), self.client) + resp = await self._make_get_request(GLOBAL_RESOLVED_DISCORD_IDS_URL.format(roblox_id)) return DiscordUserReponse(resp) async def _update_discord_user(self, discord_id: int, server_id: int): - resp = await _make_post_request(self.headers, UPDATE_DISCORD_USER_URL.format(server_id, discord_id), self.client) + resp = await self._make_post_request(UPDATE_DISCORD_USER_URL.format(server_id, discord_id)) return resp \ No newline at end of file