Single function middleware decorators
Decorated functions using @tomodachi.decorator
@tomodachi.decorator
Handler functions can of course be decorated using custom functionality. For ease of use you can then in turn decorate your decorator with the the built-in @tomodachi
decorator to ease development. If the decorator would return anything else than True
or None
(or not specifying any return statement) the invoked function will not be called and instead the returned value will be used, for example as an HTTP response.
import tomodachi
@tomodachi.decorator
async def require_csrf(instance, request):
token = request.headers.get("X-CSRF-Token")
if not token or token != request.cookies.get("csrftoken"):
return {
"body": "Invalid CSRF token",
"status": 403
}
class Service(tomodachi.Service):
name = "example"
@tomodachi.http("POST", r"/create")
@require_csrf
async def create_data(self, request):
# Do magic here!
return "OK"
Updated over 1 year ago