|
2 | 2 | from contextlib import contextmanager |
3 | 3 | from urllib.request import urlopen |
4 | 4 |
|
5 | | -from flask import jsonify, request |
| 5 | +from flask import current_app, has_app_context, jsonify, request |
6 | 6 | from jose import jwt |
7 | | -from flask import current_app |
8 | 7 | from werkzeug.local import LocalProxy |
9 | 8 | from connexion.security import NO_VALUE |
10 | 9 | from ..database import db |
@@ -73,23 +72,26 @@ def init_app(app): |
73 | 72 |
|
74 | 73 |
|
75 | 74 | def _get_current_app(): |
76 | | - try: |
| 75 | + if has_app_context(): |
77 | 76 | return current_app._get_current_object() |
78 | | - except RuntimeError as exc: # pragma: no cover - defensive |
79 | | - if _flask_app is not None and "application context" in str(exc).lower(): |
80 | | - return _flask_app |
81 | | - raise |
| 77 | + if _flask_app is not None: # pragma: no cover - defensive |
| 78 | + return _flask_app |
| 79 | + raise RuntimeError("No Flask application is configured for authentication helpers.") |
82 | 80 |
|
83 | 81 |
|
84 | 82 | @contextmanager |
85 | 83 | def _ensure_app_context(): |
86 | | - try: |
| 84 | + if has_app_context(): |
87 | 85 | yield current_app._get_current_object() |
88 | | - except RuntimeError as exc: |
89 | | - if _flask_app is None or "application context" not in str(exc).lower(): |
90 | | - raise |
91 | | - with _flask_app.app_context(): |
92 | | - yield _flask_app |
| 86 | + return |
| 87 | + |
| 88 | + if _flask_app is None: # pragma: no cover - defensive |
| 89 | + raise RuntimeError( |
| 90 | + "No Flask application is configured for authentication helpers." |
| 91 | + ) |
| 92 | + |
| 93 | + with _flask_app.app_context(): |
| 94 | + yield _flask_app |
93 | 95 |
|
94 | 96 |
|
95 | 97 | app = LocalProxy(_get_current_app) |
|
0 commit comments