Organized code

This commit is contained in:
nsde 2023-09-17 21:41:16 +02:00
parent 2f2e8512eb
commit f367520191

View file

@ -36,25 +36,6 @@ async def check_core_auth(request):
return None return None
@router.get('/users')
async def get_users(discord_id: int, incoming_request: fastapi.Request):
"""Returns a user by their discord ID. Requires a core API key."""
auth = await check_core_auth(incoming_request)
if auth:
return auth
# Get user by discord ID
manager = UserManager()
user = await manager.user_by_discord_id(discord_id)
if not user:
return await errors.error(404, 'Discord user not found in the API database.', 'Check the `discord_id` parameter.')
# turn the ObjectId into a string
user['_id'] = str(user['_id'])
return user
async def new_user_webhook(user: dict) -> None: async def new_user_webhook(user: dict) -> None:
"""Runs when a new user is created.""" """Runs when a new user is created."""
@ -73,6 +54,25 @@ async def new_user_webhook(user: dict) -> None:
dhook.send(content=f'<@{dc}>', embed=embed) dhook.send(content=f'<@{dc}>', embed=embed)
@router.get('/users')
async def get_users(discord_id: int, incoming_request: fastapi.Request):
"""Returns a user by their discord ID. Requires a core API key."""
auth = await check_core_auth(incoming_request)
if auth: return auth
# Get user by discord ID
manager = UserManager()
user = await manager.user_by_discord_id(discord_id)
if not user:
return await errors.error(404, 'Discord user not found in the API database.', 'Check the `discord_id` parameter.')
# turn the ObjectId into a string
user['_id'] = str(user['_id'])
return user
@router.post('/users') @router.post('/users')
async def create_user(incoming_request: fastapi.Request): async def create_user(incoming_request: fastapi.Request):
"""Creates a user. Requires a core API key.""" """Creates a user. Requires a core API key."""
@ -102,9 +102,7 @@ async def update_user(incoming_request: fastapi.Request):
"""Updates a user. Requires a core API key.""" """Updates a user. Requires a core API key."""
auth_error = await check_core_auth(incoming_request) auth_error = await check_core_auth(incoming_request)
if auth_error: return auth_error
if auth_error:
return auth_error
try: try:
payload = await incoming_request.json() payload = await incoming_request.json()
@ -127,9 +125,7 @@ async def run_checks(incoming_request: fastapi.Request):
"""Tests the API. Requires a core API key.""" """Tests the API. Requires a core API key."""
auth_error = await check_core_auth(incoming_request) auth_error = await check_core_auth(incoming_request)
if auth_error: return auth_error
if auth_error:
return auth_error
results = {} results = {}