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

Commit 57b9f78

Browse files
author
Phil Varner
committed
add links to product and opportunity-collection
1 parent f25f51f commit 57b9f78

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
- Conformance endpoint `/conformance` and root body `conformsTo` attribute.
1313
- Endpoint /product/{productId}/order-parameters.
1414
- Links in Product entity to order-parameters and constraints endpoints for that product.
15+
- Add links `opportunities` and `create-order` to Product
16+
- Add link `create-order` to OpportunityCollection
1517

1618
### Changed
1719

src/stapi_fastapi/routers/product_router.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,24 @@ def get_product(self, request: Request) -> Product:
113113
rel="order-parameters",
114114
type=TYPE_JSON,
115115
),
116+
Link(
117+
href=str(
118+
request.url_for(
119+
f"{self.root_router.name}:{self.product.id}:search-opportunities",
120+
),
121+
),
122+
rel="opportunities",
123+
type=TYPE_JSON,
124+
),
125+
Link(
126+
href=str(
127+
request.url_for(
128+
f"{self.root_router.name}:{self.product.id}:create-order",
129+
),
130+
),
131+
rel="create-order",
132+
type=TYPE_JSON,
133+
),
116134
],
117135
)
118136

@@ -129,7 +147,20 @@ async def search_opportunities(
129147
except ConstraintsException as exc:
130148
raise HTTPException(status.HTTP_422_UNPROCESSABLE_ENTITY, detail=exc.detail)
131149

132-
return OpportunityCollection(features=opportunities)
150+
return OpportunityCollection(
151+
features=opportunities,
152+
links=[
153+
Link(
154+
href=str(
155+
request.url_for(
156+
f"{self.root_router.name}:{self.product.id}:create-order",
157+
),
158+
),
159+
rel="create-order",
160+
type=TYPE_JSON,
161+
),
162+
],
163+
)
133164

134165
async def get_product_constraints(self: Self) -> JsonSchemaModel:
135166
"""

tests/opportunity_test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import pytest
55
from fastapi.testclient import TestClient
6-
76
from stapi_fastapi.models.opportunity import Opportunity, OpportunityCollection
87

98
from .backends import MockProductBackend
@@ -16,6 +15,7 @@ def test_search_opportunities_response(
1615
mock_test_spotlight_opportunities: List[Opportunity],
1716
product_backend: MockProductBackend,
1817
stapi_client: TestClient,
18+
assert_link
1919
) -> None:
2020
product_backend._opportunities = mock_test_spotlight_opportunities
2121

@@ -50,9 +50,11 @@ def test_search_opportunities_response(
5050

5151
# Validate response status and structure
5252
assert response.status_code == 200, f"Failed for product: {product_id}"
53-
_json = response.json()
53+
body = response.json()
5454

5555
try:
56-
OpportunityCollection(**_json)
56+
oc = OpportunityCollection(**body)
5757
except Exception as _:
5858
pytest.fail("response is not an opportunity collection")
59+
60+
assert_link(f"POST {url}", body, "create-order", f"/products/{product_id}/order")

tests/product_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ def test_product_response_self_link(
3333
assert_link(
3434
url, body, "order-parameters", f"/products/{product_id}/order-parameters"
3535
)
36+
assert_link(
37+
url, body, "opportunities", f"/products/{product_id}/opportunities"
38+
)
39+
assert_link(
40+
url, body, "create-order", f"/products/{product_id}/order"
41+
)
3642

3743

3844
@pytest.mark.parametrize("product_id", ["test-spotlight"])

0 commit comments

Comments
 (0)