Skip to content

Commit 3329db2

Browse files
author
Phil Varner
committed
rework Link
1 parent b451b0f commit 3329db2

File tree

4 files changed

+35
-62
lines changed

4 files changed

+35
-62
lines changed

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

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -202,24 +202,17 @@ async def _create_order(
202202

203203
def get_product(self, request: Request) -> ProductPydantic:
204204
links = [
205+
json_link("self", self.url_for(request, f"{self.root_router.name}:{self.product.id}:{GET_PRODUCT}")),
206+
json_link("conformance", self.url_for(request, f"{self.root_router.name}:{self.product.id}:{CONFORMANCE}")),
205207
json_link(
206-
href=self.url_for(request, f"{self.root_router.name}:{self.product.id}:{GET_PRODUCT}"),
207-
rel="self",
208+
"queryables", self.url_for(request, f"{self.root_router.name}:{self.product.id}:{GET_QUERYABLES}")
208209
),
209210
json_link(
210-
href=self.url_for(request, f"{self.root_router.name}:{self.product.id}:{CONFORMANCE}"),
211-
rel="conformance",
212-
),
213-
json_link(
214-
href=self.url_for(request, f"{self.root_router.name}:{self.product.id}:{GET_QUERYABLES}"),
215-
rel="queryables",
216-
),
217-
json_link(
218-
href=self.url_for(request, f"{self.root_router.name}:{self.product.id}:{GET_ORDER_PARAMETERS}"),
219-
rel="order-parameters",
211+
"order-parameters",
212+
self.url_for(request, f"{self.root_router.name}:{self.product.id}:{GET_ORDER_PARAMETERS}"),
220213
),
221214
Link(
222-
href=str(self.url_for(request, f"{self.root_router.name}:{self.product.id}:{CREATE_ORDER}")),
215+
href=self.url_for(request, f"{self.root_router.name}:{self.product.id}:{CREATE_ORDER}"),
223216
rel="create-order",
224217
type=TYPE_JSON,
225218
method="POST",
@@ -231,8 +224,8 @@ def get_product(self, request: Request) -> ProductPydantic:
231224
):
232225
links.append(
233226
json_link(
234-
href=self.url_for(request, f"{self.root_router.name}:{self.product.id}:{SEARCH_OPPORTUNITIES}"),
235-
rel="opportunities",
227+
"opportunities",
228+
self.url_for(request, f"{self.root_router.name}:{self.product.id}:{SEARCH_OPPORTUNITIES}"),
236229
),
237230
)
238231

@@ -392,7 +385,7 @@ async def create_order(self, payload: OrderPayload, request: Request, response:
392385

393386
def order_link(self, request: Request, opp_req: OpportunityPayload) -> Link:
394387
return Link(
395-
href=str(self.url_for(request, f"{self.root_router.name}:{self.product.id}:{CREATE_ORDER}")),
388+
href=self.url_for(request, f"{self.root_router.name}:{self.product.id}:{CREATE_ORDER}"),
396389
rel="create-order",
397390
type=TYPE_JSON,
398391
method="POST",
@@ -403,7 +396,7 @@ def pagination_link(self, request: Request, opp_req: OpportunityPayload, paginat
403396
body = opp_req.body()
404397
body["next"] = pagination_token
405398
return Link(
406-
href=str(request.url),
399+
href=request.url,
407400
rel="next",
408401
type=TYPE_JSON,
409402
method="POST",
@@ -424,12 +417,12 @@ async def get_opportunity_collection(
424417
case Success(Some(opportunity_collection)):
425418
opportunity_collection.links.append(
426419
json_link(
427-
href=self.url_for(
420+
"self",
421+
self.url_for(
428422
request,
429423
f"{self.root_router.name}:{self.product.id}:{GET_OPPORTUNITY_COLLECTION}",
430424
opportunity_collection_id=opportunity_collection_id,
431425
),
432-
rel="self",
433426
),
434427
)
435428
return opportunity_collection # type: ignore

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

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -176,38 +176,32 @@ def __init__(
176176
def get_root(self, request: Request) -> RootResponse:
177177
links = [
178178
json_link(
179-
href=self.url_for(request, f"{self.name}:{ROOT}"),
180-
rel="self",
179+
"self",
180+
self.url_for(request, f"{self.name}:{ROOT}"),
181181
),
182182
json_link(
183-
href=self.url_for(request, self.openapi_endpoint_name),
184-
rel="service-description",
183+
"service-description",
184+
self.url_for(request, self.openapi_endpoint_name),
185185
),
186186
Link(
187-
href=str(self.url_for(request, self.docs_endpoint_name)),
188187
rel="service-docs",
188+
href=self.url_for(request, self.docs_endpoint_name),
189189
type="text/html",
190190
),
191-
json_link(
192-
href=self.url_for(request, f"{self.name}:{CONFORMANCE}"),
193-
rel="conformance",
194-
),
195-
json_link(
196-
href=self.url_for(request, f"{self.name}:{LIST_PRODUCTS}"),
197-
rel="products",
198-
),
191+
json_link("conformance", href=self.url_for(request, f"{self.name}:{CONFORMANCE}")),
192+
json_link("products", self.url_for(request, f"{self.name}:{LIST_PRODUCTS}")),
199193
Link(
200-
href=str(self.url_for(request, f"{self.name}:{LIST_ORDERS}")),
201194
rel="orders",
195+
href=self.url_for(request, f"{self.name}:{LIST_ORDERS}"),
202196
type=TYPE_GEOJSON,
203197
),
204198
]
205199

206200
if self.supports_async_opportunity_search:
207201
links.append(
208202
json_link(
209-
href=self.url_for(request, f"{self.name}:{LIST_OPPORTUNITY_SEARCH_RECORDS}"),
210-
rel="opportunity-search-records",
203+
"opportunity-search-records",
204+
self.url_for(request, f"{self.name}:{LIST_OPPORTUNITY_SEARCH_RECORDS}"),
211205
),
212206
)
213207

@@ -233,8 +227,8 @@ def get_products(self, request: Request, next: str | None = None, limit: int = 1
233227
ids = self.product_ids[start:end]
234228
links = [
235229
json_link(
236-
href=self.url_for(request, f"{self.name}:{LIST_PRODUCTS}"),
237-
rel="self",
230+
"self",
231+
self.url_for(request, f"{self.name}:{LIST_PRODUCTS}"),
238232
),
239233
]
240234
if end > 0 and end < len(self.product_ids):
@@ -359,30 +353,23 @@ def generate_order_statuses_href(self, request: Request, order_id: str) -> URL:
359353
def order_links(self, order: Order[OrderStatus], request: Request) -> list[Link]:
360354
return [
361355
Link(
362-
href=str(self.generate_order_href(request, order.id)),
356+
href=self.generate_order_href(request, order.id),
363357
rel="self",
364358
type=TYPE_GEOJSON,
365359
),
366360
json_link(
367-
href=self.generate_order_statuses_href(request, order.id),
368-
rel="monitor",
361+
"monitor",
362+
self.generate_order_statuses_href(request, order.id),
369363
),
370364
]
371365

372366
def order_statuses_link(self, request: Request, order_id: str) -> Link:
373-
return json_link(
374-
href=self.url_for(
375-
request,
376-
f"{self.name}:{LIST_ORDER_STATUSES}",
377-
order_id=order_id,
378-
),
379-
rel="self",
380-
)
367+
return json_link("self", self.url_for(request, f"{self.name}:{LIST_ORDER_STATUSES}", order_id=order_id))
381368

382369
def pagination_link(self, request: Request, name: str, pagination_token: str, limit: int, **kwargs: Any) -> Link:
383370
return json_link(
384-
href=self.url_for(request, name, **kwargs).include_query_params(next=pagination_token, limit=limit),
385-
rel="next",
371+
"next",
372+
self.url_for(request, name, **kwargs).include_query_params(next=pagination_token, limit=limit),
386373
)
387374

388375
async def get_opportunity_search_records(
@@ -474,10 +461,7 @@ def generate_opportunity_search_record_href(self, request: Request, search_recor
474461
def opportunity_search_record_self_link(
475462
self, opportunity_search_record: OpportunitySearchRecord, request: Request
476463
) -> Link:
477-
return json_link(
478-
href=self.generate_opportunity_search_record_href(request, opportunity_search_record.id),
479-
rel="self",
480-
)
464+
return json_link("self", self.generate_opportunity_search_record_href(request, opportunity_search_record.id))
481465

482466
@property
483467
def _get_order_statuses(self) -> GetOrderStatuses: # type: ignore

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,5 @@
44
from stapi_fastapi.constants import TYPE_JSON
55

66

7-
def json_link(href: URL, rel: str) -> Link:
8-
return Link(
9-
href=str(href),
10-
rel=rel,
11-
type=TYPE_JSON,
12-
)
7+
def json_link(rel: str, href: URL) -> Link:
8+
return Link(href=href, rel=rel, type=TYPE_JSON)

stapi-pydantic/src/stapi_pydantic/shared.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class Link(BaseModel):
2222

2323
# redefining init is a hack to get str type to validate for `href`,
2424
# as str is ultimately coerced into an AnyUrl automatically anyway
25-
def __init__(self, href: AnyUrl | str, **kwargs: Any) -> None:
26-
super().__init__(href=str(href), **kwargs)
25+
def __init__(self, href: Any, **kwargs: Any) -> None:
26+
super().__init__(href=href if isinstance(href, AnyUrl) else str(href), **kwargs)
2727

2828
# overriding the default serialization to filter None field values from
2929
# dumped json

0 commit comments

Comments
 (0)