Search code examples
pythonrestpyramidhttpexceptioncornice

Python/Pyramid - Wrapping Cornice response with extra data when raising HTTP Exception


I'm using Pyramid and Cornice to write some RESTful Python app and I made a simple Cornice resource:

@resource(collection_path='/users/', path='/users/{id}')
class UsersResource(object):

    def __init__(self, request):
        self.request = request

    @view(renderer='json', content_type=content_type)
    @my_wrapper
    def get(self):
        return {'user_id': self.request.matchdict['id']}

As you may have noticed, along with Cornice's view decorator, I also added an extra decorator here (my_decorator), which I intended to use as an wrapper to add some extra information to the response:

def my_wrapper(method):
    def wrapper(*args, **kw):
        time_start = time()
        profiler = sqltap.start()
        fn_result = method(*args, **kw)
        stats = profiler.collect()
        time_end = time()

        result = {
            'info': {
                'api_version': args[0].request.registry.settings.api_version,
                'request_path': args[0].request.path_info,
                'request_method': args[0].request.method,
                'current_time': datetime.datetime.now(pytz.utc).strftime('%Y-%m-%dT%H:%M:%SZ'),
                'execution_time': time_end - time_start,
                'total_queries': len(stats),
                'query_time': stats[0].duration if len(stats) > 0 else 0,
            },
        }
        result.update(fn_result)

        return result

    return wrapper

This works fine unless I define Cornice validators in my view decorator:

from validators import validate_int

@resource(collection_path='/users/', path='/users/{id}')
class UsersResource(object):

    def __init__(self, request):
        self.request = request

    @view(renderer='json', content_type=content_type, validators=validate_int)  # added validators
    @my_wrapper
    def get(self):
        return {'user_id': self.request.matchdict['id']}

validators.py

import responses

def validate_int(request):
    should_be_int = request.matchdict['id']
    try:
        int(should_be_int)
    except:
        raise responses._400('This doesn\'t look like a valid ID.')

responses.py

class _400(exc.HTTPError):
    def __init__(self, desc):
        body = {'status': 400, 'message': 'Bad Request', 'description': desc}
        Response.__init__(self, json.dumps(body))

With a code like this, my_wrapper wraps a response only if the validation passes (which is completely understandable), but I wonder how can I still wrap the response with some extra information when the default HTTPException is raised (because in that case the code never reaches my_wrapper at all)?


Solution

  • In a short discussion with a brilliant Pyramid IRC community, I decided to do this with Pyramid's tweens, rather than using the wrapper.