diff --git a/api.py b/api.py index e37a5a5..dcae1dd 100644 --- a/api.py +++ b/api.py @@ -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()