|
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 ( |
| 28 | + connect_redis, |
| 29 | + get_prev_link, |
| 30 | + handle_redis_pagination, |
| 31 | + save_self_link, |
| 32 | +) |
28 | 33 | from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer |
29 | 34 | from stac_fastapi.core.session import Session |
30 | 35 | from stac_fastapi.core.utilities import filter_fields, get_bool_env |
@@ -332,17 +337,6 @@ async def all_collections( |
332 | 337 | current_url = str(request.url) |
333 | 338 | redis_enable = get_bool_env("REDIS_ENABLE", default=False) |
334 | 339 |
|
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 | 340 | # Convert q to a list if it's a string |
347 | 341 | q_list = None |
348 | 342 | if q is not None: |
@@ -441,6 +435,17 @@ async def all_collections( |
441 | 435 | }, |
442 | 436 | ] |
443 | 437 |
|
| 438 | + redis = None |
| 439 | + if redis_enable: |
| 440 | + try: |
| 441 | + redis = await connect_redis() |
| 442 | + logger.info("Redis connection established successfully") |
| 443 | + except Exception as e: |
| 444 | + redis = None |
| 445 | + logger.warning( |
| 446 | + f"Redis connection failed, continuing without Redis: {e}" |
| 447 | + ) |
| 448 | + |
444 | 449 | if redis_enable and redis: |
445 | 450 | if next_token: |
446 | 451 | await save_self_link(redis, next_token, current_url) |
@@ -775,7 +780,6 @@ async def post_search( |
775 | 780 | HTTPException: If there is an error with the cql2_json filter. |
776 | 781 | """ |
777 | 782 | base_url = str(request.base_url) |
778 | | - redis_enable = get_bool_env("REDIS_ENABLE", default=False) |
779 | 783 |
|
780 | 784 | search = self.database.make_search() |
781 | 785 |
|
@@ -900,30 +904,33 @@ async def post_search( |
900 | 904 | ] |
901 | 905 | ) |
902 | 906 | links.extend(collection_links) |
| 907 | + redis_enable = get_bool_env("REDIS_ENABLE", default=False) |
903 | 908 |
|
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 | | - ) |
| 909 | + handle_redis_pagination(request, links, next_token, token_param, redis_enable) |
| 910 | + |
| 911 | + # if redis_enable: |
| 912 | + # redis = None |
| 913 | + # try: |
| 914 | + # redis = await connect_redis() |
| 915 | + # logger.info("Redis connection established successfully") |
| 916 | + # self_link = str(request.url) |
| 917 | + # await save_self_link(redis, next_token, self_link) |
| 918 | + |
| 919 | + # prev_link = await get_prev_link(redis, token_param) |
| 920 | + # if prev_link: |
| 921 | + # links.insert( |
| 922 | + # 0, |
| 923 | + # { |
| 924 | + # "rel": "prev", |
| 925 | + # "type": "application/json", |
| 926 | + # "method": "GET", |
| 927 | + # "href": prev_link, |
| 928 | + # }, |
| 929 | + # ) |
| 930 | + # except Exception as e: |
| 931 | + # logger.warning( |
| 932 | + # f"Redis connection failed, continuing without Redis: {e}" |
| 933 | + # ) |
927 | 934 |
|
928 | 935 | return stac_types.ItemCollection( |
929 | 936 | type="FeatureCollection", |
|
0 commit comments