Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

@theodorehreuter theodorehreuter Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice side effect of model change is that limit stays the same and just get carried through and all we have to update is the new pagination token for the returned body in the link object.

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