當前位置: 首頁>>代碼示例>>Python>>正文


Python ResponseUtil.headers["Allow"]方法代碼示例

本文整理匯總了Python中indico.web.flask.util.ResponseUtil.headers["Allow"]方法的典型用法代碼示例。如果您正苦於以下問題:Python ResponseUtil.headers["Allow"]方法的具體用法?Python ResponseUtil.headers["Allow"]怎麽用?Python ResponseUtil.headers["Allow"]使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在indico.web.flask.util.ResponseUtil的用法示例。


在下文中一共展示了ResponseUtil.headers["Allow"]方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: handler

# 需要導入模塊: from indico.web.flask.util import ResponseUtil [as 別名]
# 或者: from indico.web.flask.util.ResponseUtil import headers["Allow"] [as 別名]

#.........這裏部分代碼省略.........
    try:
        oauth_valid, oauth_request = oauth.verify_request([scope])
        if not oauth_valid and oauth_request and oauth_request.error_message != "Bearer token not found.":
            raise BadRequest("OAuth error: {}".format(oauth_request.error_message))
        elif g.get("received_oauth_token") and oauth_request.error_message == "Bearer token not found.":
            raise BadRequest("OAuth error: Invalid token")
    except ValueError:
        # XXX: Dirty hack to workaround a bug in flask-oauthlib that causes it
        #      not to properly urlencode request query strings
        #      Related issue (https://github.com/lepture/flask-oauthlib/issues/213)
        oauth_valid = False

    # Get our handler function and its argument and response type
    hook, dformat = HTTPAPIHook.parseRequest(path, queryParams)
    if hook is None or dformat is None:
        raise NotFound

    # Disable caching if we are not just retrieving data (or the hook requires it)
    if request.method == "POST" or hook.NO_CACHE:
        noCache = True

    ak = error = result = None
    ts = int(time.time())
    typeMap = {}
    responseUtil = ResponseUtil()
    try:
        used_session = None
        if cookieAuth:
            used_session = session
            if not used_session.user:  # ignore guest sessions
                used_session = None

        if apiKey or oauth_valid or not used_session:
            if not oauth_valid:
                # Validate the API key (and its signature)
                ak, enforceOnlyPublic = checkAK(apiKey, signature, timestamp, path, query)
                if enforceOnlyPublic:
                    onlyPublic = True
                # Create an access wrapper for the API key's user
                aw = buildAW(ak, onlyPublic)
            else:  # Access Token (OAuth)
                at = load_token(oauth_request.access_token.access_token)
                aw = buildAW(at, onlyPublic)
            # Get rid of API key in cache key if we did not impersonate a user
            if ak and aw.getUser() is None:
                cacheKey = normalizeQuery(
                    path,
                    query,
                    remove=("_", "ak", "apiKey", "signature", "timestamp", "nc", "nocache", "oa", "onlyauthed"),
                )
            else:
                cacheKey = normalizeQuery(
                    path, query, remove=("_", "signature", "timestamp", "nc", "nocache", "oa", "onlyauthed")
                )
                if signature:
                    # in case the request was signed, store the result under a different key
                    cacheKey = "signed_" + cacheKey
        else:
            # We authenticated using a session cookie.
            if Config.getInstance().getCSRFLevel() >= 2:
                token = request.headers.get("X-CSRF-Token", get_query_parameter(queryParams, ["csrftoken"]))
                if used_session.csrf_protected and used_session.csrf_token != token:
                    raise HTTPAPIError("Invalid CSRF token", 403)
            aw = AccessWrapper()
            if not onlyPublic:
                aw.setUser(used_session.avatar)
            userPrefix = "user-{}_".format(used_session.user.id)
            cacheKey = userPrefix + normalizeQuery(
                path, query, remove=("_", "nc", "nocache", "ca", "cookieauth", "oa", "onlyauthed", "csrftoken")
            )

        # Bail out if the user requires authentication but is not authenticated
        if onlyAuthed and not aw.getUser():
            raise HTTPAPIError("Not authenticated", 403)

        addToCache = not hook.NO_CACHE
        cache = GenericCache("HTTPAPI")
        cacheKey = RE_REMOVE_EXTENSION.sub("", cacheKey)
        if not noCache:
            obj = cache.get(cacheKey)
            if obj is not None:
                result, extra, ts, complete, typeMap = obj
                addToCache = False
        if result is None:
            ContextManager.set("currentAW", aw)
            # Perform the actual exporting
            res = hook(aw)
            if isinstance(res, tuple) and len(res) == 4:
                result, extra, complete, typeMap = res
            else:
                result, extra, complete, typeMap = res, {}, True, {}
        if result is not None and addToCache:
            ttl = api_settings.get("cache_ttl")
            cache.set(cacheKey, (result, extra, ts, complete, typeMap), ttl)
    except HTTPAPIError, e:
        error = e
        if e.getCode():
            responseUtil.status = e.getCode()
            if responseUtil.status == 405:
                responseUtil.headers["Allow"] = "GET" if request.method == "POST" else "POST"
開發者ID:MichelCordeiro,項目名稱:indico,代碼行數:104,代碼來源:handlers.py


注:本文中的indico.web.flask.util.ResponseUtil.headers["Allow"]方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。