Repository URL to install this package:
|
Version:
1.0.1 ▾
|
from django.conf import settings
from django.urls.exceptions import NoReverseMatch
from rest_framework import status
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.reverse import reverse
class ResponseTypeViewHeaderMixin:
''' Adds a response type to the Header of a HTTP Response '''
DATA = 'data'
VIEW_SET = 'view'
def dispatch(self, request, *args, **kwargs):
response = super().dispatch(request, *args, **kwargs)
if hasattr(self, 'RESPONSE_TYPE'):
response['X-ResponseType'] = self.RESPONSE_TYPE
else:
response['X-ResponseType'] = self.VIEW_SET
return response
class AppModelResponseHeaderMixin:
''' Adds the app to the Header of a HTTP Response '''
def dispatch(self, request, *args, **kwargs):
response = super().dispatch(request, *args, **kwargs)
if hasattr(self, 'IDENTIFIER'):
response['X-Identifier'] = self.IDENTIFIER
return response
@api_view(["GET"])
@permission_classes([IsAuthenticated])
def api_endpoints_root(request, format=None):
try:
return Response({
wb_endpoint: reverse(
f"{wb_endpoint}:api-root", request=request, format=format
)
for wb_endpoint in settings.WB_ENDPOINTS
})
except AttributeError:
return Response({'error': 'No Endpoints specified.'}, status=status.HTTP_400_BAD_REQUEST)
except NoReverseMatch:
return Response({'error': 'No API Root specified for one app.'}, status=status.HTTP_400_BAD_REQUEST)