Skip to content

Commit f8a4ad9

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
temp
1 parent cb7ee7c commit f8a4ad9

File tree

2 files changed

+70
-13
lines changed

2 files changed

+70
-13
lines changed

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -332,17 +332,6 @@ async def all_collections(
332332
current_url = str(request.url)
333333
redis_enable = get_bool_env("REDIS_ENABLE", default=False)
334334

335-
redis = None
336-
if redis_enable:
337-
try:
338-
redis = await connect_redis()
339-
logger.info("Redis connection established successfully")
340-
except Exception as e:
341-
redis = None
342-
logger.warning(
343-
f"Redis connection failed, continuing without Redis: {e}"
344-
)
345-
346335
# Convert q to a list if it's a string
347336
q_list = None
348337
if q is not None:
@@ -441,6 +430,17 @@ async def all_collections(
441430
},
442431
]
443432

433+
redis = None
434+
if redis_enable:
435+
try:
436+
redis = await connect_redis()
437+
logger.info("Redis connection established successfully")
438+
except Exception as e:
439+
redis = None
440+
logger.warning(
441+
f"Redis connection failed, continuing without Redis: {e}"
442+
)
443+
444444
if redis_enable and redis:
445445
if next_token:
446446
await save_self_link(redis, next_token, current_url)
@@ -775,7 +775,6 @@ async def post_search(
775775
HTTPException: If there is an error with the cql2_json filter.
776776
"""
777777
base_url = str(request.base_url)
778-
redis_enable = get_bool_env("REDIS_ENABLE", default=False)
779778

780779
search = self.database.make_search()
781780

@@ -901,6 +900,8 @@ async def post_search(
901900
)
902901
links.extend(collection_links)
903902

903+
redis_enable = get_bool_env("REDIS_ENABLE", default=False)
904+
904905
if redis_enable:
905906
redis = None
906907
try:

stac_fastapi/core/stac_fastapi/core/redis_utils.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
"""Utilities for connecting to and managing Redis connections."""
22

3-
from typing import Optional
3+
import logging
4+
from typing import Dict, List, Optional
45

6+
from fastapi import Request
57
from pydantic_settings import BaseSettings
68
from redis import asyncio as aioredis
79
from redis.asyncio.sentinel import Sentinel
810

11+
from stac_fastapi.core.utilities import get_bool_env
12+
913
redis_pool: Optional[aioredis.Redis] = None
1014

15+
logger = logging.getLogger(__name__)
16+
1117

1218
class RedisSentinelSettings(BaseSettings):
1319
"""Configuration for connecting to Redis Sentinel."""
@@ -122,3 +128,53 @@ async def get_prev_link(redis: aioredis.Redis, token: Optional[str]) -> Optional
122128
if not token:
123129
return None
124130
return await redis.get(f"nav:self:{token}")
131+
132+
133+
async def handle_redis_pagination(
134+
request: Request,
135+
links: List[Dict],
136+
next_token: Optional[str] = None,
137+
current_token: Optional[str] = None,
138+
redis_enable: Optional[bool] = None,
139+
) -> None:
140+
"""
141+
Handle Redis-based pagination for both items and collections.
142+
143+
Args:
144+
request: FastAPI request object
145+
links: Links array to modify
146+
next_token: Token for the next page (used to save current URL)
147+
current_token: Current page token (used to retrieve previous page)
148+
redis_enable: Whether Redis is enabled (auto-detected if None)
149+
"""
150+
# Auto-detect Redis enablement if not provided
151+
if redis_enable is None:
152+
redis_enable = get_bool_env("REDIS_ENABLE", default=False)
153+
154+
if not redis_enable:
155+
return
156+
157+
try:
158+
redis = await connect_redis()
159+
logger.info("Redis connection established successfully")
160+
161+
# Save current URL for next page's previous link
162+
if next_token:
163+
self_link = str(request.url)
164+
await save_self_link(redis, next_token, self_link)
165+
166+
# Get previous page link if available
167+
if current_token:
168+
prev_link = await get_prev_link(redis, current_token)
169+
if prev_link:
170+
links.insert(
171+
0,
172+
{
173+
"rel": "prev",
174+
"type": "application/json",
175+
"method": "GET",
176+
"href": prev_link,
177+
},
178+
)
179+
except Exception as e:
180+
logger.warning(f"Redis connection failed, continuing without Redis: {e}")

0 commit comments

Comments
 (0)