mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-05 06:01:46 -04:00
move (non-api) clients (inc redis) from app/__init__.py to extensions
when clients are defined in app/__init__.py, it increases the chance of cyclical imports. By moving module level client singletons out to a separate extensions file, we stop cyclical imports, but keep the same code flow - the clients are still initialised in `create_app` in `__init__.py`. The redis client in particular is no longer separate - previously redis was set up on the `NotifyAdminAPIClient` base class, but now there's one singleton in `app.extensions`. This was done so that we can access redis from outside of the existing clients.
This commit is contained in:
@@ -4,6 +4,8 @@ from datetime import timedelta
|
||||
from functools import wraps
|
||||
from inspect import signature
|
||||
|
||||
from app.extensions import redis_client
|
||||
|
||||
TTL = int(timedelta(days=7).total_seconds())
|
||||
|
||||
|
||||
@@ -38,11 +40,11 @@ def set(key_format):
|
||||
@wraps(client_method)
|
||||
def new_client_method(client_instance, *args, **kwargs):
|
||||
redis_key = _make_key(key_format, client_method, args, kwargs)
|
||||
cached = client_instance.redis_client.get(redis_key)
|
||||
cached = redis_client.get(redis_key)
|
||||
if cached:
|
||||
return json.loads(cached.decode('utf-8'))
|
||||
api_response = client_method(client_instance, *args, **kwargs)
|
||||
client_instance.redis_client.set(
|
||||
redis_client.set(
|
||||
redis_key,
|
||||
json.dumps(api_response),
|
||||
ex=TTL,
|
||||
@@ -60,7 +62,7 @@ def delete(key_format):
|
||||
@wraps(client_method)
|
||||
def new_client_method(client_instance, *args, **kwargs):
|
||||
redis_key = _make_key(key_format, client_method, args, kwargs)
|
||||
client_instance.redis_client.delete(redis_key)
|
||||
redis_client.delete(redis_key)
|
||||
return client_method(client_instance, *args, **kwargs)
|
||||
|
||||
return new_client_method
|
||||
|
||||
Reference in New Issue
Block a user