Skip to content

Commit d02e3d1

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

File tree

2 files changed

+100
-52
lines changed

2 files changed

+100
-52
lines changed

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
from stac_fastapi.core.base_settings import ApiBaseSettings
2525
from stac_fastapi.core.datetime_utils import format_datetime_range
2626
from stac_fastapi.core.models.links import PagingLinks
27-
from stac_fastapi.core.redis_utils import connect_redis, get_prev_link, save_self_link
27+
from stac_fastapi.core.redis_utils import (
28+
connect_redis,
29+
get_prev_link,
30+
handle_pagination_links,
31+
save_self_link,
32+
)
2833
from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer
2934
from stac_fastapi.core.session import Session
3035
from stac_fastapi.core.utilities import filter_fields, get_bool_env
@@ -329,20 +334,6 @@ async def all_collections(
329334
if parsed_sort:
330335
sort = parsed_sort
331336

332-
current_url = str(request.url)
333-
redis_enable = get_bool_env("REDIS_ENABLE", default=False)
334-
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-
346337
# Convert q to a list if it's a string
347338
q_list = None
348339
if q is not None:
@@ -441,21 +432,37 @@ async def all_collections(
441432
},
442433
]
443434

444-
if redis_enable and redis:
445-
if next_token:
446-
await save_self_link(redis, next_token, current_url)
435+
current_url = str(request.url)
447436

448-
prev_link = await get_prev_link(redis, token)
449-
if prev_link:
450-
links.insert(
451-
0,
452-
{
453-
"rel": "prev",
454-
"type": "application/json",
455-
"method": "GET",
456-
"href": prev_link,
457-
},
458-
)
437+
await handle_pagination_links(current_url, token, next_token, links)
438+
439+
# redis_enable = get_bool_env("REDIS_ENABLE", default=False)
440+
441+
# redis = None
442+
# if redis_enable:
443+
# try:
444+
# redis = await connect_redis()
445+
# logger.info("Redis connection established successfully")
446+
# except Exception as e:
447+
# redis = None
448+
# logger.warning(
449+
# f"Redis connection failed, continuing without Redis: {e}"
450+
# )
451+
# if redis_enable and redis:
452+
# if next_token:
453+
# await save_self_link(redis, next_token, current_url)
454+
455+
# prev_link = await get_prev_link(redis, token)
456+
# if prev_link:
457+
# links.insert(
458+
# 0,
459+
# {
460+
# "rel": "prev",
461+
# "type": "application/json",
462+
# "method": "GET",
463+
# "href": prev_link,
464+
# },
465+
# )
459466

460467
if next_token:
461468
next_link = PagingLinks(next=next_token, request=request).link_next()
@@ -901,29 +908,33 @@ async def post_search(
901908
)
902909
links.extend(collection_links)
903910

904-
if redis_enable:
905-
redis = None
906-
try:
907-
redis = await connect_redis()
908-
logger.info("Redis connection established successfully")
909-
self_link = str(request.url)
910-
await save_self_link(redis, next_token, self_link)
911-
912-
prev_link = await get_prev_link(redis, token_param)
913-
if prev_link:
914-
links.insert(
915-
0,
916-
{
917-
"rel": "prev",
918-
"type": "application/json",
919-
"method": "GET",
920-
"href": prev_link,
921-
},
922-
)
923-
except Exception as e:
924-
logger.warning(
925-
f"Redis connection failed, continuing without Redis: {e}"
926-
)
911+
current_url = str(request.url)
912+
913+
await handle_pagination_links(current_url, token_param, next_token, links)
914+
915+
# if redis_enable:
916+
# redis = None
917+
# try:
918+
# redis = await connect_redis()
919+
# logger.info("Redis connection established successfully")
920+
# self_link = str(request.url)
921+
# await save_self_link(redis, next_token, self_link)
922+
923+
# prev_link = await get_prev_link(redis, token_param)
924+
# if prev_link:
925+
# links.insert(
926+
# 0,
927+
# {
928+
# "rel": "prev",
929+
# "type": "application/json",
930+
# "method": "GET",
931+
# "href": prev_link,
932+
# },
933+
# )
934+
# except Exception as e:
935+
# logger.warning(
936+
# f"Redis connection failed, continuing without Redis: {e}"
937+
# )
927938

928939
return stac_types.ItemCollection(
929940
type="FeatureCollection",

stac_fastapi/core/stac_fastapi/core/redis_utils.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""Utilities for connecting to and managing Redis connections."""
22

3+
import logging
34
from typing import Optional
45

56
from pydantic_settings import BaseSettings
67
from redis import asyncio as aioredis
78
from redis.asyncio.sentinel import Sentinel
89

10+
from stac_fastapi.core.utilities import get_bool_env
11+
912
redis_pool: Optional[aioredis.Redis] = None
1013

1114

@@ -122,3 +125,37 @@ async def get_prev_link(redis: aioredis.Redis, token: Optional[str]) -> Optional
122125
if not token:
123126
return None
124127
return await redis.get(f"nav:self:{token}")
128+
129+
130+
logger = logging.getLogger(__name__)
131+
132+
133+
async def handle_pagination_links(
134+
current_url: str, token: str, next_token: str, links: list
135+
) -> None:
136+
"""Handle Redis pagination."""
137+
redis_enable = get_bool_env("REDIS_ENABLE", default=False)
138+
redis = None
139+
if redis_enable:
140+
try:
141+
redis = await connect_redis()
142+
logger.info("Redis connection established successfully")
143+
except Exception as e:
144+
redis = None
145+
logger.warning(f"Redis connection failed, continuing without Redis: {e}")
146+
147+
if redis_enable and redis:
148+
if next_token:
149+
await save_self_link(redis, next_token, current_url)
150+
151+
prev_link = await get_prev_link(redis, token)
152+
if prev_link:
153+
links.insert(
154+
0,
155+
{
156+
"rel": "prev",
157+
"type": "application/json",
158+
"method": "GET",
159+
"href": prev_link,
160+
},
161+
)

0 commit comments

Comments
 (0)