Skip to content

Commit

Permalink
Merge pull request #559 from sennetconsortium/tjmadonna/557-vitessce-…
Browse files Browse the repository at this point in the history
…token

Adding func to strip token=None in vitessce config
  • Loading branch information
maxsibilla authored Sep 10, 2024
2 parents 87ef1e4 + 52532b2 commit b218c57
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
25 changes: 19 additions & 6 deletions src/lib/vitessce.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from typing import Optional
from typing import Union

from redis.client import Redis

Expand All @@ -15,11 +15,11 @@ class VitessceConfigCache:
def __init__(self, redis_client: Redis):
self._redis_client = redis_client

def get(self, uuid: str, groups_token: str, as_str: bool = False) -> Optional[dict]:
def get(self, uuid: str, groups_token: str, as_str: bool = False) -> Union[dict, str, None]:
cached_data = self._redis_client.get(f"{REDIS_VITESSCE_PREFIX}_{uuid}")
if cached_data is None:
return None
config_str = cached_data.decode('utf-8')
config_str = cached_data.decode("utf-8")
if GROUPS_TOKEN_PLACEHOLDER in config_str:
# Replace the groups token placeholder with the actual groups token
config_str = config_str.replace(GROUPS_TOKEN_PLACEHOLDER, groups_token)
Expand All @@ -38,10 +38,23 @@ def set(self, uuid: str, config: dict, groups_token: str):
)

def delete(self, uuid: str) -> bool:
return self._redis_client.delete(
f"{REDIS_VITESSCE_PREFIX}_{uuid}"
)
return self._redis_client.delete(f"{REDIS_VITESSCE_PREFIX}_{uuid}")

def _should_cache(self, config: dict, groups_token: str) -> bool:
# Don't cache if the config contains the groups token
return groups_token not in json.dumps(config, separators=(",", ":"))


def strip_extras(config: Union[str, dict], as_str: bool = False) -> Union[str, dict]:
if isinstance(config, dict):
config = json.dumps(config, separators=(",", ":"))

if "?token=None" in config:
config = config.replace("?token=None", "")

if "&token=None" in config:
config = config.replace("&token=None", "")

if as_str:
return config
return json.loads(config)
8 changes: 6 additions & 2 deletions src/routes/vitessce/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
build_entity_metadata,
calculate_assay_info,
)
from lib.services import get_entity, get_entity_from_search_api
from lib.vitessce import VitessceConfigCache
from lib.services import get_entity
from lib.vitessce import VitessceConfigCache, strip_extras

vitessce_blueprint = Blueprint("vitessce", __name__)
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -71,6 +71,10 @@ def get_assaytype(entity: dict) -> dict:

if cache:
cache.set(entity["uuid"], config, groups_token)

if groups_token is None:
config = strip_extras(config)

return jsonify(config), 200

except ResponseException as re:
Expand Down

0 comments on commit b218c57

Please sign in to comment.