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

Commit ef537e3

Browse files
author
Phil Varner
committed
add monitor link to Order
1 parent 2d68aed commit ef537e3

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

src/stapi_fastapi/routers/product_router.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from geojson_pydantic.geometries import Geometry
88
from returns.result import Failure, Success
99

10-
from stapi_fastapi.constants import TYPE_GEOJSON, TYPE_JSON
10+
from stapi_fastapi.constants import TYPE_JSON
1111
from stapi_fastapi.exceptions import ConstraintsException
1212
from stapi_fastapi.models.opportunity import (
1313
OpportunityCollection,
@@ -214,8 +214,8 @@ async def create_order(
214214
request,
215215
):
216216
case Success(order):
217+
self.root_router.add_order_links(order, request)
217218
location = str(self.root_router.generate_order_href(request, order.id))
218-
order.links.append(Link(href=location, rel="self", type=TYPE_GEOJSON))
219219
response.headers["Location"] = location
220220
return order
221221
case Failure(e) if isinstance(e, ConstraintsException):

src/stapi_fastapi/routers/root_router.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -190,21 +190,7 @@ async def get_order(self: Self, order_id: str, request: Request) -> Order:
190190
"""
191191
match await self.backend.get_order(order_id, request):
192192
case Success(Some(order)):
193-
order.links.extend(
194-
[
195-
Link(href=str(request.url), rel="self", type=TYPE_GEOJSON),
196-
Link(
197-
href=str(
198-
request.url_for(
199-
f"{self.name}:list-order-statuses",
200-
order_id=order_id,
201-
)
202-
),
203-
rel="monitor",
204-
type=TYPE_JSON,
205-
),
206-
]
207-
)
193+
self.add_order_links(order, request)
208194
return order
209195
case Success(Maybe.empty):
210196
raise NotFoundException("Order not found")
@@ -262,14 +248,35 @@ async def set_order_status(
262248
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
263249
detail="Error setting Order Status",
264250
)
265-
case _:
266-
raise AssertionError("Expected code to be unreachable")
251+
case x:
252+
raise AssertionError(f"Expected code to be unreachable {x}")
267253

268254
def add_product(self: Self, product: Product) -> None:
269255
# Give the include a prefix from the product router
270256
product_router = ProductRouter(product, self)
271257
self.include_router(product_router, prefix=f"/products/{product.id}")
272258
self.product_routers[product.id] = product_router
273259

274-
def generate_order_href(self: Self, request: Request, order_id: int | str) -> URL:
260+
def generate_order_href(self: Self, request: Request, order_id: str) -> URL:
275261
return request.url_for(f"{self.name}:get-order", order_id=order_id)
262+
263+
def generate_order_statuses_href(
264+
self: Self, request: Request, order_id: str
265+
) -> URL:
266+
return request.url_for(f"{self.name}:list-order-statuses", order_id=order_id)
267+
268+
def add_order_links(self, order: Order, request: Request):
269+
order.links.append(
270+
Link(
271+
href=str(self.generate_order_href(request, order.id)),
272+
rel="self",
273+
type=TYPE_GEOJSON,
274+
)
275+
)
276+
order.links.append(
277+
Link(
278+
href=str(self.generate_order_statuses_href(request, order.id)),
279+
rel="monitor",
280+
type=TYPE_JSON,
281+
),
282+
)

tests/test_order.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,25 @@ def test_new_order_location_header_matches_self_link(
6464
assert new_order_response.headers["Location"] == str(link["href"])
6565

6666

67+
@pytest.mark.parametrize("product_id", ["test-spotlight"])
68+
def test_new_order_links(new_order_response: Response, assert_link) -> None:
69+
order = new_order_response.json()
70+
assert_link(
71+
f"GET /orders/{order['id']}",
72+
order,
73+
"monitor",
74+
f"/orders/{order['id']}/statuses",
75+
)
76+
77+
assert_link(
78+
f"GET /orders/{order['id']}",
79+
order,
80+
"self",
81+
f"/orders/{order['id']}",
82+
media_type="application/geo+json",
83+
)
84+
85+
6786
@pytest.fixture
6887
def get_order_response(
6988
stapi_client: TestClient, new_order_response: Response
@@ -100,13 +119,12 @@ def test_get_order_properties(
100119

101120
@pytest.mark.parametrize("product_id", ["test-spotlight"])
102121
def test_order_status_after_create(
103-
get_order_response: Response, assert_link, stapi_client: TestClient
122+
get_order_response: Response, stapi_client: TestClient, assert_link
104123
) -> None:
105124
body = get_order_response.json()
106125
assert_link(
107126
f"GET /orders/{body['id']}", body, "monitor", f"/orders/{body['id']}/statuses"
108127
)
109-
110128
link = find_link(body["links"], "monitor")
111129

112130
res = stapi_client.get(link["href"])

0 commit comments

Comments
 (0)