Skip to content

Commit 6cdcd8f

Browse files
author
Phil Varner
committed
refactor method url_for in ProductRouter and RootRouter
1 parent 9afccdc commit 6cdcd8f

File tree

3 files changed

+32
-48
lines changed

3 files changed

+32
-48
lines changed

stapi-fastapi/src/stapi_fastapi/routers/product_router.py

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -199,50 +199,34 @@ async def _create_order(
199199
tags=["Products"],
200200
)
201201

202+
@staticmethod
203+
def url_for(request: Request, name: str, /, **path_params: Any) -> str:
204+
return str(request.url_for(name, **path_params))
205+
202206
def get_product(self, request: Request) -> ProductPydantic:
203207
links = [
204208
Link(
205-
href=str(
206-
request.url_for(
207-
f"{self.root_router.name}:{self.product.id}:{GET_PRODUCT}",
208-
),
209-
),
209+
href=self.url_for(request, f"{self.root_router.name}:{self.product.id}:{GET_PRODUCT}"),
210210
rel="self",
211211
type=TYPE_JSON,
212212
),
213213
Link(
214-
href=str(
215-
request.url_for(
216-
f"{self.root_router.name}:{self.product.id}:{CONFORMANCE}",
217-
),
218-
),
214+
href=self.url_for(request, f"{self.root_router.name}:{self.product.id}:{CONFORMANCE}"),
219215
rel="conformance",
220216
type=TYPE_JSON,
221217
),
222218
Link(
223-
href=str(
224-
request.url_for(
225-
f"{self.root_router.name}:{self.product.id}:{GET_QUERYABLES}",
226-
),
227-
),
219+
href=self.url_for(request, f"{self.root_router.name}:{self.product.id}:{GET_QUERYABLES}"),
228220
rel="queryables",
229221
type=TYPE_JSON,
230222
),
231223
Link(
232-
href=str(
233-
request.url_for(
234-
f"{self.root_router.name}:{self.product.id}:{GET_ORDER_PARAMETERS}",
235-
),
236-
),
224+
href=self.url_for(request, f"{self.root_router.name}:{self.product.id}:{GET_ORDER_PARAMETERS}"),
237225
rel="order-parameters",
238226
type=TYPE_JSON,
239227
),
240228
Link(
241-
href=str(
242-
request.url_for(
243-
f"{self.root_router.name}:{self.product.id}:{CREATE_ORDER}",
244-
),
245-
),
229+
href=self.url_for(request, f"{self.root_router.name}:{self.product.id}:{CREATE_ORDER}"),
246230
rel="create-order",
247231
type=TYPE_JSON,
248232
method="POST",
@@ -254,11 +238,7 @@ def get_product(self, request: Request) -> ProductPydantic:
254238
):
255239
links.append(
256240
Link(
257-
href=str(
258-
request.url_for(
259-
f"{self.root_router.name}:{self.product.id}:{SEARCH_OPPORTUNITIES}",
260-
),
261-
),
241+
href=self.url_for(request, f"{self.root_router.name}:{self.product.id}:{SEARCH_OPPORTUNITIES}"),
262242
rel="opportunities",
263243
type=TYPE_JSON,
264244
),
@@ -420,11 +400,7 @@ async def create_order(self, payload: OrderPayload, request: Request, response:
420400

421401
def order_link(self, request: Request, opp_req: OpportunityPayload) -> Link:
422402
return Link(
423-
href=str(
424-
request.url_for(
425-
f"{self.root_router.name}:{self.product.id}:{CREATE_ORDER}",
426-
),
427-
),
403+
href=self.url_for(request, f"{self.root_router.name}:{self.product.id}:{CREATE_ORDER}"),
428404
rel="create-order",
429405
type=TYPE_JSON,
430406
method="POST",
@@ -456,11 +432,10 @@ async def get_opportunity_collection(
456432
case Success(Some(opportunity_collection)):
457433
opportunity_collection.links.append(
458434
Link(
459-
href=str(
460-
request.url_for(
461-
f"{self.root_router.name}:{self.product.id}:{GET_OPPORTUNITY_COLLECTION}",
462-
opportunity_collection_id=opportunity_collection_id,
463-
),
435+
href=self.url_for(
436+
request,
437+
f"{self.root_router.name}:{self.product.id}:{GET_OPPORTUNITY_COLLECTION}",
438+
opportunity_collection_id=opportunity_collection_id,
464439
),
465440
rel="self",
466441
type=TYPE_JSON,

stapi-fastapi/src/stapi_fastapi/routers/root_router.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,35 +171,39 @@ def __init__(
171171

172172
self.conformances = list(_conformances)
173173

174+
@staticmethod
175+
def url_for(request: Request, name: str, /, **path_params: Any) -> str:
176+
return str(request.url_for(name, **path_params))
177+
174178
def get_root(self, request: Request) -> RootResponse:
175179
links = [
176180
Link(
177-
href=str(request.url_for(f"{self.name}:{ROOT}")),
181+
href=self.url_for(request, f"{self.name}:{ROOT}"),
178182
rel="self",
179183
type=TYPE_JSON,
180184
),
181185
Link(
182-
href=str(request.url_for(self.openapi_endpoint_name)),
186+
href=self.url_for(request, self.openapi_endpoint_name),
183187
rel="service-description",
184188
type=TYPE_JSON,
185189
),
186190
Link(
187-
href=str(request.url_for(self.docs_endpoint_name)),
191+
href=self.url_for(request, self.docs_endpoint_name),
188192
rel="service-docs",
189193
type="text/html",
190194
),
191195
Link(
192-
href=str(request.url_for(f"{self.name}:{CONFORMANCE}")),
196+
href=self.url_for(request, f"{self.name}:{CONFORMANCE}"),
193197
rel="conformance",
194198
type=TYPE_JSON,
195199
),
196200
Link(
197-
href=str(request.url_for(f"{self.name}:{LIST_PRODUCTS}")),
201+
href=self.url_for(request, f"{self.name}:{LIST_PRODUCTS}"),
198202
rel="products",
199203
type=TYPE_JSON,
200204
),
201205
Link(
202-
href=str(request.url_for(f"{self.name}:{LIST_ORDERS}")),
206+
href=self.url_for(request, f"{self.name}:{LIST_ORDERS}"),
203207
rel="orders",
204208
type=TYPE_GEOJSON,
205209
),
@@ -208,7 +212,7 @@ def get_root(self, request: Request) -> RootResponse:
208212
if self.supports_async_opportunity_search:
209213
links.append(
210214
Link(
211-
href=str(request.url_for(f"{self.name}:{LIST_OPPORTUNITY_SEARCH_RECORDS}")),
215+
href=self.url_for(request, f"{self.name}:{LIST_OPPORTUNITY_SEARCH_RECORDS}"),
212216
rel="opportunity-search-records",
213217
type=TYPE_JSON,
214218
),
@@ -236,7 +240,7 @@ def get_products(self, request: Request, next: str | None = None, limit: int = 1
236240
ids = self.product_ids[start:end]
237241
links = [
238242
Link(
239-
href=str(request.url_for(f"{self.name}:{LIST_PRODUCTS}")),
243+
href=self.url_for(request, f"{self.name}:{LIST_PRODUCTS}"),
240244
rel="self",
241245
type=TYPE_JSON,
242246
),

stapi-pydantic/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
88

99
## [Unreleased]
1010

11+
### Added
12+
13+
- ProductRouter and RootRouter now have a method `url_for` that makes the link generation code slightly cleaner and
14+
allows for overridding in child classes, to support proxy rewrite of the links.
15+
1116
## [0.0.4] - 2025-07-17
1217

1318
### Added

0 commit comments

Comments
 (0)