From bfca7817989b2f3992f7ca92df38c4288b27942c Mon Sep 17 00:00:00 2001 From: acatia Date: Thu, 28 Dec 2023 19:46:19 +0000 Subject: [PATCH 1/4] Don't create ClientSession on each request --- bloxlink/__init__.py | 2 +- bloxlink/client.py | 9 ++++----- bloxlink/payload.py | 33 +++++++++++++++++++-------------- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/bloxlink/__init__.py b/bloxlink/__init__.py index 9296a23..e53768e 100644 --- a/bloxlink/__init__.py +++ b/bloxlink/__init__.py @@ -2,7 +2,7 @@ __author__ = "acatiadroid", __license__ = "MIT" __copyright__ = "Copyright 2023-present acatiadroid" -__version__ = "0.1.0" +__version__ = "0.2.0" from .client import Bloxlink from .objects import * 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/payload.py b/bloxlink/payload.py index 30e385c..e10f6a7 100644 --- a/bloxlink/payload.py +++ b/bloxlink/payload.py @@ -16,18 +16,19 @@ def _check_exceptions(resp: dict, payload: dict, method: str): if 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: +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") return payload -async def _make_get_request(headers: str, url: str): - async with aiohttp.ClientSession(headers=headers) as session: - async with session.get(url) as resp: +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, "GET") @@ -35,24 +36,28 @@ async def _make_get_request(headers: str, url: str): return payload class Request: - async def _get_roblox_user(headers: dict, discord_id: int, server_id: int = None): + def __init__(self, api_key): + self.client = aiohttp.ClientSession() + self.headers = {"Authorization": api_key} + + 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 _make_get_request(self.headers, RESOLVED_ROBLOX_USER_URL.format(server_id, discord_id), self.client) else: - resp = await _make_get_request(headers, GLOBAL_RESOLVED_ROBLOX_USER_URL.format(discord_id)) + resp = await _make_get_request(self.headers, GLOBAL_RESOLVED_ROBLOX_USER_URL.format(discord_id), self.client) 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 _make_get_request(self.headers, RESOLVED_DISCORD_IDS_URL.format(server_id, roblox_id), self.client) else: - resp = await _make_get_request(headers, GLOBAL_RESOLVED_DISCORD_IDS_URL.format(roblox_id)) + resp = await _make_get_request(self.headers, GLOBAL_RESOLVED_DISCORD_IDS_URL.format(roblox_id), self.client) 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 _make_post_request(self.headers, UPDATE_DISCORD_USER_URL.format(server_id, discord_id), self.client) return resp \ No newline at end of file From dfb07dc0774c09a1eddeef0acc229dc85aaa7519 Mon Sep 17 00:00:00 2001 From: acatia Date: Sat, 30 Dec 2023 20:05:04 +0000 Subject: [PATCH 2/4] Rename exceptions to exception --- bloxlink/{exceptions.py => exception.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename bloxlink/{exceptions.py => exception.py} (100%) diff --git a/bloxlink/exceptions.py b/bloxlink/exception.py similarity index 100% rename from bloxlink/exceptions.py rename to bloxlink/exception.py From 8d234fef199bf06a053140c53f47f7d162e3d2fd Mon Sep 17 00:00:00 2001 From: acatia Date: Sat, 30 Dec 2023 20:06:12 +0000 Subject: [PATCH 3/4] Add default response representations --- bloxlink/objects/response.py | 6 ++++++ 1 file changed, 6 insertions(+) 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.""" From 69e19f9d0a1b886a8db0f989df2efe3a037bf91c Mon Sep 17 00:00:00 2001 From: acatia Date: Sat, 30 Dec 2023 20:06:27 +0000 Subject: [PATCH 4/4] Move some stuff around --- bloxlink/__init__.py | 2 +- bloxlink/payload.py | 48 +++++++++++++++++++++----------------------- 2 files changed, 24 insertions(+), 26 deletions(-) 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