|
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 redis_pagination_links |
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 |
@@ -270,6 +270,7 @@ async def all_collections( |
270 | 270 | A Collections object containing all the collections in the database and links to various resources. |
271 | 271 | """ |
272 | 272 | base_url = str(request.base_url) |
| 273 | + redis_enable = get_bool_env("REDIS_ENABLE", default=False) |
273 | 274 |
|
274 | 275 | # Get the global limit from environment variable |
275 | 276 | global_limit = None |
@@ -329,20 +330,6 @@ async def all_collections( |
329 | 330 | if parsed_sort: |
330 | 331 | sort = parsed_sort |
331 | 332 |
|
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 | | - |
346 | 333 | # Convert q to a list if it's a string |
347 | 334 | q_list = None |
348 | 335 | if q is not None: |
@@ -441,21 +428,13 @@ async def all_collections( |
441 | 428 | }, |
442 | 429 | ] |
443 | 430 |
|
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 | | - ) |
| 431 | + if redis_enable: |
| 432 | + await redis_pagination_links( |
| 433 | + current_url=str(request.url), |
| 434 | + token=token, |
| 435 | + next_token=next_token, |
| 436 | + links=links, |
| 437 | + ) |
459 | 438 |
|
460 | 439 | if next_token: |
461 | 440 | next_link = PagingLinks(next=next_token, request=request).link_next() |
@@ -775,9 +754,8 @@ async def post_search( |
775 | 754 | HTTPException: If there is an error with the cql2_json filter. |
776 | 755 | """ |
777 | 756 | base_url = str(request.base_url) |
778 | | - redis_enable = get_bool_env("REDIS_ENABLE", default=False) |
779 | | - |
780 | 757 | search = self.database.make_search() |
| 758 | + redis_enable = get_bool_env("REDIS_ENABLE", default=False) |
781 | 759 |
|
782 | 760 | if search_request.ids: |
783 | 761 | search = self.database.apply_ids_filter( |
@@ -902,28 +880,12 @@ async def post_search( |
902 | 880 | links.extend(collection_links) |
903 | 881 |
|
904 | 882 | 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 | | - ) |
| 883 | + await redis_pagination_links( |
| 884 | + current_url=str(request.url), |
| 885 | + token=token_param, |
| 886 | + next_token=next_token, |
| 887 | + links=links, |
| 888 | + ) |
927 | 889 |
|
928 | 890 | return stac_types.ItemCollection( |
929 | 891 | type="FeatureCollection", |
|
0 commit comments