[~] Renewed function declarations with Union type

This commit is contained in:
Charles Le Maux 2024-07-14 19:23:59 +02:00
parent ca33732efc
commit c2f21cb1c1
4 changed files with 18 additions and 15 deletions

View File

@ -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'

3
app.py
View File

@ -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:

View File

@ -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()

View File

@ -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: