77 lines
2.1 KiB
Python
77 lines
2.1 KiB
Python
from django.views.generic import View
|
|
from django.http import JsonResponse
|
|
from django.conf import settings
|
|
import inspect
|
|
|
|
import GeoffreyApp.api.commands as commands
|
|
from GeoffreyApp.errors import *
|
|
|
|
|
|
def getRequiredArgs(func):
|
|
args = inspect.getfullargspec(func)
|
|
'''
|
|
if defaults:
|
|
args = args[:-len(kwonlyargs)]
|
|
'''
|
|
return args.args + args.kwonlyargs
|
|
|
|
|
|
GET = []
|
|
POST = []
|
|
DELETE = []
|
|
|
|
for command in commands.get_list:
|
|
GET.append({"name": command.__name__, "params": getRequiredArgs(command)})
|
|
for command in commands.post_list:
|
|
POST.append({"name": command.__name__, "params": getRequiredArgs(command)})
|
|
for command in commands.delete_list:
|
|
DELETE.append({"name": command.__name__, "params": getRequiredArgs(command)})
|
|
command_response = {"GET": GET, "POST": POST, "DELETE": DELETE}
|
|
|
|
|
|
def run_command(request, command, command_list):
|
|
command = command.lower()
|
|
try:
|
|
for c in command_list:
|
|
if command == c.__name__:
|
|
response = c(**request.dict())
|
|
return JsonResponse(response, safe=False)
|
|
|
|
raise CommandNotFound
|
|
except Exception as e:
|
|
response = {"error": e.__class__.__name__}
|
|
|
|
return JsonResponse(response, safe=False)
|
|
|
|
|
|
class CommandAPI(View):
|
|
def get(self, request, command):
|
|
get = request.GET
|
|
if command.lower() != "commands":
|
|
return run_command(get, command, commands.get_list)
|
|
else:
|
|
return JsonResponse(command_response)
|
|
|
|
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)
|
|
|
|
|
|
class SettingsAPI(View):
|
|
def get(self, request):
|
|
response = {
|
|
"BOT_PREFIX": getattr(settings, 'BOT_PREFIX', '?'),
|
|
"ERROR_USERS": getattr(settings, 'ERROR_USERS', []),
|
|
"MOD_RANK": getattr(settings, 'MOD_RANK', []),
|
|
"DEFAULT_STATUS": getattr(settings, 'DEFAULT_STATUS', 'sed')
|
|
}
|
|
|
|
return JsonResponse(response)
|
|
|
|
|
|
|