Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add support for flask > 2.2 #62

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions flask_log_request_id/request_id.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import uuid
import logging as _logging

import flask
from flask import request, g, current_app

from .parser import auto_parser
Expand All @@ -15,12 +16,19 @@ def flask_ctx_get_request_id():
Get request id from flask's G object
:return: The id or None if not found.
"""
from flask import _app_ctx_stack as stack # We do not support < Flask 0.9

if stack.top is None:
# from flask import _app_ctx_stack as stack # We do not support < Flask 0.9
if hasattr(flask, 'globals') and \
hasattr(flask.globals, 'request_ctx'):
# update session for Flask >= 2.2
ctx = flask.globals.request_ctx._get_current_object()
else: # pragma: no cover
# update session for Flask < 2.2
ctx = flask._request_ctx_stack.top

if ctx is None:
raise ExecutedOutsideContext()

g_object_attr = stack.top.app.config['LOG_REQUEST_ID_G_OBJECT_ATTRIBUTE']
g_object_attr = ctx.app.config['LOG_REQUEST_ID_G_OBJECT_ATTRIBUTE']
return g.get(g_object_attr, None)


Expand Down