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

Commit 16f4dc2

Browse files
feat: tighened up logic in root_router.py to only add pagination link object if we are returned a token. tests: created simple test implementation to abstract out token handling and generation
1 parent 7734317 commit 16f4dc2

File tree

3 files changed

+52
-24
lines changed

3 files changed

+52
-24
lines changed

src/stapi_fastapi/routers/root_router.py

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

162162
async def get_orders(
163-
self, request: Request, next_token: str | None = None, limit: int | None = None
163+
self, request: Request, next: str | None = None, limit: int | None = None
164164
) -> OrderCollection:
165-
match await self.backend.get_orders(request, next_token, limit):
165+
match await self.backend.get_orders(request, next, limit):
166166
case Success((collections, token)):
167167
for order in collections:
168168
order.links.append(
@@ -176,18 +176,14 @@ async def get_orders(
176176
type=TYPE_JSON,
177177
)
178178
)
179-
if next_token:
179+
if token: # only return pagination link if backend returns token
180180
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-
181+
params = {
182+
param.split("=")[0]: param.split("=")[1]
183+
for param in query.split("&")
184+
}
185+
params["next"] = token # replace/add token
186+
updated_url = request.url.replace_query_params(**params)
191187
collections.links.append(
192188
Link(href=str(updated_url), rel="next", type=TYPE_JSON)
193189
)

tests/application.py

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
OrderCollection,
2222
OrderParameters,
2323
OrderPayload,
24-
Orders,
2524
OrderStatus,
2625
OrderStatusCode,
2726
OrderStatusPayload,
@@ -45,18 +44,51 @@ def __init__(self, orders: InMemoryOrderDB) -> None:
4544
self._orders_db: InMemoryOrderDB = orders
4645

4746
async def get_orders(
48-
self, request: Request, next_token: str, limit: int
49-
) -> ResultE[Orders]:
47+
self, request: Request, next: str | None = None, limit: int | None = None
48+
) -> ResultE[tuple[OrderCollection, str]]:
5049
"""
5150
Show all orders.
5251
"""
53-
return Success(
54-
Orders(
55-
collection=OrderCollection(
56-
features=list(self._orders_db._orders.values())
57-
),
58-
token="a",
52+
# it limit does NOT reach the last index in the db list THEN we return token
53+
54+
# if no limit - no token since getting all records - return no token
55+
# backend determines if we return a token
56+
if next: # initial implementation - if given a token, assume we continue pagination and return a new token
57+
features = list(self._orders_db._orders.values())
58+
59+
# get records based on limit
60+
def token_processor(next: str) -> tuple[str, int]:
61+
"""process token to return new token and start loc"""
62+
return "new_token", 0
63+
64+
token, start = token_processor(next)
65+
if limit:
66+
if start + limit > len(features):
67+
features = features[start:]
68+
else:
69+
features = features[start:limit]
70+
return Success((OrderCollection(features=features), token))
71+
else: # if no limit with token
72+
return Success((OrderCollection(features=features[start:]), ""))
73+
else: # no input token means give us everything and return no token
74+
return Success(
75+
(OrderCollection(features=list(self._orders_db._orders.values())), "")
5976
)
77+
# need to be agnostic to token here. If we have MORE records we COULD return - i.e. final index found by limit, then we return a token. If we return the last record THEN return EMPTY token.
78+
# token = ''
79+
# if not limit:
80+
# limit = 0
81+
# # parse token here and do stuff to get starting index
82+
# start_index = 0
83+
# end_index = start_index + limit
84+
# if not limit:
85+
# collection = OrderCollection(features=list(self._orders_db._orders.values()))
86+
# else:
87+
# all_orders = list(self._orders_db._orders.values())
88+
# features = [all_orders[i] for i in indicies if 0 <= i < len(lst)]
89+
# collection =
90+
return Success(
91+
(OrderCollection(features=list(self._orders_db._orders.values())), "")
6092
)
6193

6294
async def get_order(self, order_id: str, request: Request) -> ResultE[Maybe[Order]]:

tests/test_order.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,13 @@ def test_order_pagination(
222222
)
223223

224224
# call all orders
225-
res = stapi_client.get("/orders", params={"next_token": OLD_TOKEN, "limit": 1})
225+
res = stapi_client.get("/orders", params={"next": OLD_TOKEN, "limit": 1})
226226
checker = res.json()
227227

228228
assert res.status_code == status.HTTP_200_OK
229229

230230
# temp check to make sure token link isn't added to inside collection
231-
for link in checker["features"][1]["links"]:
231+
for link in checker["features"][0]["links"]:
232232
assert link["rel"] != "next"
233233
assert checker["links"] != []
234234

0 commit comments

Comments
 (0)