# Convenience

# Fixed serializer

Often when developing an application, there will be certain routes that always return the same sort of response. When this is the case, you can predefine the return serializer and on the endpoint, and then all that needs to be returned is the content.

from sanic_ext import serializer
async def hello_world(request, name: str):
    if name.isnumeric():
        return "hello " * int(name)
    return f"Hello, {name}"

The serializer decorator also can add status codes.

from sanic_ext import serializer
@serializer(text, status=202)
async def create_something(request):

# Custom serializer

Using the @serializer decorator, you can also pass your own custom functions as long as they also return a valid type (HTTPResonse).

def message(retval, request, action, status):
    return json(
            "request_id": str(request.id),
            "action": action,
            "message": retval,
async def do_action(request, action: str):
    return "This is a message"

Now, returning just a string should return a nice serialized output.

$ curl localhost:8000/eat_cookies -X POST
  "request_id": "ef81c45b-235c-46dd-9dbd-b550f8fa77f9",
  "action": "eat_cookies",
  "message": "This is a message"

# Request counter

Sanic Extensions comes with a subcleass of Request that can be setup to automatically keep track of the number of requests processed per worker process. To enable this, you should pass the CountedRequest class to your application contructor.

from sanic_ext import CountedRequest
app = Sanic(..., request_class=CountedRequest)

You will now have access to the number of requests served during the lifetime of the worker process.

async def handler(request: CountedRequest):
    return json({"count": request.count})

If possible, the request count will also be added to the worker state.

MIT Licensed
Copyright © 2018-present Sanic Community Organization

~ Made with ❤️ and ☕️ ~