Reputation: 13457
Assuming the following AppEngine/webapp2 code:
import webapp2
# insert header injection code here...
class HelloWebapp2(webapp2.RequestHandler):
def get(self):
self.response.write('Hello, webapp2!')
app = webapp2.WSGIApplication([
('/', HelloWebapp2),
], debug=True)
How can I inject request headers before app is initialized/called?
Upvotes: 0
Views: 964
Reputation: 13457
We decided to just extend webapp2.RequestHandler
and create a base class for all other handlers that we use in our application. In this base class, we override the dispatch
method, and inject headers. This makes those headers available to instances of any classes that derive from this base class.
class BaseHandler(webapp2.RequestHandler):
def dispatch(self):
// inject headers here (self.request.headers)
super(BaseHandler, self).dispatch()
class Page|SecurePage|APIEndPoint|ETC(BaseHandler):
// ...
Upvotes: 0
Reputation: 12986
Consider using some form of wsgi middle ware, which your wrapp app
with.
From wikipedia http://en.wikipedia.org/wiki/Web_Server_Gateway_Interface Under Specification overview
wsgi middleware implements both sides of the API so that it can intermediate between a WSGI server and a WSGI application: the middleware acts as an application from some WSGI server's point of view and as a server from some WSGI application's point of view. A "middleware" component can perform such functions as: Routing a request to different application objects based on the target URL, after changing the environment variables accordingly. Allowing multiple applications or frameworks to run side-by-side in the same process Load balancing and remote processing, by forwarding requests and responses over a network Perform content postprocessing, such as applying XSLT stylesheets
See article WSGI and WSGI Middleware is Easy http://be.groovie.org/2005/10/07/wsgi_and_wsgi_middleware_is_easy.html
I use middleware wrappers for a number of things in appengine.
Session management, event propagation - (ie send event on new session, new login). authorization (which is different authentication). Though I have never used webapp(2) it functions in the same way and is wsgi compliant.
Upvotes: 1