1- # Generic product router factory
21from __future__ import annotations
32
4- from typing import Self
3+ from typing import TYPE_CHECKING , Self
54
65from fastapi import APIRouter , HTTPException , Request , status
7- from fastapi .encoders import jsonable_encoder
8- from fastapi .responses import JSONResponse
96
10- import stapi_fastapi
117from stapi_fastapi .constants import TYPE_GEOJSON , TYPE_JSON
128from stapi_fastapi .exceptions import ConstraintsException
139from stapi_fastapi .models .opportunity import (
1410 OpportunityCollection ,
1511 OpportunityRequest ,
1612)
13+ from stapi_fastapi .models .order import Order
1714from stapi_fastapi .models .product import Product
1815from stapi_fastapi .models .shared import Link
1916from stapi_fastapi .types .json_schema_model import JsonSchemaModel
2017
21- """
22- /products[MainRouter]/opportunities
23- /products[MainRouter]/parameters
24- /products[MainRouter]/order
25- """
18+ if TYPE_CHECKING :
19+ from stapi_fastapi .routers import RootRouter
2620
2721
2822class ProductRouter (APIRouter ):
2923 def __init__ (
30- self : Self ,
24+ self ,
3125 product : Product ,
32- root_router : stapi_fastapi . routers . RootRouter ,
26+ root_router : RootRouter ,
3327 * args ,
3428 ** kwargs ,
35- ):
29+ ) -> None :
3630 super ().__init__ (* args , ** kwargs )
3731 self .product = product
3832 self .root_router = root_router
@@ -50,6 +44,13 @@ def __init__(
5044 endpoint = self .search_opportunities ,
5145 name = f"{ self .root_router .name } :{ self .product .id } :search-opportunities" ,
5246 methods = ["POST" ],
47+ responses = {
48+ 200 : {
49+ "content" : {
50+ "TYPE_GEOJSON" : {},
51+ },
52+ }
53+ },
5354 summary = "Search Opportunities for the product" ,
5455 )
5556
@@ -66,6 +67,13 @@ def __init__(
6667 endpoint = self .create_order ,
6768 name = f"{ self .root_router .name } :{ self .product .id } :create-order" ,
6869 methods = ["POST" ],
70+ responses = {
71+ 201 : {
72+ "content" : {
73+ "TYPE_GEOJSON" : {},
74+ },
75+ }
76+ },
6977 summary = "Create an order for the product" ,
7078 )
7179
@@ -92,37 +100,33 @@ async def search_opportunities(
92100 """
93101 try :
94102 opportunities = await self .product .backend .search_opportunities (
95- search , request
103+ self . product , search , request
96104 )
97105 except ConstraintsException as exc :
98106 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- )
107+ return OpportunityCollection (features = opportunities )
103108
104- async def get_product_constraints (self : Self , request : Request ) -> JsonSchemaModel :
109+ async def get_product_constraints (self : Self ) -> JsonSchemaModel :
105110 """
106111 Return supported constraints of a specific product
107112 """
108113 return self .product .constraints
109114
110115 async def create_order (
111116 self , payload : OpportunityRequest , request : Request
112- ) -> JSONResponse :
117+ ) -> Order :
113118 """
114119 Create a new order.
115120 """
116121 try :
117- order = await self .product .backend .create_order (payload , request )
122+ order = await self .product .backend .create_order (
123+ self .product ,
124+ payload ,
125+ request ,
126+ )
118127 except ConstraintsException as exc :
119128 raise HTTPException (status .HTTP_422_UNPROCESSABLE_ENTITY , detail = exc .detail )
120129
121130 location = self .root_router .generate_order_href (request , order .id )
122- order .links .append (Link (href = location , rel = "self" , type = TYPE_GEOJSON ))
123- return JSONResponse (
124- jsonable_encoder (order , exclude_unset = True ),
125- status .HTTP_201_CREATED ,
126- {"Location" : location },
127- TYPE_GEOJSON ,
128- )
131+ order .links .append (Link (href = str (location ), rel = "self" , type = TYPE_GEOJSON ))
132+ return order
0 commit comments