|
4 | 4 | from typing import Optional, cast
|
5 | 5 | from uuid import uuid4
|
6 | 6 |
|
7 |
| -from flask import Response, g |
| 7 | +from flask import Response, g, request |
8 | 8 | from flask_limiter import Limiter
|
9 | 9 | from flask_limiter.util import get_remote_address
|
10 | 10 | from werkzeug.local import LocalProxy
|
11 | 11 | import redis
|
12 | 12 |
|
13 |
| -from ._common import app, request |
| 13 | +from ._common import app |
14 | 14 | from ._config import (API_KEY_REQUIRED_STARTING_AT, RATELIMIT_STORAGE_URL,
|
15 |
| - URL_PREFIX) |
| 15 | + URL_PREFIX, REDIS_HOST) |
16 | 16 | from ._exceptions import MissingAPIKeyException, UnAuthenticatedException
|
17 | 17 | from .admin.models import User, UserRole
|
18 | 18 | # from ._logger import get_structured_logger
|
@@ -178,17 +178,15 @@ def _no_rate_limit() -> bool:
|
178 | 178 | return user is not None and user.registered # type: ignore
|
179 | 179 |
|
180 | 180 |
|
181 |
| -# TODO: fix function below |
182 |
| - |
183 |
| -# @app.after_request |
184 |
| -# def update_key_last_time_used(response): |
185 |
| -# if _is_public_route(): |
186 |
| -# return response |
187 |
| -# try: |
188 |
| -# r = redis.Redis(host="delphi_redis_instance") |
189 |
| -# api_key = g.user.api_key |
190 |
| -# r.set(f"LAST_USED/{api_key}", datetime.strftime(datetime.now(), "%Y-%m-%d")) |
191 |
| -# except Exception as e: |
192 |
| -# print(e) # TODO: should be handled properly |
193 |
| -# finally: |
194 |
| -# return response |
| 181 | +@app.after_request |
| 182 | +def update_key_last_time_used(response): |
| 183 | + if _is_public_route(): |
| 184 | + return response |
| 185 | + try: |
| 186 | + r = redis.Redis(host=REDIS_HOST) |
| 187 | + api_key = g.user.api_key |
| 188 | + r.set(f"LAST_USED/{api_key}", datetime.strftime(datetime.now(), "%Y-%m-%d")) |
| 189 | + except Exception as e: |
| 190 | + print(e) # TODO: should be handled properly |
| 191 | + finally: |
| 192 | + return response |
0 commit comments