1

I adapted this sample code in order to get webapp2 sessions to work on Google App Engine.

What do I need to do to be able to return webapp2.Response objects from a handler that's inheriting from a BaseHandler that overrides the dispatch method?

Here's a demonstration of the kind of handler I want to write:

import webapp2
import logging

from webapp2_extras import sessions


class BaseHandler(webapp2.RequestHandler):
  def dispatch(self):
    # Get a session store for this request.
    self.session_store = sessions.get_store(request=self.request)
    try:
      # Dispatch the request.
      webapp2.RequestHandler.dispatch(self)
    finally:
      # Save all sessions.
      self.session_store.save_sessions(self.response)


class HomeHandler(BaseHandler):
  def get(self):
    logging.debug('In homehandler')
    response = webapp2.Response()
    response.write('Foo')
    return response

config = {}
config['webapp2_extras.sessions'] = {
    'secret_key': 'some-secret-key',
}


app = webapp2.WSGIApplication([
    ('/test', HomeHandler),
], debug=True, config=config)


This code is obviously not working, since BaseHandler always calls dispatch with self. I've looked through the code of webapp2.RequestHandler, but it seriously eludes me how to modify my BaseHandler (or perhaps set a custom dispatcher) such that I can simply return response objects from inheriting handlers.

Curiously, the shortcut of assigning self.response = copy.deepcopy(response) does not work either.

5
  • You forgot two important parts in your code, which are part of the example you adapted. 1) the session method with the cached property 2) to make use of sessions in your base handler, using self.session...
    – voscausa
    Apr 2, 2013 at 21:39
  • I'm aware of that - I've got sessions working fine (with the methods you mentioned) in my actual application, they're not the issue. This is just a bare-bones example of what is needed to understand the dispatch and handler-subclassing problem.
    – Alice
    Apr 3, 2013 at 13:17
  • Why do you want to return a response object? What do you want to do with it?
    – Robsdedude
    May 15, 2013 at 10:00
  • I'm adapting a library (github.com/StartTheShift/pyoauth2) that already constructs response objects from request data. I wouldn't mind the overhead of just copying the returned response objects into self.response, but given that the deepcopy assignment doesn't work ... - it seems rather ugly and error-prone to copy each response attribute on its own.
    – Alice
    May 15, 2013 at 15:27
  • 1
    Maybe set self.response=response ? I haven't tried that before but it may work... Jun 13, 2013 at 23:51

2 Answers 2

0

You're mixing the two responses in one method. Use either

return webapp2.Response('Foo')

or

self.response.write('Foo')

...not both.

3
  • Thanks for your reply. I don't exactly see how this pertains to my question though. (I don't think it matters how I construct the response body (and this is just an example, mind you)). My question was how I can enable sessions while having inheriting classes still be able to expicitly return simple response objects (as opposed to the implicit call to self.response). My temporary solution is to explicitly do the session grunt work that the BaseHandler is doing in dispatch for every call. I'd prefer a leaner solution.
    – Alice
    Jun 13, 2013 at 14:53
  • In your code, it looks like your dispatch() method is doing the right thing (your "leaner solution"). Simply change your get() method to use self.response instead of creating a new webapp2.Response(). Jun 13, 2013 at 18:27
  • Oh, I see what you're saying. Yes, you're right. However, I'm adapting a library that's outputting ready-made Response objects including status codes, body, headers. Now in the application that's using that library ... - I would prefer to simply return those prepackaged responses instead of copying over their contents to self.response. Especially since self.response = copy.deepcopy(response) doesn't work for reasons that elude me too. See my comments above if it helps. Thanks for helping out.
    – Alice
    Jun 13, 2013 at 23:48
0

I took a look at webapp2.RequestHandler and noticed that returned values are just passed up the stack.

A solution which works for me is to use the returned Response when one is returned from the handler, or self.response when nothing is returned.

class BaseHandler(webapp2.RequestHandler):
    def dispatch(self):
        # Get a session store for this request.
        self.session_store = sessions.get_store(request=self.request)

        response = None
        try:
            # Dispatch the request.
            response = webapp2.RequestHandler.dispatch(self)
            return response
        finally:
            # Save all sessions.
            if response is None:
                response = self.response
            self.session_store.save_sessions(response)

While I was playing I noticed that my session stored as a secure cookie was not getting updated when exceptions were raised in the handler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.