def dispatch(self): """Dispatches the requested method.""" request = self.request method_name = request.route.handler_method if not method_name: method_name = webapp2._normalize_handler_method(request.method) method = getattr(self, method_name, None) if method is None: # 405 Method Not Allowed. # The response MUST include an Allow header containing a # list of valid methods for the requested resource. # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6 valid = ', '.join(webapp2._get_handler_methods(self)) self.abort(405, headers=[('Allow', valid)]) # The handler only receives *args if no named variables are set. args, kwargs = request.route_args, request.route_kwargs if kwargs: args = () # bind session on dispatch (not in __init__) self.session = get_current_session() # init messages array based on session self.session['_gaetk_messages'] = self.session.get('_gaetk_messages', []) # Give authentication Hooks opportunity to do their thing self.authchecker(method, *args, **kwargs) # Execute the method. reply = method(*args, **kwargs) # find out which return convention was used - first set defaults ... content = reply statuscode = 200 cachingtime = self.default_cachingtime # ... then check if we got a 2-tuple reply ... if isinstance(reply, tuple) and len(reply) == 2: content, statuscode = reply # ... or a 3-tuple reply. if isinstance(reply, tuple) and len(reply) == 3: content, statuscode, cachingtime = reply # Finally begin sending the response response = self.serialize(content) if cachingtime: self.response.headers['Cache-Control'] = 'max-age=%d, public' % cachingtime # If we have gotten a `callback` parameter, we expect that this is a # [JSONP](http://en.wikipedia.org/wiki/JSONP#JSONP) cann and therefore add the padding if self.request.get('callback', None): response = "%s (%s)" % (self.request.get('callback', None), response) self.response.headers['Content-Type'] = 'text/javascript' else: self.response.headers['Content-Type'] = 'application/json' # Set status code and write JSON to output stream self.response.set_status(statuscode) self.response.out.write(response) self.response.out.write('\n')
def dispatch(self): """Dispatches the requested method.""" request = self.request method_name = request.route.handler_method if not method_name: method_name = webapp2._normalize_handler_method(request.method) method = getattr(self, method_name, None) if method is None: # 405 Method Not Allowed. # The response MUST include an Allow header containing a # list of valid methods for the requested resource. # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6 methods = [] for method in ('GET', 'POST', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE'): if getattr(self, webapp2._normalize_handler_method(method), None): methods.append(method) valid = ', '.join(methods) self.abort(405, headers=[('Allow', valid)]) # The handler only receives *args if no named variables are set. args, kwargs = request.route_args, request.route_kwargs if kwargs: args = () # bind session on dispatch (not in __init__) try: self.session = get_current_session() except AttributeError: # session handling not activated self.session = {} # init messages array based on session self.session['_gaetk_messages'] = self.session.get('_gaetk_messages', []) # Give authentication Hooks opportunity to do their thing self.authchecker(method, *args, **kwargs) ret = method(*args, **kwargs) self.finished_hook(ret, method, *args, **kwargs) return ret