Skip to content

Commit

Permalink
Add pagination token + limit to OpportunityRequest model (#139)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
theodorehreuter authored Feb 5, 2025
1 parent e82101f commit 8baad02
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 38 deletions.
3 changes: 3 additions & 0 deletions src/stapi_fastapi/models/opportunity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
20 changes: 8 additions & 12 deletions src/stapi_fastapi/routers/product_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
48 changes: 22 additions & 26 deletions tests/test_opportunity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down

0 comments on commit 8baad02

Please sign in to comment.