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

Commit ac661d9

Browse files
feat: adding pagination for GET orders/order_id/statuses inputs to root_router and fleshing out mock root backend
1 parent d3c5aa8 commit ac661d9

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

src/stapi_fastapi/backends/root_backend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ async def get_order(self, order_id: str, request: Request) -> ResultE[Maybe[Orde
3535
...
3636

3737
async def get_order_statuses(
38-
self, order_id: str, request: Request
39-
) -> ResultE[list[T]]:
38+
self, order_id: str, request: Request, next: str | None, limit: int
39+
) -> ResultE[tuple[list[T], str]]:
4040
"""
4141
Get statuses for order with `order_id`.
4242

src/stapi_fastapi/routers/product_router.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def __init__(
4747
tags=["Products"],
4848
)
4949

50+
# Any paginated POST or requires a body needs the same POST body to be sent
51+
# method property on Link object needs to be set to POST
52+
# next link for GET can just include next token dont need to specify method or body
53+
# where there's a POST body, have to include post body body property is request.body()
5054
self.add_api_route(
5155
path="/opportunities",
5256
endpoint=self.search_opportunities,
@@ -171,7 +175,7 @@ async def search_opportunities(
171175
return OpportunityCollection(
172176
features=features,
173177
links=[
174-
Link(
178+
Link( # current bug is missing method set and setting body for
175179
href=str(
176180
request.url_for(
177181
f"{self.root_router.name}:{self.product.id}:create-order",

src/stapi_fastapi/routers/root_router.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,13 @@ async def get_order(self: Self, order_id: str, request: Request) -> Order:
241241
raise AssertionError("Expected code to be unreachable")
242242

243243
async def get_order_statuses(
244-
self: Self, order_id: str, request: Request
244+
self: Self,
245+
order_id: str,
246+
request: Request,
247+
next: str | None = None,
248+
limit: int = 10,
245249
) -> OrderStatuses:
246-
match await self.backend.get_order_statuses(order_id, request):
250+
match await self.backend.get_order_statuses(order_id, request, next, limit):
247251
case Success(statuses):
248252
return OrderStatuses(
249253
statuses=statuses,

tests/application.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,29 @@ async def get_order(self, order_id: str, request: Request) -> ResultE[Maybe[Orde
8080
return Success(Maybe.from_optional(self._orders_db._orders.get(order_id)))
8181

8282
async def get_order_statuses(
83-
self, order_id: str, request: Request
84-
) -> ResultE[list[OrderStatus]]:
85-
return Success(self._orders_db._statuses[order_id])
83+
self, order_id: str, request: Request, next: str | None, limit: int
84+
) -> ResultE[tuple[list[OrderStatus], str]]:
85+
try:
86+
start = 0
87+
if limit > 100:
88+
limit = 100
89+
statuses = self._orders_db._statuses[order_id]
90+
91+
if next:
92+
start = int(next)
93+
if not statuses and not next:
94+
return Success(([], ""))
95+
96+
end = start + limit
97+
stati = statuses[start:end]
98+
99+
next = ""
100+
if end < len(statuses):
101+
next = str(end)
102+
return Success((stati, next))
103+
# return Success(self._orders_db._statuses[order_id])
104+
except Exception as e:
105+
return Failure(e)
86106

87107

88108
class MockProductBackend(ProductBackend):

0 commit comments

Comments
 (0)