diff --git a/bloxlink/__init__.py b/bloxlink/__init__.py index 9296a23..21fd79e 100644 --- a/bloxlink/__init__.py +++ b/bloxlink/__init__.py @@ -2,8 +2,8 @@ __author__ = "acatiadroid", __license__ = "MIT" __copyright__ = "Copyright 2023-present acatiadroid" -__version__ = "0.1.0" +__version__ = "0.2.0" 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/client.py b/bloxlink/client.py index d641f9e..edddf32 100644 --- a/bloxlink/client.py +++ b/bloxlink/client.py @@ -4,8 +4,7 @@ class Bloxlink: """The base class for all Bloxlink API interactions.""" def __init__(self, api_key: str): - self.api_key = api_key - self._headers = {"Authorization": self.api_key} + self.request = Request(api_key) async def lookup_roblox_user(self, discord_id: int, server_id: int = None) -> RobloxUserResponse: """Looks up a Roblox user from the given Discord ID (and optional server ID). @@ -19,7 +18,7 @@ async def lookup_roblox_user(self, discord_id: int, server_id: int = None) -> Ro *if you intend on doing a global search (which required approval), do not include this argument. Otherwise, you must provide this argument.""" - return await Request._get_roblox_user(self._headers, discord_id, server_id) + return await self.request._get_roblox_user(discord_id, server_id) async def lookup_discord_user(self, roblox_id: int, server_id: int = None) -> DiscordUserReponse: @@ -34,7 +33,7 @@ async def lookup_discord_user(self, roblox_id: int, server_id: int = None) -> Di *if you intend on doing a global search (which required approval), do not include this argument. Otherwise, you must provide this argument.""" - return await Request._get_discord_user(self._headers, roblox_id, server_id) + return await self.request._get_discord_user(roblox_id, server_id) async def update_user(self, discord_id: int, server_id: int) -> dict: """Updates a Discord user in the specified server. This is equivalent to running /verify in a server. @@ -45,4 +44,4 @@ async def update_user(self, discord_id: int, server_id: int) -> dict: discord_id - the ID of the Discord user you want to update. server_id - the ID of the server you want to update the user in.""" - return await Request._update_discord_user(self._headers, discord_id, server_id) + return await self.request._update_discord_user(discord_id, server_id) diff --git a/bloxlink/exceptions.py b/bloxlink/exception.py similarity index 100% rename from bloxlink/exceptions.py rename to bloxlink/exception.py diff --git a/bloxlink/objects/response.py b/bloxlink/objects/response.py index 6be376a..909ce43 100644 --- a/bloxlink/objects/response.py +++ b/bloxlink/objects/response.py @@ -7,6 +7,9 @@ class RobloxUserResponse: """Represents a response from retrieving a Roblox user.""" def __init__(self, data: dict): self.data = data + + def __str__(self): + return str(self.user_id) @property def user_id(self) -> Optional[int]: @@ -28,6 +31,9 @@ class DiscordUserReponse: def __init__(self, data: dict): self.data = data + def __iter__(self): + return iter(self.discord_users) + @property def discord_users(self) -> list[DiscordUser]: """A list of users associated with the given ID.""" diff --git a/bloxlink/payload.py b/bloxlink/payload.py index 30e385c..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,45 +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): - async with aiohttp.ClientSession(headers=headers) as session: - async with session.post(url) as resp: - payload = await resp.json() +class Request: + def __init__(self, api_key): + self.headers = {"Authorization": api_key} - _check_exceptions(resp, payload, "POST") + 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() - return payload + _check_exceptions(resp, payload, "POST") -async def _make_get_request(headers: str, url: str): - async with aiohttp.ClientSession(headers=headers) as session: - async with session.get(url) as resp: - payload = await resp.json() + 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() - _check_exceptions(resp, payload, "GET") + _check_exceptions(resp, payload, "GET") - return payload + return payload -class Request: - async def _get_roblox_user(headers: dict, discord_id: int, server_id: int = None): + async def _get_roblox_user(self, discord_id: int, server_id: int = None): if server_id: - resp = await _make_get_request(headers, RESOLVED_ROBLOX_USER_URL.format(server_id, discord_id)) + resp = await self._make_get_request(RESOLVED_ROBLOX_USER_URL.format(server_id, discord_id)) else: - resp = await _make_get_request(headers, GLOBAL_RESOLVED_ROBLOX_USER_URL.format(discord_id)) + resp = await self._make_get_request(GLOBAL_RESOLVED_ROBLOX_USER_URL.format(discord_id)) return RobloxUserResponse(resp) - async def _get_discord_user(headers: dict, roblox_id: int, server_id: int = None): + async def _get_discord_user(self, roblox_id: int, server_id: int = None): if server_id: - resp = await _make_get_request(headers, RESOLVED_DISCORD_IDS_URL.format(server_id, roblox_id)) + resp = await self._make_get_request(RESOLVED_DISCORD_IDS_URL.format(server_id, roblox_id)) else: - resp = await _make_get_request(headers, GLOBAL_RESOLVED_DISCORD_IDS_URL.format(roblox_id)) + resp = await self._make_get_request(GLOBAL_RESOLVED_DISCORD_IDS_URL.format(roblox_id)) return DiscordUserReponse(resp) - async def _update_discord_user(headers: dict, discord_id: int, server_id: int): - resp = await _make_post_request(headers, UPDATE_DISCORD_USER_URL.format(server_id, discord_id)) + async def _update_discord_user(self, discord_id: int, server_id: int): + resp = await self._make_post_request(UPDATE_DISCORD_USER_URL.format(server_id, discord_id)) return resp \ No newline at end of file