Skip to content

Commit

Permalink
Move some stuff around
Browse files Browse the repository at this point in the history
  • Loading branch information
acatiadroid committed Dec 30, 2023
1 parent 8d234fe commit 69e19f9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion bloxlink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

from .client import Bloxlink
from .objects import *
from .exceptions import *
from .exception import *
48 changes: 23 additions & 25 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,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

0 comments on commit 69e19f9

Please sign in to comment.