Enhance API response handling and implement unified API standard across multiple modules. Added feature flags for unified exception handling and debug throttling in settings. Updated pagination and response formats in various viewsets to align with the new standard. Improved error handling and response validation in frontend components for better user feedback.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
"""
|
||||
Custom pagination class for DRF to support dynamic page_size query parameter
|
||||
and unified response format
|
||||
"""
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
from .response import get_request_id
|
||||
|
||||
|
||||
class CustomPageNumberPagination(PageNumberPagination):
|
||||
@@ -11,8 +13,37 @@ class CustomPageNumberPagination(PageNumberPagination):
|
||||
|
||||
Default page size: 10
|
||||
Max page size: 100
|
||||
|
||||
Returns unified format with success field
|
||||
"""
|
||||
page_size = 10
|
||||
page_size_query_param = 'page_size'
|
||||
max_page_size = 100
|
||||
|
||||
def paginate_queryset(self, queryset, request, view=None):
|
||||
"""
|
||||
Override to store request for later use in get_paginated_response
|
||||
"""
|
||||
self.request = request
|
||||
return super().paginate_queryset(queryset, request, view)
|
||||
|
||||
def get_paginated_response(self, data):
|
||||
"""
|
||||
Return a paginated response with unified format including success field
|
||||
"""
|
||||
from rest_framework.response import Response
|
||||
|
||||
response_data = {
|
||||
'success': True,
|
||||
'count': self.page.paginator.count,
|
||||
'next': self.get_next_link(),
|
||||
'previous': self.get_previous_link(),
|
||||
'results': data
|
||||
}
|
||||
|
||||
# Add request_id if request is available
|
||||
if hasattr(self, 'request') and self.request:
|
||||
response_data['request_id'] = get_request_id(self.request)
|
||||
|
||||
return Response(response_data)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user