Created
May 2, 2012 13:37
-
-
Save pilt/2576557 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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