Исправление мелких ошибок, добавление http error code к ответу (400 если проблема с параметрами)
This commit is contained in:
parent
e3aae67375
commit
24fbf1287b
@ -3,6 +3,8 @@ import time
|
||||
from datetime import date as dt
|
||||
import traceback
|
||||
|
||||
from django.http import HttpResponseNotFound
|
||||
|
||||
from .api_media_utils import *
|
||||
from .api_utils import *
|
||||
from .models import *
|
||||
@ -708,7 +710,9 @@ class ApiMedia:
|
||||
return res
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
return make_error_object(Exception(API_ERROR_NOT_FOUND, "object in storage not found"))
|
||||
|
||||
# return make_error_object(Exception(API_ERROR_NOT_FOUND, "object in storage not found"))
|
||||
return HttpResponseNotFound()
|
||||
|
||||
@staticmethod
|
||||
@api_method("media.list",
|
||||
|
@ -1,4 +1,6 @@
|
||||
from django.http import HttpResponse
|
||||
import json
|
||||
|
||||
from django.http import *
|
||||
from .api_params import *
|
||||
|
||||
|
||||
@ -24,6 +26,13 @@ def api_make_response(response):
|
||||
# )
|
||||
|
||||
|
||||
def default_serializer(obj):
|
||||
try:
|
||||
return obj.to_json()
|
||||
except Exception:
|
||||
return str(obj)
|
||||
|
||||
|
||||
def api_method(func_name, doc="", params: list or None = None, returns=""):
|
||||
"""
|
||||
Декоратор для методов API, автоматически валидирует и передает параметры методам
|
||||
@ -56,9 +65,12 @@ def api_method(func_name, doc="", params: list or None = None, returns=""):
|
||||
# print(f"errors: {errors}, args: {func_args}")
|
||||
if len(errors) > 0:
|
||||
if len(errors) == 1:
|
||||
return make_error_object(errors[0])
|
||||
obj = make_error_object(errors[0])
|
||||
else:
|
||||
return make_error_object(errors)
|
||||
obj = make_error_object(errors)
|
||||
response = HttpResponseBadRequest(json.dumps(obj, default=default_serializer, ensure_ascii=False, indent=4))
|
||||
response.headers["Content-type"] = "application/json; charset=utf-8"
|
||||
return response
|
||||
else:
|
||||
try:
|
||||
if asyncio.iscoroutinefunction(func):
|
||||
@ -69,10 +81,16 @@ def api_method(func_name, doc="", params: list or None = None, returns=""):
|
||||
return make_error_object(ex)
|
||||
|
||||
if out is None:
|
||||
return make_error_object(Exception(API_ERROR_INTERNAL_ERROR, "method returned null object"))
|
||||
obj = make_error_object(Exception(API_ERROR_INTERNAL_ERROR, "method returned null object"))
|
||||
response = HttpResponseServerError(json.dumps(obj, default=default_serializer, ensure_ascii=False, indent=4))
|
||||
response.headers["Content-type"] = "application/json; charset=utf-8"
|
||||
return response
|
||||
|
||||
if not isinstance(out, dict) and not isinstance(out, HttpResponse):
|
||||
return make_error_object(Exception(API_ERROR_INTERNAL_ERROR, "method returned invalid object type"))
|
||||
obj = make_error_object(Exception(API_ERROR_INTERNAL_ERROR, "method returned invalid object type"))
|
||||
response = HttpResponseServerError(json.dumps(obj, default=default_serializer, ensure_ascii=False, indent=4))
|
||||
response.headers["Content-type"] = "application/json; charset=utf-8"
|
||||
return response
|
||||
|
||||
return out
|
||||
|
||||
|
10
api/views.py
10
api/views.py
@ -4,6 +4,7 @@ from django.http import HttpResponse, HttpResponseBadRequest
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
from .api_methods import api_call_method, api_get_documentation
|
||||
from .api_utils import default_serializer
|
||||
|
||||
|
||||
def view_methods(request):
|
||||
@ -11,13 +12,6 @@ def view_methods(request):
|
||||
return render(request, 'index.html', {'api_methods': methods})
|
||||
|
||||
|
||||
def _default_serializer(obj):
|
||||
try:
|
||||
return obj.to_json()
|
||||
except Exception:
|
||||
return str(obj)
|
||||
|
||||
|
||||
async def call_method(request, method_name):
|
||||
if request.method == "GET":
|
||||
params = request.GET
|
||||
@ -33,7 +27,7 @@ async def call_method(request, method_name):
|
||||
out = await api_call_method(request, method_name, api_params)
|
||||
|
||||
if isinstance(out, dict):
|
||||
response = HttpResponse(json.dumps(out, default=_default_serializer, ensure_ascii=False, indent=4))
|
||||
response = HttpResponse(json.dumps(out, default=default_serializer, ensure_ascii=False, indent=4))
|
||||
response.headers["Content-type"] = "application/json; charset=utf-8"
|
||||
return response
|
||||
else:
|
||||
|
Reference in New Issue
Block a user