Everything is better as WSGI middleware unless you need framework-specific details. Especially if you're smart and use the webob decorators to turn the complex WSGI protocol into simple request/response objects. For example when integrating with permissions I'm not even sure a tween makes sense. From within your groupfinder you can just connect to your entitlement system. For logging there are a lot of examples of both WSGI (paste's translogger) and tween (pyramid_exclog, pyramid_debugtoolbar) loggers that you can pull ideas from.
Pyramid: Tween or WSGI middleware for custom logger and external entitlement system?
-
04-06-2022 - |
Question
I would like to get a clear understanding of what would be the most pythonic and cleaner way to implement:
- a custom logger.
- a piece of code which connects via REST to a third-party entitlement system to be combined with the internal Pyramid ACLs and permission system.
Should I rather write a WSGI middleware which gets the app as parameter or a pure Pyramid Tween for either one or both my requirements?
Also, which of wsgi middleware or tween is the most compliant with apache + mod_wsgi?
Thanks
Solution 2
OTHER TIPS
I beg to differ from Merickels opinions. For the case 2 definitely you want to use tween as it talks about "integrating". The WSGI middleware is overused - my opinion is if your application requires a middleware, then it shouldn't be a middleware anymore. See for example the excellent rant from the author of PEP 333.
Furthermore, even the logging - for your own app - should be done in pyramid tween as the api is cleaner and there is less overhead. In any case it will be trivial to write it as a WSGI middleware IF you ever need.