def oauth_error_response(e): logging.error("OAuth error. %s" % e.message) logging.exception(e) return current_app.response_class( "OAuth error. %s" % e.message, status=401, headers=build_authenticate_header(realm="http://www.khanacademy.org"))
def jsonp_enabled(*args, **kwargs): val = func(*args, **kwargs) if isinstance(val, current_app.response_class): return val callback = request.values.get("callback") if callback: val = "%s(%s)" % (callback, val) return current_app.response_class( val, mimetype="application/json; charset=utf-8")
def etag_enabled(*args, **kwargs): tag_content = func_tag_content(*args, **kwargs) # You can disable etags on a per-arg basis by returning None above. if tag_content is None: return func(*args, **kwargs) etag_inner_content = "%s:%s" % (tag_content, App.version) etag_server = '"%s"' % hashlib.md5(etag_inner_content).hexdigest() etag_client = request.headers.get("If-None-Match") if etag_client and etag_client == etag_server: return current_app.response_class(status=304) result = func(*args, **kwargs) if isinstance(result, current_app.response_class): result.headers["ETag"] = etag_server return result else: return current_app.response_class( result, headers={"Etag": etag_server})
def api_errors_formatted(*args, **kwargs): try: return func(*args, **kwargs) except Exception, e: # If any exception makes it all the way up to the top of # an API request, send possibly helpful message down for # consumer if isinstance(e, QuietException): logging.info(e) else: logging.exception(e) return current_app.response_class("API error. %s" % e.message, status=500)
def caching_enabled(*args, **kwargs): cache_token = request.args.get(cache_token_key) if cache_token is None: # If no cache_token is specified, don't treat it as cacheable return func(*args, **kwargs) result = func(*args, **kwargs) if not isinstance(result, current_app.response_class): result = current_app.response_class(result) result.cache_control.max_age = caching_age result.cache_control.public = True result.headers['Expires'] = ( datetime.datetime.utcnow() + datetime.timedelta(seconds=caching_age)) return result
def oauth_error_response(e): logging.error("OAuth error. %s" % e.message) logging.exception(e) return current_app.response_class( "OAuth error. %s" % e.message, status=401, headers=build_authenticate_header(realm="http://www.khanacademy.org") )
def api_forbidden_response(message): return current_app.response_class(message, status=403)
def api_invalid_param_response(message): return current_app.response_class(message, status=400)
def unauthorized_response(): return current_app.response_class("Unauthorized", status=401)
def api_success_no_content_response(): return current_app.response_class(status=204)
def api_created_response(message): return current_app.response_class(message, status=201)
def api_opengraph_error_response(e): logging.error("Open Graph error: %s" % e) return current_app.response_class(e.message, status=400)
def api_error_response(e): logging.error("API error: %s" % e) return current_app.response_class("API error. %s" % e.message, status=500)
def api_not_found_response(message): return current_app.response_class(message, status=404)
def api_unauthorized_response(message): return current_app.response_class(message, status=401)