Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common JSON Format for Django Rest Framework responses for Android client

I am developing the rest apis for an android app using Djnago Rest Framework. The android guys told me that it would be nice if I could provide the responses in a common format, like

{
   “Status”:””,
   “Message”:””,
   “Data”:[]
}

Guys, is it the right approach? If so, how to achieve this? My assumption is that using a middleware would be an option.

Thanks in advance.

like image 560
Ajith k James Avatar asked Feb 23 '18 04:02

Ajith k James


2 Answers

Writing your own reponse function will not help, in case you are using class based views say APIView or generic views. I think a better way could be to write custom renderer for formatting successfull responses and custom exception handler for responses with errors. This implementation gives you a single point to control all reponses without changing your already existing views. Here is the sample code from one of my project. You need modify it as per your requirements.

Custom Renderer

#renderers.py
class CustomJSONRenderer(JSONRenderer):

def render(self, data, accepted_media_type=None, renderer_context=None):

    response_data = {'message': '', 'errors': [], 'data': data, 'status': 'success'}

    getattr(renderer_context.get('view').get_serializer().Meta,'resource_name', 'objects')

    # call super to render the response
    response = super(CustomJSONRenderer, self).render(response_data, accepted_media_type, renderer_context)

    return response

Custom Exception Handler

# custom_exception_handler.py
def custom_exception_handler(exc, context):
  # Call REST framework's default exception handler first,
  # to get the standard error response.
  response = exception_handler(exc, context)

  # Now add the HTTP status code to the response.
  if response is not None:

    errors = []
    message = response.data.get('detail')
    if not message:
        for field, value in response.data.items():
            errors.append("{} : {}".format(field, " ".join(value)))
        response.data = {'data': [], 'message': 'Validation Error', 'errors': errors, 'status': 'failure'}
    else:
        response.data = {'data': [], 'message': message, 'error': [message], 'success': 'failure'}

  return response

Override REST_FRAMEWORK settings to use these renderer and exception handler.

REST_FRAMEWORK = {
  # Add custom renderer.
  'DEFAULT_RENDERER_CLASSES'=(
              'path.to.CustomJSONRenderer',
              'rest_framework.renderers.JSONRenderer',
              'rest_framework.renderers.BrowsableAPIRenderer'),
  # Add cutom exception handler
  'EXCEPTION_HANDLER': 'path.to.custom_exception_handler'
   }
like image 182
Atul Mishra Avatar answered Oct 17 '22 17:10

Atul Mishra


You can write function for customized django rest response and reuse it. Below is the way you can do this.

def custom_response(status_code=status.HTTP_200_OK, message='', data):
    return Response(
        {
             "status": status_code, 
             "message": message, 
              "data": data
        },
        status=status.HTTP_200_OK
    )

In this way response code for request will always be 200 but you can provide what ever code you want in status_code value. You can use this like this.

return custom_response(status_code=status.HTTP_200_OK, message='Data saved successfully', data=my_serialized_data)
like image 35
Muhammad Hassan Avatar answered Oct 17 '22 16:10

Muhammad Hassan