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

Commit 7734317

Browse files
feat: correctly adding pagination link object to top level collection feat: replacing token in url if new token is provided tests: continue to updae test to validate additional pieces of the limit/token expected functionality, still breaking on limit check feat: removing use or Orders type and going with tuple instead
1 parent 0dcf05b commit 7734317

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

src/stapi_fastapi/backends/root_backend.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66

77
from stapi_fastapi.models.order import (
88
Order,
9-
Orders,
9+
OrderCollection,
1010
OrderStatus,
1111
OrderStatusPayload,
1212
)
1313

1414

1515
class RootBackend[T: OrderStatusPayload, U: OrderStatus](Protocol): # pragma: nocover
1616
async def get_orders(
17-
self, request: Request, next_token: str, limit: int
18-
) -> ResultE[Orders]:
17+
self, request: Request, next: str | None, limit: int | None
18+
) -> ResultE[tuple[OrderCollection, str]]:
1919
"""
20-
Return a list of existing orders.
20+
Return a list of existing orders and pagination token if applicable
21+
No pagination will return empty string for token
2122
"""
2223
...
2324

src/stapi_fastapi/models/order.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,6 @@ def __getitem__(self, index: int) -> Order:
113113
return self.features[index]
114114

115115

116-
class Orders(BaseModel):
117-
collection: OrderCollection
118-
token: str
119-
120-
121116
class OrderPayload(BaseModel, Generic[ORP]):
122117
datetime: DatetimeInterval
123118
geometry: Geometry

src/stapi_fastapi/routers/root_router.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ def get_products(self, request: Request) -> ProductsCollection:
160160
)
161161

162162
async def get_orders(
163-
self, request: Request, next_token: str, limit: int
163+
self, request: Request, next_token: str | None = None, limit: int | None = None
164164
) -> OrderCollection:
165165
match await self.backend.get_orders(request, next_token, limit):
166-
case Success(orders):
167-
for order in orders.collection:
166+
case Success((collections, token)):
167+
for order in collections:
168168
order.links.append(
169169
Link(
170170
href=str(
@@ -176,7 +176,22 @@ async def get_orders(
176176
type=TYPE_JSON,
177177
)
178178
)
179-
return orders.collection
179+
if next_token:
180+
query = request.url.components.query
181+
if query: # check url for params
182+
params = {
183+
param.split("=")[0]: param.split("=")[1]
184+
for param in query.split("&")
185+
}
186+
params["next_token"] = token # replace old token if exists
187+
updated_url = request.url.replace_query_params(**params)
188+
else: # add if doesn't exist
189+
updated_url = request.url.include_query_params(token=token)
190+
191+
collections.links.append(
192+
Link(href=str(updated_url), rel="next", type=TYPE_JSON)
193+
)
194+
return collections
180195
case Failure(e):
181196
logging.exception("An error occurred while retrieving orders", e)
182197
raise HTTPException(

tests/test_order.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,11 @@ def test_order_pagination(
196196
create_second_order_allowed_payloads: list[OrderPayload],
197197
) -> None:
198198
product_backend._allowed_payloads = create_order_allowed_payloads
199+
OLD_TOKEN = "a_token"
199200

200201
# check empty
201-
res = stapi_client.get("/orders", params={"next_token": "a", "limit": 1})
202+
res = stapi_client.get("/orders")
203+
202204
default_orders = {"type": "FeatureCollection", "features": [], "links": []}
203205

204206
assert res.status_code == status.HTTP_200_OK
@@ -220,9 +222,17 @@ def test_order_pagination(
220222
)
221223

222224
# call all orders
223-
res = stapi_client.get("/orders", params={"next_token": "a", "limit": 1})
225+
res = stapi_client.get("/orders", params={"next_token": OLD_TOKEN, "limit": 1})
224226
checker = res.json()
225227

226228
assert res.status_code == status.HTTP_200_OK
227-
assert len(checker["features"]) == 1
229+
230+
# temp check to make sure token link isn't added to inside collection
231+
for link in checker["features"][1]["links"]:
232+
assert link["rel"] != "next"
228233
assert checker["links"] != []
234+
235+
# check to make sure new token in link
236+
assert OLD_TOKEN not in checker["links"][0]["href"]
237+
238+
assert len(checker["features"]) == 1

0 commit comments

Comments
 (0)