From 828baff301445a0a47b968681b5aec5079d95105 Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Tue, 23 Apr 2024 14:00:33 +0200 Subject: [PATCH] refactor assert --- .pre-commit-config.yaml | 1 + pyproject.toml | 6 +++--- titiler/xarray/main.py | 13 +------------ titiler/xarray/reader.py | 15 ++------------- titiler/xarray/redis_pool.py | 23 ++++++++++++++++++++--- 5 files changed, 27 insertions(+), 31 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6d84490..e6ef50a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -30,3 +30,4 @@ repos: exclude: tests/.* additional_dependencies: - types-attrs + - types-redis diff --git a/pyproject.toml b/pyproject.toml index db844d3..bfd8073 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,9 +30,9 @@ dependencies = [ "xarray==2024.3.0", "rioxarray==0.15.0", "zarr==2.17.2", - "fsspec==2024.3.1", - "s3fs==2024.3.1", - "aiohttp==3.9.5", + "fsspec", + "s3fs", + "aiohttp", "requests==2.31.0", "pydantic==2.0.2", "titiler.core==0.14.1", diff --git a/titiler/xarray/main.py b/titiler/xarray/main.py index 233289d..8065fc3 100644 --- a/titiler/xarray/main.py +++ b/titiler/xarray/main.py @@ -1,7 +1,6 @@ """titiler app.""" import logging -from typing import Any, Callable, Optional import rioxarray import zarr @@ -22,14 +21,6 @@ from titiler.xarray.middleware import ServerTimingMiddleware from titiler.xarray.settings import ApiSettings -get_redis: Optional[Callable[[], Any]] - -try: - from titiler.xarray.redis_pool import get_redis -except ImportError: - get_redis = None - - logging.getLogger("botocore.credentials").disabled = True logging.getLogger("botocore.utils").disabled = True logging.getLogger("rio-tiler").setLevel(logging.ERROR) @@ -107,9 +98,7 @@ def ping(): if api_settings.enable_cache: - assert ( - get_redis is not None - ), "`redis` must be installed to enable caching. Please install titiler-xarray with the `cache` optional dependencies that include `redis`." + from titiler.xarray.redis_pool import get_redis @app.get("/clear_cache") def clear_cache(cache_client=Depends(get_redis)): diff --git a/titiler/xarray/reader.py b/titiler/xarray/reader.py index d999c9e..dcdd286 100644 --- a/titiler/xarray/reader.py +++ b/titiler/xarray/reader.py @@ -3,7 +3,7 @@ import contextlib import pickle import re -from typing import Any, Callable, Dict, List, Optional +from typing import Any, Dict, List, Optional import attr import fsspec @@ -16,15 +16,9 @@ from rio_tiler.io.xarray import XarrayReader from rio_tiler.types import BBox +from titiler.xarray.redis_pool import get_redis from titiler.xarray.settings import ApiSettings -get_redis: Optional[Callable[[], Any]] -try: - from titiler.xarray.redis_pool import get_redis -except ImportError: - get_redis = None - - api_settings = ApiSettings() @@ -95,11 +89,6 @@ def xarray_open_dataset( """Open dataset.""" # Generate cache key and attempt to fetch the dataset from cache if api_settings.enable_cache: - - assert ( - get_redis is not None - ), "`redis` must be installed to enable caching. Please install titiler-xarray with the `cache` optional dependencies that include `redis`." - cache_client = get_redis() cache_key = f"{src_path}_{group}" if group is not None else src_path data_bytes = cache_client.get(cache_key) diff --git a/titiler/xarray/redis_pool.py b/titiler/xarray/redis_pool.py index 2c26d0c..11ac563 100644 --- a/titiler/xarray/redis_pool.py +++ b/titiler/xarray/redis_pool.py @@ -1,13 +1,20 @@ """ Redis singleton class. """ import os -import fakeredis -import redis # type: ignore - from titiler.xarray.settings import ApiSettings api_settings = ApiSettings() +try: + import redis +except ImportError: + redis = None # type: ignore + +try: + import fakeredis +except ImportError: + fakeredis = None + class RedisCache: """Redis connection pool singleton class.""" @@ -17,6 +24,11 @@ class RedisCache: @classmethod def get_instance(cls): """Get the redis connection pool.""" + + assert ( + redis + ), "`redis` must be installed to enable caching. Please install titiler-xarray with the `cache` optional dependencies that include `redis`." + if cls._instance is None: cls._instance = redis.ConnectionPool( host=api_settings.cache_host, port=6379, db=0 @@ -27,6 +39,11 @@ def get_instance(cls): def get_redis(): """Get a redis connection.""" if os.getenv("TEST_ENVIRONMENT"): + + assert ( + fakeredis + ), "`fakeredis` must be installed to enable caching in test environment. Please install titiler-xarray with the `dev` optional dependencies that include `fakeredis`." + server = fakeredis.FakeServer() # Use fakeredis in a test environment return fakeredis.FakeRedis(server=server)