From c2f21cb1c1e4e0402e58d5e6ce446d39bde2185e Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Sun, 14 Jul 2024 19:23:59 +0200 Subject: [PATCH] [~] Renewed function declarations with Union type --- api_sender.html | 2 +- app.py | 3 ++- firebase_manager.py | 5 +++-- generic_executor.py | 23 ++++++++++++----------- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/api_sender.html b/api_sender.html index be7275f..05ce61b 100644 --- a/api_sender.html +++ b/api_sender.html @@ -100,7 +100,7 @@ document.addEventListener('DOMContentLoaded', () => { }); function sendRequest(endpoint, payload) { - return fetch(`https://www.servii.fr/api/${endpoint}`, { + return fetch(`http://127.0.0.1:3000/${endpoint}`, { method: 'POST', headers: { 'Content-Type': 'application/json' diff --git a/app.py b/app.py index d441f43..8cc2587 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,6 @@ import http import inspect +from typing import Union from flask import (Blueprint, Flask, Response, jsonify, request) from flask_cors import CORS @@ -62,7 +63,7 @@ def authenticate_request(data: dict): return user -def parse_and_validate_request(parameters: [str]) -> list[str] or None: +def parse_and_validate_request(parameters: list[str]) -> Union[list[str], None]: args = [] data = request.get_json() if not data: diff --git a/firebase_manager.py b/firebase_manager.py index 7adc87a..1e0e31b 100644 --- a/firebase_manager.py +++ b/firebase_manager.py @@ -1,4 +1,5 @@ from datetime import datetime +from typing import Union import firebase_admin import jwt @@ -36,7 +37,7 @@ def verify_jwt_token(token): return False, None -def fetch_port() -> int or None: +def fetch_port() -> Union[int, None]: servers_ref = firestore_database.collection("users") query = servers_ref.order_by("port", direction="DESCENDING").limit(1) highest_port_doc = next(query.stream(), None) @@ -73,7 +74,7 @@ def get_user_field(user_id, field_name): return None -def get_server_port(user_id: str) -> int or None: +def get_server_port(user_id: str) -> Union[int, None]: try: servers_ref = firestore_database.collection('users').document(user_id) server_doc = servers_ref.get() diff --git a/generic_executor.py b/generic_executor.py index 284564c..e2dbb11 100644 --- a/generic_executor.py +++ b/generic_executor.py @@ -1,4 +1,5 @@ from http import HTTPStatus +from typing import Union from cloudflare.types.dns import SRVRecord from firebase_admin.auth import UserRecord @@ -11,7 +12,7 @@ from server_mc_manager import MinecraftServerManager mc_manager: MinecraftServerManager = MinecraftServerManager() -def set_subdomain(user: UserRecord, subdomain: str) -> tuple[HTTPStatus, str or None]: +def set_subdomain(user: UserRecord, subdomain: str) -> tuple[HTTPStatus, Union[str, None]]: user_id: str = user.uid store = firebase_manager.firestore_database _users = store.collection("users") @@ -42,7 +43,7 @@ def set_subdomain(user: UserRecord, subdomain: str) -> tuple[HTTPStatus, str or return HTTPStatus.FORBIDDEN, str(e) -def fetch_servers(user: UserRecord) -> tuple[HTTPStatus, str or list]: +def fetch_servers(user: UserRecord) -> tuple[HTTPStatus, Union[str, list]]: user_id: str = user.uid server_ref = firebase_manager.firestore_database.collection('users').document(user_id).collection('servers') try: @@ -56,7 +57,7 @@ def fetch_servers(user: UserRecord) -> tuple[HTTPStatus, str or list]: return account_create(user) -def account_create(user: UserRecord) -> tuple[HTTPStatus, str or None]: +def account_create(user: UserRecord) -> tuple[HTTPStatus, Union[str, None]]: if firebase_manager.user_field_exists(user.uid): return HTTPStatus.FORBIDDEN, "User already exists." try: @@ -78,7 +79,7 @@ allowed_frameworks: list[str] = ["paper", "spigot", "bukkit"] def server_create(user: UserRecord, name: str, version: str, framework: str = "paper") -> ( - tuple)[HTTPStatus, str or None]: + tuple)[HTTPStatus, Union[str, None]]: if framework not in allowed_frameworks: return HTTPStatus.METHOD_NOT_ALLOWED, f"Framework {framework} not recognized." user_id = user.uid @@ -105,7 +106,7 @@ def server_create(user: UserRecord, name: str, version: str, framework: str = "p return HTTPStatus.INTERNAL_SERVER_ERROR, f"Server creation failed | {e}" -def server_delete(name: str, user: UserRecord) -> tuple[HTTPStatus, str or None]: +def server_delete(name: str, user: UserRecord) -> tuple[HTTPStatus, Union[str, None]]: user_id = user.uid try: port: int = firebase_manager.get_server_port(user_id) @@ -122,7 +123,7 @@ def server_delete(name: str, user: UserRecord) -> tuple[HTTPStatus, str or None] return HTTPStatus.INTERNAL_SERVER_ERROR, None -def account_delete(user: UserRecord) -> tuple[HTTPStatus, str or None]: +def account_delete(user: UserRecord) -> tuple[HTTPStatus, Union[str, None]]: user_id = user.uid try: dns_record_id = firebase_manager.get_user_field(user_id, "cloudflare_entry") @@ -143,7 +144,7 @@ def account_delete(user: UserRecord) -> tuple[HTTPStatus, str or None]: return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error deleting user '{user_id}' on the server." -def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]: +def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[str, None]]: user_id = user.uid mc_manager.set_cooldown(user_id=user_id) try: @@ -163,7 +164,7 @@ def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]: return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error when running server: {e}" -def server_stop(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]: +def server_stop(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[str, None]]: port = None user_id: str = user.uid if mc_manager.has_cooldown(user_id=user_id): @@ -187,7 +188,7 @@ def server_stop(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]: return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error occurred when stopping server '{name}'." -def update_property(uid: str, name: str, prop: str, value: str) -> tuple[HTTPStatus, str or None]: +def update_property(uid: str, name: str, prop: str, value: str) -> tuple[HTTPStatus, Union[str, None]]: property_file_path: str = f"users/{uid}/{name}/server.properties" try: file_manager.update_server_property(property_file_path, prop, value) @@ -203,7 +204,7 @@ def update_property(uid: str, name: str, prop: str, value: str) -> tuple[HTTPSta return HTTPStatus.INTERNAL_SERVER_ERROR, f"Unhandled error: {type(e).__name__}, {str(e)}" -def update_properties(user: UserRecord, name: str, props: list[tuple[str, str]]) -> tuple[HTTPStatus, str or None]: +def update_properties(user: UserRecord, name: str, props: list[tuple[str, str]]) -> tuple[HTTPStatus, Union[str, None]]: errors: list[str] = [] for prop, value in props: if prop not in MinecraftServerManager.allowed_properties: @@ -216,7 +217,7 @@ def update_properties(user: UserRecord, name: str, props: list[tuple[str, str]]) return HTTPStatus.OK, f"Successfully updated server '{name}'." -def run_command(user: UserRecord, command: str, name: str) -> tuple[HTTPStatus, str or None]: +def run_command(user: UserRecord, command: str, name: str) -> tuple[HTTPStatus, Union[str, None]]: try: port = firebase_manager.get_server_port(user.uid) if port is None: