from django.views.generic import View from django.http import JsonResponse import GeoffreyApp.api.commands as commands from GeoffreyApp.api.discord_bot.BotErrors import * def run_command(request, command, command_list): command = command.lower() response = [] try: for c in command_list: if command == c.__name__: ret = c(**request.dict()) response.append(ret) return JsonResponse(response, safe=False) raise CommandNotFound except Exception as e: response.append({"error": e.__class__.__name__}) return JsonResponse(response, safe=False) class CommandAPI(View): def get(self, request, command): get = request.GET return run_command(get, command, commands.get_list) def post(self, request, command): post = request.POST return run_command(post, command, commands.post_list) def delete(self, request, command): delete = request.DELETE return run_command(delete, command, commands.delete_list)