|
24 | 24 | from stac_fastapi.core.base_settings import ApiBaseSettings |
25 | 25 | from stac_fastapi.core.datetime_utils import format_datetime_range |
26 | 26 | 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 |
28 | 28 | from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer |
29 | 29 | from stac_fastapi.core.session import Session |
30 | 30 | from stac_fastapi.core.utilities import filter_fields, get_bool_env |
@@ -329,20 +329,8 @@ async def all_collections( |
329 | 329 | if parsed_sort: |
330 | 330 | sort = parsed_sort |
331 | 331 |
|
332 | | - current_url = str(request.url) |
333 | 332 | redis_enable = get_bool_env("REDIS_ENABLE", default=False) |
334 | 333 |
|
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 | | - |
346 | 334 | # Convert q to a list if it's a string |
347 | 335 | q_list = None |
348 | 336 | if q is not None: |
@@ -441,21 +429,7 @@ async def all_collections( |
441 | 429 | }, |
442 | 430 | ] |
443 | 431 |
|
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) |
459 | 433 |
|
460 | 434 | if next_token: |
461 | 435 | next_link = PagingLinks(next=next_token, request=request).link_next() |
@@ -901,29 +875,9 @@ async def post_search( |
901 | 875 | ) |
902 | 876 | links.extend(collection_links) |
903 | 877 |
|
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 | + ) |
927 | 881 |
|
928 | 882 | return stac_types.ItemCollection( |
929 | 883 | type="FeatureCollection", |
|
0 commit comments