Commit a740e8cf authored by Ristylou Dolar's avatar Ristylou Dolar

Added status response in utils, CRUD for services and endpoint models

parent f7da0abd
...@@ -123,3 +123,49 @@ class APIEndpointSerializer(serializers.ModelSerializer): ...@@ -123,3 +123,49 @@ class APIEndpointSerializer(serializers.ModelSerializer):
'http_method', 'endpoint_url', 'is_need_auth', 'is_active', 'created_at', 'updated_at', 'deleted_at' 'http_method', 'endpoint_url', 'is_need_auth', 'is_active', 'created_at', 'updated_at', 'deleted_at'
) )
read_only_fields = ('id', 'api_endpoint_no', 'created_at', 'updated_at', 'deleted_at') read_only_fields = ('id', 'api_endpoint_no', 'created_at', 'updated_at', 'deleted_at')
def is_valid(self, raise_exception=False):
assert not hasattr(self, 'restore_object'), (
'Serializer `%s.%s` has old-style version 2 `.restore_object()` '
'that is no longer compatible with REST framework 3. '
'Use the new-style `.create()` and `.update()` methods instead.' %
(self.__class__.__module__, self.__class__.__name__)
)
assert hasattr(self, 'initial_data'), (
'Cannot call `.is_valid()` as no `data=` keyword argument was '
'passed when instantiating the serializer instance.'
)
if not hasattr(self, '_validated_data'):
try:
self._validated_data = self.run_validation(self.initial_data)
except ValidationError as exc:
self._validated_data = {}
self._errors = exc.detail
else:
self._errors = {}
#if validation failed
if self._errors and raise_exception:
error_message = {}
message = str(self.errors)
for k, v in self.errors.items():
message = str(v)
start = message.find('string=') + 8
end = message.find(', code=') - 1
message = message[start:end]
error_message[str(k)] = message
raise BadRequestException(error_message)
return not bool(self._errors)
def create(self, validated_data):
new_endpoint = APIEndpoint.objects.create(**validated_data)
new_endpoint.api_endpoint_no = number_generator('ENP', new_endpoint.id)
new_endpoint.save()
return new_endpoint
...@@ -148,3 +148,14 @@ def number_generator(prefix, id): ...@@ -148,3 +148,14 @@ def number_generator(prefix, id):
autogenerated_no = prefix + '-' + date + '-' + id_num autogenerated_no = prefix + '-' + date + '-' + id_num
return autogenerated_no return autogenerated_no
# status message
def status_message_response(code, status, message, results):
message = {
'code': code,
'status': status,
'message': message,
'results': results
}
return message
...@@ -7,7 +7,7 @@ from .models import APIService, APIEndpoint, Application ...@@ -7,7 +7,7 @@ from .models import APIService, APIEndpoint, Application
from .serializers import ( from .serializers import (
APIServiceSerializer, APIEndpointSerializer, ApplicationSerializer APIServiceSerializer, APIEndpointSerializer, ApplicationSerializer
) )
from .utils import (APIEndpointFilter, Helper, CustomPagination, BadRequestException) from .utils import (APIEndpointFilter, Helper, CustomPagination, BadRequestException, status_message_response)
...@@ -27,43 +27,23 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -27,43 +27,23 @@ class ApplicationViewSet(viewsets.ModelViewSet):
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
self.perform_create(serializer) self.perform_create(serializer)
headers = self.get_success_headers(serializer.data) headers = self.get_success_headers(serializer.data)
message = { message = status_message_response(201, 'success', 'New application created', serializer.data)
'code': 201,
'status': 'success',
'message': 'New application created',
'results': serializer.data
}
return Response(message) return Response(message)
except BadRequestException as e: except BadRequestException as e:
message = { message = status_message_response(400, 'failed', str(e), None)
'code': 400,
'status': 'failed',
'message': '' + str(e),
}
return Response(message, status=status.HTTP_400_BAD_REQUEST) return Response(message, status=status.HTTP_400_BAD_REQUEST)
except Exception as e: except Exception as e:
message = { message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SHOW LIST of Applications # SHOW LIST of Applications
def list(self, request, *args, **kwargs): def list(self, request, *args, **kwargs):
try: try:
queryset = Application.objects.filter(deleted_at__exact=None) queryset = Application.objects.filter(deleted_at__exact=None)
if not queryset.exists():
message = {
'code': 404,
'status': 'failed',
'message': 'No records found'
}
return Response(message, status=status.HTTP_404_NOT_FOUND)
page = self.paginate_queryset(queryset) page = self.paginate_queryset(queryset)
if page is not None: if page is not None:
serializer = self.get_serializer(page, many=True) serializer = self.get_serializer(page, many=True)
...@@ -76,11 +56,7 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -76,11 +56,7 @@ class ApplicationViewSet(viewsets.ModelViewSet):
return self.get_paginated_response(message) return self.get_paginated_response(message)
except Exception as e: except Exception as e:
message = { message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SHOW application details # SHOW application details
...@@ -89,28 +65,16 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -89,28 +65,16 @@ class ApplicationViewSet(viewsets.ModelViewSet):
id = self.kwargs['pk'] id = self.kwargs['pk']
queryset = Application.objects.filter(id=id) queryset = Application.objects.filter(id=id)
serializer = self.get_serializer(queryset, many=True) serializer = self.get_serializer(queryset, many=True)
if not queryset.exists(): if not queryset.exists():
message = { message = status_message_response(404, 'failed', 'No record found', None)
'code': 404,
'status': 'failed',
'message': 'No records found'
}
return Response(message, status=status.HTTP_404_NOT_FOUND) return Response(message, status=status.HTTP_404_NOT_FOUND)
else: else:
message = { message = status_message_response(200, 'success', 'Application retrieved', serializer.data)
'code': 200,
'status': 'success',
'message': 'Application retrieved',
'results': serializer.data
}
return Response(message, status=status.HTTP_200_OK) return Response(message, status=status.HTTP_200_OK)
except Exception as e: except Exception as e:
message = { message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# UPDATE Application # UPDATE Application
...@@ -123,21 +87,12 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -123,21 +87,12 @@ class ApplicationViewSet(viewsets.ModelViewSet):
self.perform_update(serializer) self.perform_update(serializer)
if getattr(instance, '_prefetched_objects_cache', None): if getattr(instance, '_prefetched_objects_cache', None):
instance._prefetched_objects_cache = {} instance._prefetched_objects_cache = {}
message = {
'code': 200,
'status': 'success',
'message': 'Application updated',
'results': serializer.data
}
message = status_message_response(200, 'success', 'Application updated', serializer.data)
return Response(message) return Response(message)
except Exception as e: except Exception as e:
message = { message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SOFT DELETE / ARCHIVED # SOFT DELETE / ARCHIVED
...@@ -145,19 +100,12 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -145,19 +100,12 @@ class ApplicationViewSet(viewsets.ModelViewSet):
try: try:
instance = self.get_object() instance = self.get_object()
self.perform_destroy(instance) self.perform_destroy(instance)
message = {
'code': 200, message = status_message_response(200, 'success', 'Application deleted', None)
'status': 'Success',
'message': 'Application deleted'
}
return Response(message, status=status.HTTP_200_OK) return Response(message, status=status.HTTP_200_OK)
except Exception as e: except Exception as e:
message = { message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
...@@ -168,22 +116,12 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -168,22 +116,12 @@ class ApplicationViewSet(viewsets.ModelViewSet):
instance = self.get_object() instance = self.get_object()
instance.deleted_at = None instance.deleted_at = None
serializer = self.get_serializer(instance) serializer = self.get_serializer(instance)
message = status_message_response(200, 'success', 'Archived application restored', serializer.data)
message = {
'code': 200,
'status': 'success',
'message': 'Archived application restored',
'results': serializer.data
}
instance.save() instance.save()
return Response(message) return Response(message)
except Exception as e: except Exception as e:
message = { message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
...@@ -192,15 +130,7 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -192,15 +130,7 @@ class ApplicationViewSet(viewsets.ModelViewSet):
def archived(self, request, pk=None): def archived(self, request, pk=None):
try: try:
queryset = Application.objects.filter(deleted_at__isnull=False) queryset = Application.objects.filter(deleted_at__isnull=False)
if not queryset.exists():
message = {
'code': 404,
'status': 'failed',
'message': 'No records found'
}
return Response(message, status=status.HTTP_404_NOT_FOUND)
page = self.paginate_queryset(queryset) page = self.paginate_queryset(queryset)
if page is not None: if page is not None:
serializer = self.get_serializer(page, many=True) serializer = self.get_serializer(page, many=True)
...@@ -213,11 +143,7 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -213,11 +143,7 @@ class ApplicationViewSet(viewsets.ModelViewSet):
return self.get_paginated_response(message) return self.get_paginated_response(message)
except Exception as e: except Exception as e:
message = { message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# Show app services # Show app services
...@@ -226,34 +152,19 @@ class ApplicationViewSet(viewsets.ModelViewSet): ...@@ -226,34 +152,19 @@ class ApplicationViewSet(viewsets.ModelViewSet):
try: try:
services = APIService.objects.filter(application=pk) services = APIService.objects.filter(application=pk)
if not services.exists():
message = {
'code': 404,
'status': 'failed',
'message': 'No records found'
}
return Response(message, status=status.HTTP_404_NOT_FOUND)
page = self.paginate_queryset(services) page = self.paginate_queryset(services)
if page is not None: if page is not None:
serializer = APIServiceSerializer(page, many=True) serializer = APIServiceSerializer(page, many=True)
message = { message = {
'code': 200, 'code': 200,
'status': 'success', 'status': 'success',
'message': 'Archived applications found', 'message': 'Application services found',
'results': serializer.data 'results': serializer.data
} }
return self.get_paginated_response(message) return self.get_paginated_response(message)
# serializer = APIServiceSerializer(services, many=True)
# return Response(serializer.data, status=status.HTTP_200_OK)
except Exception as e: except Exception as e:
message = { message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
...@@ -266,39 +177,26 @@ class APIServiceViewSet(viewsets.ModelViewSet): ...@@ -266,39 +177,26 @@ class APIServiceViewSet(viewsets.ModelViewSet):
lookup_field = 'pk' lookup_field = 'pk'
pagination_class = CustomPagination pagination_class = CustomPagination
# CREATE Application # CREATE Service
def create(self, request, *args, **kwargs): def create(self, request, *args, **kwargs):
try: try:
serializer = self.get_serializer(data=request.data) serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
self.perform_create(serializer) self.perform_create(serializer)
headers = self.get_success_headers(serializer.data) headers = self.get_success_headers(serializer.data)
message = { message = status_message_response(201, 'success', 'New service created', serializer.data)
'code': 201,
'status': 'success',
'message': 'New service created',
'results': serializer.data
}
return Response(message) return Response(message)
except BadRequestException as e: except BadRequestException as e:
message = { message = status_message_response(400, 'failed', str(e), None)
'code': 400,
'status': 'failed',
'message': '' + str(e),
}
return Response(message, status=status.HTTP_400_BAD_REQUEST) return Response(message, status=status.HTTP_400_BAD_REQUEST)
except Exception as e: except Exception as e:
message = { message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SHOW LIST of Applications # SHOW LIST of services
def list(self, request, *args, **kwargs): def list(self, request, *args, **kwargs):
try: try:
queryset = APIService.objects.filter(deleted_at__exact=None) queryset = APIService.objects.filter(deleted_at__exact=None)
...@@ -322,44 +220,28 @@ class APIServiceViewSet(viewsets.ModelViewSet): ...@@ -322,44 +220,28 @@ class APIServiceViewSet(viewsets.ModelViewSet):
return self.get_paginated_response(message) return self.get_paginated_response(message)
except Exception as e: except Exception as e:
message = { message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SHOW application details # SHOW service details
def retrieve(self, request, *args, **kwargs): def retrieve(self, request, *args, **kwargs):
try: try:
id = self.kwargs['pk'] id = self.kwargs['pk']
queryset = APIService.objects.filter(id=id) queryset = APIService.objects.filter(id=id)
serializer = self.get_serializer(queryset, many=True) serializer = self.get_serializer(queryset, many=True)
if not queryset.exists(): if not queryset.exists():
message = { message = status_message_response(404, 'failed', 'No record found', None)
'code': 404,
'status': 'failed',
'message': 'No records found'
}
return Response(message, status=status.HTTP_404_NOT_FOUND) return Response(message, status=status.HTTP_404_NOT_FOUND)
else: else:
message = { message = status_message_response(200, 'success', 'Service retrieved', serializer.data)
'code': 200,
'status': 'success',
'message': 'Service retrieved',
'results': serializer.data
}
return Response(message, status=status.HTTP_200_OK) return Response(message, status=status.HTTP_200_OK)
except Exception as e: except Exception as e:
message = { message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# UPDATE Application # UPDATE service
def update(self, request, *args, **kwargs): def update(self, request, *args, **kwargs):
try: try:
partial = kwargs.pop('partial', False) partial = kwargs.pop('partial', False)
...@@ -369,21 +251,12 @@ class APIServiceViewSet(viewsets.ModelViewSet): ...@@ -369,21 +251,12 @@ class APIServiceViewSet(viewsets.ModelViewSet):
self.perform_update(serializer) self.perform_update(serializer)
if getattr(instance, '_prefetched_objects_cache', None): if getattr(instance, '_prefetched_objects_cache', None):
instance._prefetched_objects_cache = {} instance._prefetched_objects_cache = {}
message = {
'code': 200, message = status_message_response(200, 'success', 'Service updated', serializer.data)
'status': 'success',
'message': 'Application updated',
'results': serializer.data
}
return Response(message) return Response(message)
except Exception as e: except Exception as e:
message = { message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SOFT DELETE / ARCHIVED # SOFT DELETE / ARCHIVED
...@@ -391,62 +264,37 @@ class APIServiceViewSet(viewsets.ModelViewSet): ...@@ -391,62 +264,37 @@ class APIServiceViewSet(viewsets.ModelViewSet):
try: try:
instance = self.get_object() instance = self.get_object()
self.perform_destroy(instance) self.perform_destroy(instance)
message = {
'code': 200, message = status_message_response(200, 'success', 'Service deleted', None)
'status': 'Success',
'message': 'Service deleted'
}
return Response(message, status=status.HTTP_200_OK) return Response(message, status=status.HTTP_200_OK)
except Exception as e: except Exception as e:
message = { message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# PATCH - RESTORE archived application # PATCH - RESTORE archived service
def partial_update(self, request, *args, **kwargs): def partial_update(self, request, *args, **kwargs):
try: try:
kwargs['partial'] = True kwargs['partial'] = True
instance = self.get_object() instance = self.get_object()
instance.deleted_at = None instance.deleted_at = None
serializer = self.get_serializer(instance) serializer = self.get_serializer(instance)
message = status_message_response(200, 'success', 'Archived service restored', serializer.data)
message = {
'code': 200,
'status': 'success',
'message': 'Archived service restored',
'results': serializer.data
}
instance.save() instance.save()
return Response(message) return Response(message)
except Exception as e: except Exception as e:
message = { message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# /archived - show list of archived application # /archived - show list of archived services
@action(methods=["GET"], detail=False) @action(methods=["GET"], detail=False)
def archived(self, request, pk=None): def archived(self, request, pk=None):
try: try:
queryset = APIService.objects.filter(deleted_at__isnull=False) queryset = APIService.objects.filter(deleted_at__isnull=False)
if not queryset.exists():
message = {
'code': 404,
'status': 'failed',
'message': 'No records found'
}
return Response(message, status=status.HTTP_404_NOT_FOUND)
page = self.paginate_queryset(queryset) page = self.paginate_queryset(queryset)
if page is not None: if page is not None:
serializer = self.get_serializer(page, many=True) serializer = self.get_serializer(page, many=True)
...@@ -459,30 +307,174 @@ class APIServiceViewSet(viewsets.ModelViewSet): ...@@ -459,30 +307,174 @@ class APIServiceViewSet(viewsets.ModelViewSet):
return self.get_paginated_response(message) return self.get_paginated_response(message)
except Exception as e: except Exception as e:
message = { message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# @action(methods=["GET"], detail=True) # Show service endpoints
# def endpoints(self, request, id=None): @action(methods=['GET'], detail=True)
# service = self.get_object() def endpoints(self, request, pk):
# endpoints = APIEndpoint.objects.filter(service=service) try:
# serializer = APIEndpointSerializer(endpoints, many=True) endpoints = APIEndpoint.objects.filter(service=pk)
# return Response(serializer.data, status=status.HTTP_200_OK)
page = self.paginate_queryset(endpoints)
if page is not None:
serializer = APIEndpointSerializer(page, many=True)
message = {
'code': 200,
'status': 'success',
'message': 'Service endpoints found',
'results': serializer.data
}
return self.get_paginated_response(message)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
class APIEndpointViewSet(viewsets.ModelViewSet): class APIEndpointViewSet(viewsets.ModelViewSet):
http_method_names = [ http_method_names = [
'get', 'post', 'put', 'patch', 'head', 'options', 'trace' 'get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'
] ]
queryset = APIEndpoint.objects.all().order_by('service') queryset = APIEndpoint.objects.all().order_by('service')
serializer_class = APIEndpointSerializer serializer_class = APIEndpointSerializer
lookup_field = 'id' lookup_field = 'pk'
filter_backends = (DjangoFilterBackend, ) filter_backends = (DjangoFilterBackend, )
filter_class = APIEndpointFilter filter_class = APIEndpointFilter
pagination_class = CustomPagination
# CREATE Endpoint
def create(self, request, *args, **kwargs):
try:
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
message = status_message_response(201, 'success', 'New endpoint created', serializer.data)
return Response(message)
except BadRequestException as e:
message = status_message_response(400, 'failed', str(e), None)
return Response(message, status=status.HTTP_400_BAD_REQUEST)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SHOW LIST of endpoints
def list(self, request, *args, **kwargs):
try:
queryset = APIEndpoint.objects.filter(deleted_at__exact=None)
if not queryset.exists():
message = {
'code': 404,
'status': 'failed',
'message': 'No records found'
}
return Response(message, status=status.HTTP_404_NOT_FOUND)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
message = {
'code': 200,
'status': 'success',
'message': 'List of endpoints found',
'results': serializer.data
}
return self.get_paginated_response(message)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SHOW endpoint details
def retrieve(self, request, *args, **kwargs):
try:
id = self.kwargs['pk']
queryset = APIEndpoint.objects.filter(id=id)
serializer = self.get_serializer(queryset, many=True)
if not queryset.exists():
message = status_message_response(404, 'failed', 'No record found', None)
return Response(message, status=status.HTTP_404_NOT_FOUND)
else:
message = status_message_response(200, 'success', 'Endpoint retrieved', serializer.data)
return Response(message, status=status.HTTP_200_OK)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# UPDATE endpoint
def update(self, request, *args, **kwargs):
try:
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
if getattr(instance, '_prefetched_objects_cache', None):
instance._prefetched_objects_cache = {}
message = status_message_response(200, 'success', 'Endpoint updated', serializer.data)
return Response(message)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SOFT DELETE / ARCHIVED
def destroy(self, request, *args, **kwargs):
try:
instance = self.get_object()
self.perform_destroy(instance)
message = status_message_response(200, 'success', 'Endpoint deleted', None)
return Response(message, status=status.HTTP_200_OK)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# PATCH - RESTORE archived endpoint
def partial_update(self, request, *args, **kwargs):
try:
kwargs['partial'] = True
instance = self.get_object()
instance.deleted_at = None
serializer = self.get_serializer(instance)
message = status_message_response(200, 'success', 'Archived endpoint restored', serializer.data)
instance.save()
return Response(message)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# /archived - show list of archived endpoints
@action(methods=["GET"], detail=False)
def archived(self, request, pk=None):
try:
queryset = APIEndpoint.objects.filter(deleted_at__isnull=False)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
message = {
'code': 200,
'status': 'success',
'message': 'Archived endpoints found',
'results': serializer.data
}
return self.get_paginated_response(message)
except Exception as e:
message = status_message_response(500, 'failed', 'Request was not able to process' + str(e), None)
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
class APIGatewayList(APIView): class APIGatewayList(APIView):
......
...@@ -20,5 +20,5 @@ swagger = get_swagger_view(title='API Main End Point') ...@@ -20,5 +20,5 @@ swagger = get_swagger_view(title='API Main End Point')
urlpatterns = [ urlpatterns = [
path('docs/', swagger), path('docs/', swagger),
path('', include('api.urls')), path('api/v1/', include('api.urls')),
] ]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment