Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.

Commit 8baad02

Browse files
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
1 parent e82101f commit 8baad02

File tree

3 files changed

+33
-38
lines changed

3 files changed

+33
-38
lines changed

src/stapi_fastapi/models/opportunity.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class OpportunityRequest(BaseModel):
2121
geometry: Geometry
2222
# TODO: validate the CQL2 filter?
2323
filter: CQL2Filter | None = None
24+
next: str | None = None
25+
limit: int = 10
26+
2427
model_config = ConfigDict(strict=True)
2528

2629

src/stapi_fastapi/routers/product_router.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import logging
44
import traceback
5-
from typing import TYPE_CHECKING, Annotated, Self
5+
from typing import TYPE_CHECKING, Self
66

7-
from fastapi import APIRouter, Body, HTTPException, Request, Response, status
7+
from fastapi import APIRouter, HTTPException, Request, Response, status
88
from geojson_pydantic.geometries import Geometry
99
from returns.maybe import Some
1010
from returns.result import Failure, Success
@@ -165,8 +165,6 @@ async def search_opportunities(
165165
self,
166166
search: OpportunityRequest,
167167
request: Request,
168-
next: Annotated[str | None, Body()] = None,
169-
limit: Annotated[int, Body()] = 10,
170168
) -> OpportunityCollection:
171169
"""
172170
Explore the opportunities available for a particular set of constraints
@@ -175,18 +173,16 @@ async def search_opportunities(
175173
match await self.product._search_opportunities(
176174
self,
177175
search,
178-
next,
179-
limit,
176+
search.next,
177+
search.limit,
180178
request,
181179
):
182180
case Success((features, Some(pagination_token))):
183181
links.append(self.order_link(request))
184-
body = {
185-
"search": search.model_dump(mode="json"),
186-
"next": pagination_token,
187-
"limit": limit,
188-
}
189-
links.append(self.pagination_link(request, body))
182+
search.next = pagination_token
183+
links.append(
184+
self.pagination_link(request, search.model_dump(mode="json"))
185+
)
190186
case Success((features, Nothing)): # noqa: F841
191187
links.append(self.order_link(request))
192188
case Failure(e) if isinstance(e, ConstraintsException):

tests/test_opportunity.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,17 @@ def test_search_opportunities_response(
2424
end_string = rfc3339_strftime(end, format)
2525

2626
request_payload = {
27-
"search": {
28-
"geometry": {
29-
"type": "Point",
30-
"coordinates": [0, 0],
31-
},
32-
"datetime": f"{start_string}/{end_string}",
33-
"filter": {
34-
"op": "and",
35-
"args": [
36-
{"op": ">", "args": [{"property": "off_nadir"}, 0]},
37-
{"op": "<", "args": [{"property": "off_nadir"}, 45]},
38-
],
39-
},
27+
"geometry": {
28+
"type": "Point",
29+
"coordinates": [0, 0],
30+
},
31+
"datetime": f"{start_string}/{end_string}",
32+
"filter": {
33+
"op": "and",
34+
"args": [
35+
{"op": ">", "args": [{"property": "off_nadir"}, 0]},
36+
{"op": "<", "args": [{"property": "off_nadir"}, 45]},
37+
],
4038
},
4139
"limit": 10,
4240
}
@@ -81,19 +79,17 @@ def test_search_opportunities_pagination(
8179
end_string = rfc3339_strftime(end, format)
8280

8381
request_payload = {
84-
"search": {
85-
"geometry": {
86-
"type": "Point",
87-
"coordinates": [0, 0],
88-
},
89-
"datetime": f"{start_string}/{end_string}",
90-
"filter": {
91-
"op": "and",
92-
"args": [
93-
{"op": ">", "args": [{"property": "off_nadir"}, 0]},
94-
{"op": "<", "args": [{"property": "off_nadir"}, 45]},
95-
],
96-
},
82+
"geometry": {
83+
"type": "Point",
84+
"coordinates": [0, 0],
85+
},
86+
"datetime": f"{start_string}/{end_string}",
87+
"filter": {
88+
"op": "and",
89+
"args": [
90+
{"op": ">", "args": [{"property": "off_nadir"}, 0]},
91+
{"op": "<", "args": [{"property": "off_nadir"}, 45]},
92+
],
9793
},
9894
"limit": limit,
9995
}

0 commit comments

Comments
 (0)