Reputation:
How can I add a hook before and after processing a request on twisted.web (twisted.web2 is fine too)? The equivalent of webpy's:
app = web.application(urls, globals())
app.add_processor(web.loadhook(my_attach_callback))
app.add_processor(web.unloadhook(my_detach_callback))
Thanks!
Upvotes: 2
Views: 433
Reputation: 48335
One approach Twisted Web allows is to insert an extra resource into the resource hierarchy the only purpose of which is to run your custom hooks, rather than to actually handle a segment of the request URL as resources typically do.
You can find an implementation of this approach in twisted/web/_auth/wrapper.py which implements the HTTPAuthSessionWrapper resource (exposed publicly in twisted.web.guard). Note the first line of getChildWithDefault
which ensures the resource doesn't consume one of the request segments. This allows it to sit in the resource hierarchy, modify behavior, but not otherwise change the way URLs are dispatched.
Upvotes: 1