Skip to content

Commit

Permalink
move max datetime limit to ApiSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
hrodmn committed Dec 18, 2024
1 parent 24a756f commit 8f15dd5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
15 changes: 15 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,18 @@ def test_unbounded_start(app, xarray_query_params) -> None:
)

assert response.status_code == 400


def test_max_datetime(app, xarray_query_params) -> None:
"""Make sure a request that yields too many sub-requests returns a 400"""

response = app.get(
"/timeseries",
params={
**xarray_query_params,
"datetime": "2008-10-12T00:00:01Z/2024-10-12T23:59:59Z",
"step": "P1D",
},
)

assert response.status_code == 400
1 change: 1 addition & 0 deletions titiler/cmr/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ApiSettings(BaseSettings):
cors_origins: str = "*"
cachecontrol: str = "public, max-age=3600"
root_path: str = ""
time_series_max_requests: int = 995
debug: bool = False

model_config = {
Expand Down
10 changes: 10 additions & 0 deletions titiler/cmr/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from titiler.cmr.dependencies import ConceptID
from titiler.cmr.errors import InvalidDatetime
from titiler.cmr.factory import Endpoints
from titiler.cmr.settings import ApiSettings
from titiler.cmr.utils import parse_datetime
from titiler.core.algorithm import algorithms as available_algorithms
from titiler.core.dependencies import CoordCRSParams, DefaultDependency, DstCRSParams
Expand All @@ -50,6 +51,8 @@
from titiler.core.resources.enums import ImageType
from titiler.core.resources.responses import GeoJSONResponse

settings = ApiSettings()

# this section should eventually get moved to titiler.extensions.timeseries
timeseries_img_endpoint_params: Dict[str, Any] = {
"responses": {
Expand Down Expand Up @@ -342,6 +345,13 @@ def timeseries_cmr_query(
"list of comma-separated datetime strings",
)

if len(datetime_params) > settings.time_series_max_requests:
raise HTTPException(
status_code=400,
detail=f"this request ({len(datetime_params)}) exceeds the maximum number of distinct "
f"time series points/intervals of {settings.time_series_max_requests}",
)

return [
CMRQueryParameters(
concept_id=concept_id,
Expand Down
3 changes: 1 addition & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8f15dd5

Please sign in to comment.