Skip to content

Commit 654d46f

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
first commit
1 parent 7065798 commit 654d46f

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -269,34 +269,27 @@ async def all_collections(
269269
request = kwargs["request"]
270270
base_url = str(request.base_url)
271271

272-
# Get the global limit from environment variable
273-
global_limit = None
274-
env_limit = os.getenv("STAC_ITEM_LIMIT")
275-
if env_limit:
276-
try:
277-
global_limit = int(env_limit)
278-
except ValueError:
279-
# Handle invalid integer in environment variable
280-
pass
281-
282-
# Apply global limit if it exists
283-
if global_limit is not None:
284-
# If a limit was provided, use the smaller of the two
285-
if limit is not None:
286-
limit = min(limit, global_limit)
287-
else:
288-
limit = global_limit
272+
global_max_limit = int(os.getenv("STAC_COLLECTION_GLOBAL_MAX_LIMIT", 300))
273+
default_limit = int(os.getenv("STAC_DEFAULT_COLLECTION_LIMIT", 10))
274+
query_limit = request.query_params.get("limit")
275+
276+
body_limit = None
277+
try:
278+
if request.method == "POST" and request.body():
279+
body_data = await request.json()
280+
body_limit = body_data.get("limit")
281+
except:
282+
pass
283+
284+
if body_limit is not None:
285+
limit = int(body_limit)
286+
elif query_limit:
287+
limit = int(query_limit)
289288
else:
290-
# No global limit, use provided limit or default
291-
if limit is None:
292-
query_limit = request.query_params.get("limit")
293-
if query_limit:
294-
try:
295-
limit = int(query_limit)
296-
except ValueError:
297-
limit = 10
298-
else:
299-
limit = 10
289+
limit = default_limit
290+
291+
if global_max_limit > 0:
292+
limit = min(limit, global_max_limit)
300293

301294
token = request.query_params.get("token")
302295

@@ -660,7 +653,17 @@ async def get_search(
660653
Raises:
661654
HTTPException: If any error occurs while searching the catalog.
662655
"""
663-
limit = int(request.query_params.get("limit", os.getenv("STAC_ITEM_LIMIT", 10)))
656+
global_max_limit = int(os.getenv("STAC_ITEM_GLOBAL_MAX_LIMIT", 300))
657+
default_limit = int(os.getenv("STAC_DEFAULT_ITEM_LIMIT", 100))
658+
659+
query_limit = request.query_params.get("limit")
660+
if query_limit:
661+
limit = int(query_limit)
662+
else:
663+
limit = default_limit
664+
665+
if global_max_limit > 0:
666+
limit = min(limit, global_max_limit)
664667

665668
base_args = {
666669
"collections": collections,

stac_fastapi/tests/api/test_api.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,9 +1475,11 @@ def create_items(date_prefix: str, start_day: int, count: int) -> dict:
14751475

14761476

14771477
@pytest.mark.asyncio
1478-
async def test_collections_limit_env_variable(app_client, txn_client, load_test_data):
1478+
async def test_collections_limit_env_variable(
1479+
app_client, txn_client, load_test_data, monkeypatch
1480+
):
14791481
limit = "5"
1480-
os.environ["STAC_ITEM_LIMIT"] = limit
1482+
os.environ["STAC_DEFAULT_COLLECTION_LIMIT"] = limit
14811483
item = load_test_data("test_collection.json")
14821484

14831485
for i in range(10):
@@ -1497,7 +1499,7 @@ async def test_search_collection_limit_env_variable(
14971499
app_client, txn_client, load_test_data
14981500
):
14991501
limit = "5"
1500-
os.environ["STAC_ITEM_LIMIT"] = limit
1502+
os.environ["STAC_DEFAULT_ITEM_LIMIT"] = limit
15011503

15021504
test_collection = load_test_data("test_collection.json")
15031505
test_collection_id = "test-collection-search-limit"

0 commit comments

Comments
 (0)