Created
May 11, 2010 22:04
-
-
Save shazow/397945 to your computer and use it in GitHub Desktop.
Google AppEngine API controller base.
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
from django.utils import simplejson | |
from google.appengine.ext.webapp import RequestHandler | |
import logging | |
log = logging.getLogger(__name__) | |
class APIException(Exception): | |
def __init__(self, msg, code=400): | |
self.msg = msg | |
self.code = 400 | |
class APIHandler(RequestHandler): | |
def _index(self): | |
function = self.request.params.get('function') | |
if not function: | |
raise APIException("Missing parameter: function") | |
fn = getattr(self, "api_%s" % function, None) | |
if not fn: | |
raise APIException("Invalid function: %s" % function) | |
try: | |
r = fn() | |
except KeyError, e: | |
log.debug(e) | |
raise APIException("Function missing required parameter: %s" % e.args[0]) | |
return {'returned': r} | |
def get(self): | |
response = {'messages': [], 'status': 'ok', 'code': 200, 'returned': []} | |
response_template = "%s" | |
callback = self.request.params.get('callback') | |
if callback: | |
response_template = "%s(%%s);" % callback | |
try: | |
r = self._index() | |
except APIException, e: | |
self.response.set_status(e.code) | |
r = {'messages': [e.msg], 'status': 'error', 'code': e.code} | |
response.update(r) | |
self.response.headers['Content-Type'] = "text/javascript; charset=utf-8" | |
self.response.out.write(response_template % simplejson.dumps(response)) | |
def post(self): | |
return self.get() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment