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"))
Beispiel #2
0
    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")
Beispiel #3
0
    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")
Beispiel #4
0
        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})
Beispiel #5
0
        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})
Beispiel #6
0
    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)
Beispiel #7
0
 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
Beispiel #8
0
        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
Beispiel #9
0
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")
    )
Beispiel #10
0
def api_forbidden_response(message):
    return current_app.response_class(message, status=403)
Beispiel #11
0
def api_invalid_param_response(message):
    return current_app.response_class(message, status=400)
Beispiel #12
0
def unauthorized_response():
    return current_app.response_class("Unauthorized", status=401)
Beispiel #13
0
def api_success_no_content_response():
    return current_app.response_class(status=204)
Beispiel #14
0
def api_created_response(message):
    return current_app.response_class(message, status=201)
Beispiel #15
0
def api_opengraph_error_response(e):
    logging.error("Open Graph error: %s" % e)
    return current_app.response_class(e.message, status=400)
Beispiel #16
0
def api_error_response(e):
    logging.error("API error: %s" % e)
    return current_app.response_class("API error. %s" % e.message, status=500)
Beispiel #17
0
def api_not_found_response(message):
    return current_app.response_class(message, status=404)
def unauthorized_response():
    return current_app.response_class("Unauthorized", status=401)
Beispiel #19
0
def api_unauthorized_response(message):
    return current_app.response_class(message, status=401)