aiohttp_utils/negotiation.py
def negotiation_middleware(
    renderers=DEFAULTS['RENDERERS'],
    negotiator=DEFAULTS['NEGOTIATOR'],
    force_negotiation=DEFAULTS['FORCE_NEGOTIATION']
):
    """Middleware which selects a renderer for a given request then renders
    a handler's data to a `aiohttp.web.Response`.
    """
    @asyncio.coroutine
    def factory(app, handler):

        @asyncio.coroutine
        def middleware(request):
            content_type, renderer = negotiator(
                request,
                renderers,
                force_negotiation,
            )
            request['selected_media_type'] = content_type
            response = yield from handler(request)

            if getattr(response, 'data', None):
                # Render data with the selected renderer
                if asyncio.iscoroutinefunction(renderer):
                    render_result = yield from renderer(request, response.data)
                else:
                    render_result = renderer(request, response.data)
            else:
                render_result = response

            if isinstance(render_result, web.Response):
                return render_result

            if getattr(response, 'data', None):
                response.body = render_result
                response.content_type = content_type

            return response
        return middleware
    return factory
def negotiation_middleware(
    renderers=DEFAULTS['RENDERERS'],
    negotiator=DEFAULTS['NEGOTIATOR'],
    force_negotiation=DEFAULTS['FORCE_NEGOTIATION']
):
    """Middleware which selects a renderer for a given request then renders
    a handler's data to a `aiohttp.web.Response`.
    """
    @asyncio.coroutine
    def factory(app, handler):

        @asyncio.coroutine
        def middleware(request):
            content_type, renderer = negotiator(
                request,
                renderers,
                force_negotiation,
            )
            request['selected_media_type'] = content_type
            response = yield from handler(request)

            if getattr(response, 'data', None):
                # Render data with the selected renderer
                if asyncio.iscoroutinefunction(renderer):
                    render_result = yield from renderer(request, response.data)
                else:
                    render_result = renderer(request, response.data)
            else:
                render_result = response

            if isinstance(render_result, web.Response):
                return render_result

            if getattr(response, 'data', None):
                response.body = render_result
                response.content_type = content_type

            return response
        return middleware
    return factory
Middleware which selects a renderer for a given request then renders
    a handler's data to a `aiohttp.web.Response`.