[~] API Routes upgrade

This commit is contained in:
Charles Le Maux 2024-06-23 23:38:07 +01:00
parent bbff1b0f88
commit afb35cc79a

18
api.py
View File

@ -1,5 +1,6 @@
import http
from typing import Dict
from enum import Enum
from typing import Dict, Optional
from flask import Flask, Response, jsonify, request
from flask_cors import CORS
@ -11,6 +12,11 @@ CORS(app)
cors = CORS(app, origins=['*'])
class ActionType(Enum):
ACCOUNT_CREATE = '/AccountCreate'
SERVER_CREATE = '/ServerCreate'
def generic_response_maker(status_code: http.HTTPStatus) -> tuple[Response, int]:
match status_code:
case http.HTTPStatus.CREATED:
@ -35,6 +41,16 @@ def generic_response_maker(status_code: http.HTTPStatus) -> tuple[Response, int]
return message, status_code.value
def parse_and_validate_request(required_keys: set) -> Optional[dict]:
data = request.get_json()
if not data:
raise Exception("Empty request body.")
missing_keys = required_keys - set(data.keys())
if missing_keys:
raise Exception("Missing keys: {}".format(missing_keys))
return data
@app.route('/AccountCreate', methods=['POST'])
def account_create() -> tuple[Response, int]:
data: Dict[str, str] = request.get_json()