Добавлены классы ответов
This commit is contained in:
16
net/xeaf/rack/models/responses/__init__.py
Normal file
16
net/xeaf/rack/models/responses/__init__.py
Normal file
@@ -0,0 +1,16 @@
|
||||
# DRF Rack
|
||||
# Библиотека классов расширений для Django REST Framework
|
||||
#
|
||||
# Автор: Николай В. Анохин <n.anokhin@xeaf.net>
|
||||
# Все права защищены. Лицензия: MIT
|
||||
|
||||
"""
|
||||
Пакет описания классов моделей ответов по протоколу HTTP
|
||||
"""
|
||||
|
||||
from .bool_response import BoolResponse
|
||||
from .data_response import DataResponse
|
||||
from .empty_response import EmptyResponse
|
||||
from .error_response import ErrorResponse
|
||||
from .exception_response import ExceptionResponse
|
||||
from .list_response import ListResponse
|
||||
33
net/xeaf/rack/models/responses/bool_response.py
Normal file
33
net/xeaf/rack/models/responses/bool_response.py
Normal file
@@ -0,0 +1,33 @@
|
||||
# DRF Rack
|
||||
# Библиотека классов расширений для Django REST Framework
|
||||
#
|
||||
# Автор: Николай В. Анохин <n.anokhin@xeaf.net>
|
||||
# Все права защищены. Лицензия: MIT
|
||||
|
||||
"""
|
||||
Описание класса BoolResponse
|
||||
"""
|
||||
|
||||
from rest_framework import status
|
||||
|
||||
from net.xeaf.rack.core import CoreResponse
|
||||
|
||||
|
||||
class BoolResponse(CoreResponse):
|
||||
"""
|
||||
Ответ с логическим значением
|
||||
"""
|
||||
|
||||
def __init__(self, value: bool, meta: dict | None = None):
|
||||
"""
|
||||
Инициализация
|
||||
|
||||
:param value: Логическое значение
|
||||
:param meta: Дополнительная информация
|
||||
"""
|
||||
|
||||
super(BoolResponse, self).__init__(
|
||||
internal_status=status.HTTP_200_OK,
|
||||
external_status=status.HTTP_200_OK,
|
||||
data=value,
|
||||
meta=meta)
|
||||
33
net/xeaf/rack/models/responses/data_response.py
Normal file
33
net/xeaf/rack/models/responses/data_response.py
Normal file
@@ -0,0 +1,33 @@
|
||||
# DRF Rack
|
||||
# Библиотека классов расширений для Django REST Framework
|
||||
#
|
||||
# Автор: Николай В. Анохин <n.anokhin@xeaf.net>
|
||||
# Все права защищены. Лицензия: MIT
|
||||
|
||||
"""
|
||||
Описание класса DataResponse
|
||||
"""
|
||||
|
||||
from rest_framework import status
|
||||
|
||||
from net.xeaf.rack.core import CoreResponse
|
||||
|
||||
|
||||
class DataResponse(CoreResponse):
|
||||
"""
|
||||
Ответ с одним объектом данных
|
||||
"""
|
||||
|
||||
def __init__(self, data: dict, meta: dict | None = None):
|
||||
"""
|
||||
Инициализация
|
||||
|
||||
:param data: Объект данных
|
||||
:param meta: Дополнительная информация
|
||||
"""
|
||||
|
||||
super(DataResponse, self).__init__(
|
||||
internal_status=status.HTTP_200_OK,
|
||||
external_status=status.HTTP_200_OK,
|
||||
data=data,
|
||||
meta=meta)
|
||||
30
net/xeaf/rack/models/responses/empty_response.py
Normal file
30
net/xeaf/rack/models/responses/empty_response.py
Normal file
@@ -0,0 +1,30 @@
|
||||
# DRF Rack
|
||||
# Библиотека классов расширений для Django REST Framework
|
||||
#
|
||||
# Автор: Николай В. Анохин <n.anokhin@xeaf.net>
|
||||
# Все права защищены. Лицензия: MIT
|
||||
|
||||
"""
|
||||
Описание класса EmptyResponse
|
||||
"""
|
||||
|
||||
from rest_framework import status
|
||||
|
||||
from net.xeaf.rack.core import CoreResponse
|
||||
|
||||
|
||||
class EmptyResponse(CoreResponse):
|
||||
"""
|
||||
Пустой положительный ответ
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Инициализация
|
||||
"""
|
||||
|
||||
super(EmptyResponse, self).__init__(
|
||||
internal_status=status.HTTP_200_OK,
|
||||
external_status=status.HTTP_200_OK,
|
||||
data=None,
|
||||
meta=None)
|
||||
49
net/xeaf/rack/models/responses/error_response.py
Normal file
49
net/xeaf/rack/models/responses/error_response.py
Normal file
@@ -0,0 +1,49 @@
|
||||
# DRF Rack
|
||||
# Библиотека классов расширений для Django REST Framework
|
||||
#
|
||||
# Автор: Николай В. Анохин <n.anokhin@xeaf.net>
|
||||
# Все права защищены. Лицензия: MIT
|
||||
|
||||
"""
|
||||
Описание класса ErrorResponse
|
||||
"""
|
||||
|
||||
from rest_framework import status
|
||||
|
||||
from net.xeaf.rack.core import CoreResponse
|
||||
|
||||
|
||||
class ErrorResponse(CoreResponse):
|
||||
"""
|
||||
Ответ с информацией об ошибке
|
||||
"""
|
||||
|
||||
def __init__(self, status_code: int, detail: list | dict | str | bool | None, meta: dict | None = None):
|
||||
"""
|
||||
Инициализация
|
||||
|
||||
:param status_code: Код состояния
|
||||
:param detail: Информация об ошибке
|
||||
:param meta: Дополнительные данные
|
||||
"""
|
||||
|
||||
super(ErrorResponse, self).__init__(
|
||||
internal_status=status_code,
|
||||
external_status=self._calc_external_status(status_code),
|
||||
data=detail,
|
||||
meta=meta)
|
||||
|
||||
@classmethod
|
||||
def _calc_external_status(cls, status_code: int) -> int:
|
||||
"""
|
||||
Вычисляет значение внешнего возвращаемого статуса
|
||||
|
||||
:param status_code: Код состояния
|
||||
|
||||
:return: Внешний код состояния
|
||||
"""
|
||||
|
||||
if status_code < status.HTTP_500_INTERNAL_SERVER_ERROR:
|
||||
return status.HTTP_200_OK
|
||||
|
||||
return status_code
|
||||
40
net/xeaf/rack/models/responses/exception_response.py
Normal file
40
net/xeaf/rack/models/responses/exception_response.py
Normal file
@@ -0,0 +1,40 @@
|
||||
# DRF Rack
|
||||
# Библиотека классов расширений для Django REST Framework
|
||||
#
|
||||
# Автор: Николай В. Анохин <n.anokhin@xeaf.net>
|
||||
# Все права защищены. Лицензия: MIT
|
||||
|
||||
"""
|
||||
Описание класса ExceptionResponse
|
||||
"""
|
||||
|
||||
from rest_framework import status
|
||||
|
||||
from net.xeaf.rack.core import CoreException
|
||||
from net.xeaf.rack.core import CoreResponse
|
||||
|
||||
|
||||
class ExceptionResponse(CoreResponse):
|
||||
"""
|
||||
Ответ с информацией об исключении
|
||||
"""
|
||||
|
||||
def __init__(self, exc: CoreException, meta: dict | None = None):
|
||||
"""
|
||||
Инициализация
|
||||
|
||||
:param exc: Исключение
|
||||
:param meta: Дополнительная информация
|
||||
"""
|
||||
|
||||
# Проверяем требование передать реальный статус
|
||||
if exc.is_pure_status():
|
||||
external_status = exc.status_code
|
||||
else:
|
||||
external_status = status.HTTP_200_OK
|
||||
|
||||
super(ExceptionResponse, self).__init__(
|
||||
internal_status=exc.status_code,
|
||||
external_status=external_status,
|
||||
data=exc.detail,
|
||||
meta=meta)
|
||||
33
net/xeaf/rack/models/responses/list_response.py
Normal file
33
net/xeaf/rack/models/responses/list_response.py
Normal file
@@ -0,0 +1,33 @@
|
||||
# DRF Rack
|
||||
# Библиотека классов расширений для Django REST Framework
|
||||
#
|
||||
# Автор: Николай В. Анохин <n.anokhin@xeaf.net>
|
||||
# Все права защищены. Лицензия: MIT
|
||||
|
||||
"""
|
||||
Описание класса ListResponse
|
||||
"""
|
||||
|
||||
from rest_framework import status
|
||||
|
||||
from net.xeaf.rack.core import CoreResponse
|
||||
|
||||
|
||||
class ListResponse(CoreResponse):
|
||||
"""
|
||||
Ответ со списком объектов данных
|
||||
"""
|
||||
|
||||
def __init__(self, data: list, meta: dict | None = None):
|
||||
"""
|
||||
Инициализация
|
||||
|
||||
:param data: Список объектов данных
|
||||
:param meta: Дополнительная информация
|
||||
"""
|
||||
|
||||
super(ListResponse, self).__init__(
|
||||
internal_status=status.HTTP_200_OK,
|
||||
external_status=status.HTTP_200_OK,
|
||||
data=data,
|
||||
meta=meta)
|
||||
Reference in New Issue
Block a user