Skip to content

Instantly share code, notes, and snippets.

@pilt
Created May 2, 2012 13:37
Show Gist options
  • Save pilt/2576557 to your computer and use it in GitHub Desktop.
Save pilt/2576557 to your computer and use it in GitHub Desktop.
def unpacked(callback):
"""Decorator to unpack responses before passing the result to a callback.
"""
@functools.wraps(callback)
@tornado.stack_context.wrap
def unpacking_callback(response):
callback(mdp.unpack_response(response))
return unpacking_callback
class SomeHandler(tornado.web.RequestHandler):
def get_account(self, callback):
api_key = self.get_api_key()
arg = {"key": "api_key", "value": api_key}
request = mdp.pack_request("get", arg)
jarmodmoo.tornado.send("accounts", request, unpacked(callback))
@tornado.web.asynchronous
@gen.engine
def get(self):
try:
account = yield gen.Task(self.get_account)
except mdp.exceptions.ResponseError:
self.write_error(400)
else:
logger.info("got account %r", account)
self.write_json(account)
self.finish()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment