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

Commit 7949857

Browse files
feat: changed so /opportunities no longer can accept query params and next/limit must be passed as key/value pairs in the POST body. These are also now returned in the POST body in the 'next' link object returned by /opportunities tests: update tests to reflect changes in endpoint. POST body is now extracted wholesale from returned link object istead of just the params
1 parent 3a2d9c1 commit 7949857

File tree

3 files changed

+44
-36
lines changed

3 files changed

+44
-36
lines changed

src/stapi_fastapi/routers/product_router.py

Lines changed: 9 additions & 6 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, Self
5+
from typing import TYPE_CHECKING, Annotated, Self
66

7-
from fastapi import APIRouter, HTTPException, Request, Response, status
7+
from fastapi import APIRouter, Body, 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,8 @@ async def search_opportunities(
165165
self,
166166
search: OpportunityRequest,
167167
request: Request,
168-
next: str | None = None,
169-
limit: int = 10,
168+
next: Annotated[str | None, Body()] = None,
169+
limit: Annotated[int, Body()] = 10,
170170
) -> OpportunityCollection:
171171
"""
172172
Explore the opportunities available for a particular set of constraints
@@ -177,8 +177,11 @@ async def search_opportunities(
177177
):
178178
case Success((features, Some(pagination_token))):
179179
links.append(self.order_link(request))
180-
body = search.model_dump(mode="json")
181-
body["next"] = pagination_token
180+
body = {
181+
"search": search.model_dump(mode="json"),
182+
"next": pagination_token,
183+
"limit": limit,
184+
}
182185
links.append(self.pagination_link(request, body))
183186
case Success((features, Nothing)): # noqa: F841
184187
links.append(self.order_link(request))

tests/conftest.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ def pagination_tester(
177177
while next_url:
178178
url = next_url
179179
if method == "POST":
180-
next_url = next(
181-
(d["body"]["next"] for d in resp_body["links"] if d["rel"] == "next"),
180+
body = next(
181+
(d["body"] for d in resp_body["links"] if d["rel"] == "next"), None
182182
)
183183

184184
res = make_request(stapi_client, url, method, body, next_url, limit)
@@ -192,10 +192,6 @@ def pagination_tester(
192192
next_url = next(
193193
(d["href"] for d in resp_body["links"] if d["rel"] == "next"), None
194194
)
195-
body = next(
196-
(d.get("body") for d in resp_body["links"] if d.get("body")),
197-
None,
198-
)
199195
else:
200196
next_url = None
201197

@@ -221,6 +217,6 @@ def make_request(
221217
if method == "GET":
222218
res = stapi_client.get(endpoint, params=params)
223219
if method == "POST":
224-
res = stapi_client.post(endpoint, json=body, params=params)
220+
res = stapi_client.post(endpoint, json=body)
225221

226222
return res

tests/test_opportunity.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
from geojson_pydantic import Point
88
from geojson_pydantic.types import Position2D
99

10-
from stapi_fastapi.models.opportunity import Opportunity, OpportunityCollection
10+
from stapi_fastapi.models.opportunity import (
11+
Opportunity,
12+
OpportunityCollection,
13+
)
1114
from tests.application import MyOpportunityProperties
1215
from tests.conftest import pagination_tester
1316

@@ -65,18 +68,21 @@ def test_search_opportunities_response(
6568
end_string = rfc3339_strftime(end, format)
6669

6770
request_payload = {
68-
"geometry": {
69-
"type": "Point",
70-
"coordinates": [0, 0],
71-
},
72-
"datetime": f"{start_string}/{end_string}",
73-
"filter": {
74-
"op": "and",
75-
"args": [
76-
{"op": ">", "args": [{"property": "off_nadir"}, 0]},
77-
{"op": "<", "args": [{"property": "off_nadir"}, 45]},
78-
],
71+
"search": {
72+
"geometry": {
73+
"type": "Point",
74+
"coordinates": [0, 0],
75+
},
76+
"datetime": f"{start_string}/{end_string}",
77+
"filter": {
78+
"op": "and",
79+
"args": [
80+
{"op": ">", "args": [{"property": "off_nadir"}, 0]},
81+
{"op": "<", "args": [{"property": "off_nadir"}, 45]},
82+
],
83+
},
7984
},
85+
"limit": 10,
8086
}
8187

8288
url = f"/products/{product_id}/opportunities"
@@ -117,18 +123,21 @@ def test_search_opportunities_pagination(
117123
end_string = rfc3339_strftime(end, format)
118124

119125
request_payload = {
120-
"geometry": {
121-
"type": "Point",
122-
"coordinates": [0, 0],
123-
},
124-
"datetime": f"{start_string}/{end_string}",
125-
"filter": {
126-
"op": "and",
127-
"args": [
128-
{"op": ">", "args": [{"property": "off_nadir"}, 0]},
129-
{"op": "<", "args": [{"property": "off_nadir"}, 45]},
130-
],
126+
"search": {
127+
"geometry": {
128+
"type": "Point",
129+
"coordinates": [0, 0],
130+
},
131+
"datetime": f"{start_string}/{end_string}",
132+
"filter": {
133+
"op": "and",
134+
"args": [
135+
{"op": ">", "args": [{"property": "off_nadir"}, 0]},
136+
{"op": "<", "args": [{"property": "off_nadir"}, 45]},
137+
],
138+
},
131139
},
140+
"limit": limit,
132141
}
133142

134143
pagination_tester(

0 commit comments

Comments
 (0)