From 8baad02bfdeb2de72e3863ad49c5c119adc5c212 Mon Sep 17 00:00:00 2001 From: Theodore Reuter Date: Wed, 5 Feb 2025 11:10:39 -0500 Subject: [PATCH] Add pagination token + limit to OpportunityRequest model (#139) * fix: reworked search opportunity request model so limit and paginatino token are in POST body, and changed tests and implementation to refelct this change in the model --- src/stapi_fastapi/models/opportunity.py | 3 ++ src/stapi_fastapi/routers/product_router.py | 20 ++++----- tests/test_opportunity.py | 48 ++++++++++----------- 3 files changed, 33 insertions(+), 38 deletions(-) diff --git a/src/stapi_fastapi/models/opportunity.py b/src/stapi_fastapi/models/opportunity.py index 930cfcf..0de3385 100644 --- a/src/stapi_fastapi/models/opportunity.py +++ b/src/stapi_fastapi/models/opportunity.py @@ -21,6 +21,9 @@ class OpportunityRequest(BaseModel): geometry: Geometry # TODO: validate the CQL2 filter? filter: CQL2Filter | None = None + next: str | None = None + limit: int = 10 + model_config = ConfigDict(strict=True) diff --git a/src/stapi_fastapi/routers/product_router.py b/src/stapi_fastapi/routers/product_router.py index 8dbd031..175c763 100644 --- a/src/stapi_fastapi/routers/product_router.py +++ b/src/stapi_fastapi/routers/product_router.py @@ -2,9 +2,9 @@ import logging import traceback -from typing import TYPE_CHECKING, Annotated, Self +from typing import TYPE_CHECKING, Self -from fastapi import APIRouter, Body, HTTPException, Request, Response, status +from fastapi import APIRouter, HTTPException, Request, Response, status from geojson_pydantic.geometries import Geometry from returns.maybe import Some from returns.result import Failure, Success @@ -165,8 +165,6 @@ async def search_opportunities( self, search: OpportunityRequest, request: Request, - next: Annotated[str | None, Body()] = None, - limit: Annotated[int, Body()] = 10, ) -> OpportunityCollection: """ Explore the opportunities available for a particular set of constraints @@ -175,18 +173,16 @@ async def search_opportunities( match await self.product._search_opportunities( self, search, - next, - limit, + search.next, + search.limit, request, ): case Success((features, Some(pagination_token))): links.append(self.order_link(request)) - body = { - "search": search.model_dump(mode="json"), - "next": pagination_token, - "limit": limit, - } - links.append(self.pagination_link(request, body)) + search.next = pagination_token + links.append( + self.pagination_link(request, search.model_dump(mode="json")) + ) case Success((features, Nothing)): # noqa: F841 links.append(self.order_link(request)) case Failure(e) if isinstance(e, ConstraintsException): diff --git a/tests/test_opportunity.py b/tests/test_opportunity.py index 389d47d..bfb604d 100644 --- a/tests/test_opportunity.py +++ b/tests/test_opportunity.py @@ -24,19 +24,17 @@ def test_search_opportunities_response( end_string = rfc3339_strftime(end, format) request_payload = { - "search": { - "geometry": { - "type": "Point", - "coordinates": [0, 0], - }, - "datetime": f"{start_string}/{end_string}", - "filter": { - "op": "and", - "args": [ - {"op": ">", "args": [{"property": "off_nadir"}, 0]}, - {"op": "<", "args": [{"property": "off_nadir"}, 45]}, - ], - }, + "geometry": { + "type": "Point", + "coordinates": [0, 0], + }, + "datetime": f"{start_string}/{end_string}", + "filter": { + "op": "and", + "args": [ + {"op": ">", "args": [{"property": "off_nadir"}, 0]}, + {"op": "<", "args": [{"property": "off_nadir"}, 45]}, + ], }, "limit": 10, } @@ -81,19 +79,17 @@ def test_search_opportunities_pagination( end_string = rfc3339_strftime(end, format) request_payload = { - "search": { - "geometry": { - "type": "Point", - "coordinates": [0, 0], - }, - "datetime": f"{start_string}/{end_string}", - "filter": { - "op": "and", - "args": [ - {"op": ">", "args": [{"property": "off_nadir"}, 0]}, - {"op": "<", "args": [{"property": "off_nadir"}, 45]}, - ], - }, + "geometry": { + "type": "Point", + "coordinates": [0, 0], + }, + "datetime": f"{start_string}/{end_string}", + "filter": { + "op": "and", + "args": [ + {"op": ">", "args": [{"property": "off_nadir"}, 0]}, + {"op": "<", "args": [{"property": "off_nadir"}, 45]}, + ], }, "limit": limit, }