Skip to content

Commit ab22909

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
dummy
1 parent cb7ee7c commit ab22909

File tree

3 files changed

+94
-52
lines changed

3 files changed

+94
-52
lines changed

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
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 _handle_pagination_via_redis
2828
from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer
2929
from stac_fastapi.core.session import Session
3030
from stac_fastapi.core.utilities import filter_fields, get_bool_env
@@ -329,20 +329,8 @@ async def all_collections(
329329
if parsed_sort:
330330
sort = parsed_sort
331331

332-
current_url = str(request.url)
333332
redis_enable = get_bool_env("REDIS_ENABLE", default=False)
334333

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-
346334
# Convert q to a list if it's a string
347335
q_list = None
348336
if q is not None:
@@ -441,21 +429,7 @@ async def all_collections(
441429
},
442430
]
443431

444-
if redis_enable and redis:
445-
if next_token:
446-
await save_self_link(redis, next_token, current_url)
447-
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-
)
432+
_handle_pagination_via_redis(redis_enable, next_token, token, request, links)
459433

460434
if next_token:
461435
next_link = PagingLinks(next=next_token, request=request).link_next()
@@ -901,29 +875,9 @@ async def post_search(
901875
)
902876
links.extend(collection_links)
903877

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-
)
878+
_handle_pagination_via_redis(
879+
redis_enable, next_token, token_param, request, links
880+
)
927881

928882
return stac_types.ItemCollection(
929883
type="FeatureCollection",
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
redis = None
2+
if redis_enable:
3+
try:
4+
redis = await connect_redis()
5+
logger.info("Redis connection established successfully")
6+
if redis and next_token:
7+
await save_self_link(redis, next_token, current_url)
8+
9+
prev_link = await get_prev_link(redis, token)
10+
if prev_link:
11+
links.insert(
12+
0,
13+
{
14+
"rel": "prev",
15+
"type": "application/json",
16+
"method": "GET",
17+
"href": prev_link,
18+
},
19+
)
20+
except Exception as e:
21+
redis = None
22+
logger.warning(
23+
f"Redis connection failed, continuing without Redis: {e}"
24+
)
25+
26+
if redis_enable:
27+
redis = None
28+
try:
29+
redis = await connect_redis()
30+
logger.info("Redis connection established successfully")
31+
if redis and next_token:
32+
self_link = str(request.url)
33+
await save_self_link(redis, next_token, self_link)
34+
35+
prev_link = await get_prev_link(redis, token_param)
36+
if prev_link:
37+
links.insert(
38+
0,
39+
{
40+
"rel": "prev",
41+
"type": "application/json",
42+
"method": "GET",
43+
"href": prev_link,
44+
},
45+
)
46+
except Exception as e:
47+
logger.warning(
48+
f"Redis connection failed, continuing without Redis: {e}"
49+
)

stac_fastapi/core/stac_fastapi/core/redis_utils.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
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+
logger = logging.getLogger(__name__)
12+
913
redis_pool: Optional[aioredis.Redis] = None
1014

1115

@@ -122,3 +126,38 @@ async def get_prev_link(redis: aioredis.Redis, token: Optional[str]) -> Optional
122126
if not token:
123127
return None
124128
return await redis.get(f"nav:self:{token}")
129+
130+
131+
async def _handle_pagination_via_redis(
132+
redis_enable: bool,
133+
next_token: Optional[str],
134+
token_param: Optional[str],
135+
request: Request,
136+
links: List[Dict],
137+
) -> None:
138+
"""Handle Redis connection and operations for pagination links."""
139+
if not redis_enable:
140+
return
141+
142+
redis = None
143+
try:
144+
redis = await connect_redis()
145+
logger.info("Redis connection established successfully")
146+
147+
if redis and next_token:
148+
self_link = str(request.url)
149+
await save_self_link(redis, next_token, self_link)
150+
151+
prev_link = await get_prev_link(redis, token_param)
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+
)
162+
except Exception as e:
163+
logger.warning(f"Redis connection failed, continuing without Redis: {e}")

0 commit comments

Comments
 (0)