my_api/rest/views.py
import json
from django.conf import settings
from django.http import JsonResponse
from rest_framework.decorators import api_view
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from docs.version import __version__
from my_api.context_processors import check_authorization_user
from my_api.rest.serializers import FileToFilesystemSerializer
"""
Official drf doc here:
https://www.django-rest-framework.org/api-guide/requests/#authenticators
"""
@api_view(['GET'])
def status_api(request):
"""
For Icinga2 or Nagios. Function based.
Return 'status': 'OK' and the current version running.
"""
if request.method == 'GET':
return JsonResponse({
'status': 'OK',
'version': __version__
}, status=200)
class Icinga2API(APIView):
"""
For Icinga2 or Nagios. Class based.
Return 'status': 'OK' and the current version running.
"""
def get(self, request):
return Response({
'status': 'OK',
'version': __version__
}, status=200)
class FileAPI(APIView):
"""
Example API to push a json image on the server and get it by a key.
"""
# def delete(self, request):
# """
# Required that the client is authenticated,
# This method delete a file on the disk (NotImplemented)
# :param request: the key corresponding to that file.
# :return: status
# """
# raise NotImplemented()
def get(self, request):
"""
Required that the client is authenticated,
This method load a file from the disk to the json send it in the
response
:param request: the key corresponding to that file.
:return: the file in json Base64 format
"""
# http://www.django-rest-framework.org/api-guide/requests/#query_params
if 'name' not in request.query_params \
or not request.query_params['name']:
return Response({'error': 'file Not found'}, status=422)
else:
name = request.query_params['name']
with open(settings.PATH_TO_STORE_FILE + name + '.json') as f:
json_file = json.load(f)
return Response(json_file, status=200)
@check_authorization_user()
def post(self, request):
"""
Required that the client is authenticated,
This method load a file from the json and save it on the disk
:param request: the file in json Base64 format
:return: the key to get to that file after.
"""
serializer = FileToFilesystemSerializer(
path=settings.PATH_TO_STORE_FILE, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.instance,
status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
# def put(self, request):
# """
# Required that the client is authenticated,
# This method load a file from the json and update it on the disk
# (NotImplemented)
# :param request: the key and the new file in json Base64 format
# :return: the key to get to that file after.
# """
# raise NotImplemented()
def api_handler400(request, *args, **kwargs):
data = {
'error': 'That\'s a BAD REQUEST, try again (400)'
}
return JsonResponse(data, status=status.HTTP_400_BAD_REQUEST)
def api_handler403(request, *args, **kwargs):
data = {
'error': 'This is FORBIDDEN, you certainly forgot to take the right Token (403)'
}
return JsonResponse(data, status=status.HTTP_403_FORBIDDEN)
def api_handler404(request, *args, **kwargs):
data = {
'error': 'I do NOT FOUND it! sorry ...(404)'
}
return JsonResponse(data, status=status.HTTP_404_NOT_FOUND)
def api_handler500(request, *args, **kwargs):
data = {
'error': 'INTERNAL SERVER ERROR, Allo Huston we got a problem! (500)'
}
return JsonResponse(data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)