[Answered ]-Tastypie. How to add time of execution to responses?

0👍

I found an answer, custom middleware is helpful in that case. It suits not only tastypie, but also DRF, and any another framework.

middleware.py:

from datetime import datetime

class AddMetaMiddleware(object):
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = None
        if hasattr(self, 'process_request'):
            response = self.process_request(request)
        if not response:
            response = self.get_response(request)
        if hasattr(self, 'process_response'):
            response = self.process_response(request, response)
        return response

    def process_request(self, request):
        request._request_start_time = datetime.now()

    def process_template_response(self, request, response):
        if not hasattr(response, 'data'):
            return response
        response.data = {
            'data': response.data,
            'meta': {
                'current_time' : datetime.now(),
                'benchmark': {
                    'time': datetime.now() - request._request_start_time,
                }
            }
        }
        return response
👤svfat

1👍

I don’t know if there is a direct way through settings to do this or not but I can suggest to override get_list method and add this extra field to the meta like the following:

import json
from django.http import HttpResponse

class MyResource(ModelResource):

    def get_list(self, request, **kwargs):
        start = time.time()
        resp = super(MyResource, self).get_list(request, **kwargs)

        data = json.loads(resp.content)

        data['meta']['execution_time_in_ms'] = time.time() - start

        data = json.dumps(data)

        return HttpResponse(data, content_type='application/json', status=200)

If you would use this for more than one ModelResource you can make a base class that overrides all the method needed and then inherit from this base class.

1👍

That’ll only work for list endpoints though. My advice is to use a middleware to add X- headers, it’s a cleaner, more generalized solution.

Leave a comment