Global API refactoring

This commit is contained in:
2022-10-09 13:43:06 +03:00
parent ad659b5f30
commit 47359a7932
11 changed files with 471 additions and 213 deletions

View File

@@ -3,12 +3,23 @@ import traceback
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseBadRequest
from .api_methods import api_methods
from .api_methods import api_call_method, api_get_documentation
from .api_errors import *
def view_methods(request):
return render(request, 'api/index.html', {'api_methods': api_methods})
methods = []
def __make_param(p):
return {
"name": p["name"],
"type": p["type"],
"description": p["description"],
"required": p["required"]
}
methods = api_get_documentation()
return render(request, 'api/index.html', {'api_methods': methods})
def call_method(request, method_name):
@@ -19,17 +30,8 @@ def call_method(request, method_name):
else:
return HttpResponseBadRequest()
try:
if method_name in api_methods:
out = api_methods[method_name]["func"](params)
if out is None:
raise Exception(API_ERROR_INTERNAL_ERROR, "method returned null object")
else:
raise Exception(API_ERROR_METHOD_NOT_FOUND)
except Exception as ex:
traceback.print_exc()
out = make_error_object(ex)
out = api_call_method(method_name, params)
response = HttpResponse(json.dumps(out, ensure_ascii=False))
response.headers["Content-type"] = "application/json"
response.headers["Content-type"] = "application/json; charset=utf-8"
return response