Skip to content

Commit

Permalink
refactor assert
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Apr 23, 2024
1 parent d67fe5f commit 828baff
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 31 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ repos:
exclude: tests/.*
additional_dependencies:
- types-attrs
- types-redis
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 1 addition & 12 deletions titiler/xarray/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""titiler app."""

import logging
from typing import Any, Callable, Optional

import rioxarray
import zarr
Expand All @@ -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)
Expand Down Expand Up @@ -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)):
Expand Down
15 changes: 2 additions & 13 deletions titiler/xarray/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()


Expand Down Expand Up @@ -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)
Expand Down
23 changes: 20 additions & 3 deletions titiler/xarray/redis_pool.py
Original file line number Diff line number Diff line change
@@ -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."""
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 828baff

Please sign in to comment.