I want to...

Kind of FAQ.

... do something before/after request

Use CodernityREST decorators (not @ from python), you can add decorator to:

  • global_dec - which is a list of global decorators, and all functions will be wrapped with them,
  • ResourceRouter - only registered functions on this router will be wrapped,
  • single route - only this one function will be wrapped

With good combination of those 3 “levels” you can do everything you want.

... add database session with commit on request end

If all requests should have it add it to global_dec, if just some then use ResourceRouter dec, if only one function then use single route dec. And read ... do something before/after request.

... add/change default XYZ encoder

It’s very easy. Just add a pre_app_hook like

def my_json_hook(app):
    app.content_types['application/json'] = (my_json_load_funct, my_json_dump_funct)

The same way to add and change existing content_types.

... do something after my app is configured

app_hook is way to go. It is a list of hooks that will be executed just before app is returned to wsgi server. You have access to your newly configured app via app argument (or first one in hook).

... do something before my app is configured

pre_app_hook is way to go. It is a list of hooks that will be executed before any configuration is made. You have access to your newly created app via app argument (or first one in hook).

... send my exceptions/errors somewhere

UNMAPPED_EX_PROCESSORS is the way to go, it’s a list of functions that will be executed on non mapped exception (not defined in ExceptionMap). Sentry integration is done like this:

def unmapped_ex_to_sentry(obj, *args, **kwargs):
    client.captureException()