Skip to content

Commit

Permalink
Merge pull request #1 from acatiadroid/acatia-dev
Browse files Browse the repository at this point in the history
Make HTTP requests inside Request class
  • Loading branch information
acatiadroid committed Dec 30, 2023
2 parents 26a22bd + 69e19f9 commit 8a3d7c0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 30 deletions.
4 changes: 2 additions & 2 deletions bloxlink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
from .exception import *
9 changes: 4 additions & 5 deletions bloxlink/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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:
Expand All @@ -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.
Expand All @@ -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)
File renamed without changes.
6 changes: 6 additions & 0 deletions bloxlink/objects/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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."""
Expand Down
49 changes: 26 additions & 23 deletions bloxlink/payload.py
Original file line number Diff line number Diff line change
@@ -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/{}"
Expand All @@ -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

0 comments on commit 8a3d7c0

Please sign in to comment.