Fuyukai / Kyoukai

[OLD] A fully async web framework for Python3.5+ using asyncio
https://mirai.veriny.tf
MIT License
298 stars 14 forks source link

more info about hooks #10

Closed njordr closed 7 years ago

njordr commented 7 years ago

Hi.

Could you please explain in more detail pre and post hooks?

Fuyukai commented 7 years ago

Hooks are just something that is ran before or after a request.

For example, a pre hook is a coroutine that takes in the application context, and returns either a) A modified version of it or b) A new context.

@bp.before_request
async def something(ctx):
    return MyContext(ctx)  # change to your own context class

This is very similar for after_request, but this just takes in the context and the Response object, and returns a new or modified Response object.

This is useful for things such as an automatic jsonifier, which will turn any dict response body into JSON.

njordr commented 7 years ago

Many thanks