import logging from flask import Response from flask import blueprints from flask import request from flask_api import status from .command_execution import enqueue_command from .commands import CommandId from .commands import get_command_id_from_name from .commands import get_request_class _logger = logging.getLogger(__name__) bp = blueprints.Blueprint("command", __name__) @bp.route("//command", methods=["POST", "GET"]) def command(role: str): logger = _logger.getChild(f"{role}/command") arguments = dict(request.args) logger.debug(f"arguments: {arguments}") cmd = arguments.pop("cmd") try: command_id = CommandId(int(cmd)) except ValueError: command_id = get_command_id_from_name(cmd) try: command = get_request_class(command_id=command_id)(**arguments) except Exception: return Response(status=status.HTTP_400_BAD_REQUEST) try: enqueue_command( role=role, command=command, ) except KeyError: logger.error(f"role {role} does not exist") return Response(status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_200_OK)