|
| 1 | +# Generic product router factory |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from typing import Self |
| 5 | + |
| 6 | +from fastapi import APIRouter, HTTPException, Request, status |
| 7 | +from fastapi.encoders import jsonable_encoder |
| 8 | +from fastapi.responses import JSONResponse |
| 9 | + |
| 10 | +import stapi_fastapi |
| 11 | +from stapi_fastapi.constants import TYPE_GEOJSON, TYPE_JSON |
| 12 | +from stapi_fastapi.exceptions import ConstraintsException |
| 13 | +from stapi_fastapi.models.opportunity import ( |
| 14 | + OpportunityCollection, |
| 15 | + OpportunityRequest, |
| 16 | +) |
| 17 | +from stapi_fastapi.models.product import Product |
| 18 | +from stapi_fastapi.models.shared import Link |
| 19 | +from stapi_fastapi.types.json_schema_model import JsonSchemaModel |
| 20 | + |
| 21 | +""" |
| 22 | +/products[MainRouter]/opportunities |
| 23 | +/products[MainRouter]/parameters |
| 24 | +/products[MainRouter]/order |
| 25 | +""" |
| 26 | + |
| 27 | + |
| 28 | +class ProductRouter(APIRouter): |
| 29 | + def __init__( |
| 30 | + self: Self, |
| 31 | + product: Product, |
| 32 | + root_router: stapi_fastapi.routers.RootRouter, |
| 33 | + *args, |
| 34 | + **kwargs, |
| 35 | + ): |
| 36 | + super().__init__(*args, **kwargs) |
| 37 | + self.product = product |
| 38 | + self.root_router = root_router |
| 39 | + |
| 40 | + self.add_api_route( |
| 41 | + path="", |
| 42 | + endpoint=self.get_product, |
| 43 | + name=f"{self.root_router.name}:{self.product.id}:get-product", |
| 44 | + methods=["GET"], |
| 45 | + summary="Retrieve this product", |
| 46 | + ) |
| 47 | + |
| 48 | + self.add_api_route( |
| 49 | + path="/opportunities", |
| 50 | + endpoint=self.search_opportunities, |
| 51 | + name=f"{self.root_router.name}:{self.product.id}:search-opportunities", |
| 52 | + methods=["POST"], |
| 53 | + summary="Search Opportunities for the product", |
| 54 | + ) |
| 55 | + |
| 56 | + self.add_api_route( |
| 57 | + path="/constraints", |
| 58 | + endpoint=self.get_product_constraints, |
| 59 | + name=f"{self.root_router.name}:{self.product.id}:get-constraints", |
| 60 | + methods=["GET"], |
| 61 | + summary="Get constraints for the product", |
| 62 | + ) |
| 63 | + |
| 64 | + self.add_api_route( |
| 65 | + path="/order", |
| 66 | + endpoint=self.create_order, |
| 67 | + name=f"{self.root_router.name}:{self.product.id}:create-order", |
| 68 | + methods=["POST"], |
| 69 | + summary="Create an order for the product", |
| 70 | + ) |
| 71 | + |
| 72 | + def get_product(self, request: Request) -> Product: |
| 73 | + return self.product.with_links( |
| 74 | + links=[ |
| 75 | + Link( |
| 76 | + href=str( |
| 77 | + request.url_for( |
| 78 | + f"{self.root_router.name}:{self.product.id}:get-product", |
| 79 | + ), |
| 80 | + ), |
| 81 | + rel="self", |
| 82 | + type=TYPE_JSON, |
| 83 | + ), |
| 84 | + ], |
| 85 | + ) |
| 86 | + |
| 87 | + async def search_opportunities( |
| 88 | + self, search: OpportunityRequest, request: Request |
| 89 | + ) -> OpportunityCollection: |
| 90 | + """ |
| 91 | + Explore the opportunities available for a particular set of constraints |
| 92 | + """ |
| 93 | + try: |
| 94 | + opportunities = await self.product.backend.search_opportunities( |
| 95 | + search, request |
| 96 | + ) |
| 97 | + except ConstraintsException as exc: |
| 98 | + raise HTTPException(status.HTTP_422_UNPROCESSABLE_ENTITY, detail=exc.detail) |
| 99 | + return JSONResponse( |
| 100 | + jsonable_encoder(OpportunityCollection(features=opportunities)), |
| 101 | + media_type=TYPE_GEOJSON, |
| 102 | + ) |
| 103 | + |
| 104 | + async def get_product_constraints(self: Self, request: Request) -> JsonSchemaModel: |
| 105 | + """ |
| 106 | + Return supported constraints of a specific product |
| 107 | + """ |
| 108 | + return { |
| 109 | + "product.id": self.product.product.id, |
| 110 | + "constraints": self.product.constraints, |
| 111 | + } |
| 112 | + |
| 113 | + async def create_order( |
| 114 | + self, payload: OpportunityRequest, request: Request |
| 115 | + ) -> JSONResponse: |
| 116 | + """ |
| 117 | + Create a new order. |
| 118 | + """ |
| 119 | + try: |
| 120 | + order = await self.product.backend.create_order(payload, request) |
| 121 | + except ConstraintsException as exc: |
| 122 | + raise HTTPException(status.HTTP_422_UNPROCESSABLE_ENTITY, detail=exc.detail) |
| 123 | + |
| 124 | + location = self.root_router.generate_order_href(request, order.id) |
| 125 | + order.links.append(Link(href=location, rel="self", type=TYPE_GEOJSON)) |
| 126 | + return JSONResponse( |
| 127 | + jsonable_encoder(order, exclude_unset=True), |
| 128 | + status.HTTP_201_CREATED, |
| 129 | + {"Location": location}, |
| 130 | + TYPE_GEOJSON, |
| 131 | + ) |
0 commit comments