-def api_response(request: sanic.Request, result: Any) -> sanic.HTTPResponse:
- """ Render a response from the query results using the configured
- formatter.
- """
- body = request.ctx.formatter.format(result, request.ctx.format)
- return sanic.response.text(body,
- content_type=CONTENT_TYPE.get(request.ctx.format,
- 'application/json'))
-
-
-@api.on_request # type: ignore[misc]
-async def extract_format(request: sanic.Request) -> Optional[sanic.HTTPResponse]:
- """ Get and check the 'format' parameter and prepare the formatter.
- `ctx.result_type` describes the expected return type and
- `ctx.default_format` the format value to assume when no parameter
- is present.
- """
- assert request.route is not None
- request.ctx.formatter = request.app.ctx.formatters[request.route.ctx.result_type]