11import logging
2+ import traceback
23from typing import Self
34
45from fastapi import APIRouter , HTTPException , Request , status
56from fastapi .datastructures import URL
6- from fastapi .responses import Response
77from returns .maybe import Maybe , Some
88from returns .result import Failure , Success
99
1515 Order ,
1616 OrderCollection ,
1717 OrderStatuses ,
18- OrderStatusPayload ,
1918)
2019from stapi_fastapi .models .product import Product , ProductsCollection
2120from stapi_fastapi .models .root import RootResponse
2221from stapi_fastapi .models .shared import Link
2322from stapi_fastapi .responses import GeoJSONResponse
2423from stapi_fastapi .routers .product_router import ProductRouter
2524
25+ logger = logging .getLogger (__name__ )
26+
2627
2728class RootRouter (APIRouter ):
2829 def __init__ (
@@ -98,14 +99,6 @@ def __init__(
9899 tags = ["Orders" ],
99100 )
100101
101- self .add_api_route (
102- "/orders/{order_id}/statuses" ,
103- self .set_order_status ,
104- methods = ["POST" ],
105- name = f"{ self .name } :set-order-status" ,
106- tags = ["Orders" ],
107- )
108-
109102 def get_root (self , request : Request ) -> RootResponse :
110103 return RootResponse (
111104 id = "STAPI API" ,
@@ -205,8 +198,10 @@ async def get_order(self: Self, order_id: str, request: Request) -> Order:
205198 case Success (Maybe .empty ):
206199 raise NotFoundException ("Order not found" )
207200 case Failure (e ):
208- logging .exception (
209- f"An error occurred while retrieving order '{ order_id } '" , e
201+ logger .error (
202+ "An error occurred while retrieving order '%s': %s" ,
203+ order_id ,
204+ traceback .format_exception (e ),
210205 )
211206 raise HTTPException (
212207 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
@@ -236,8 +231,9 @@ async def get_order_statuses(
236231 ],
237232 )
238233 case Failure (e ):
239- logging .exception (
240- "An error occurred while retrieving order statuses" , e
234+ logger .error (
235+ "An error occurred while retrieving order statuses: %s" ,
236+ traceback .format_exception (e ),
241237 )
242238 raise HTTPException (
243239 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
@@ -246,24 +242,9 @@ async def get_order_statuses(
246242 case _:
247243 raise AssertionError ("Expected code to be unreachable" )
248244
249- async def set_order_status (
250- self , order_id : str , payload : OrderStatusPayload , request : Request
251- ) -> Response :
252- match await self .backend .set_order_status (order_id , payload , request ):
253- case Success (_):
254- return Response (status_code = status .HTTP_202_ACCEPTED )
255- case Failure (e ):
256- logging .exception ("An error occurred while setting order status" , e )
257- raise HTTPException (
258- status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
259- detail = "Error setting Order Status" ,
260- )
261- case x :
262- raise AssertionError (f"Expected code to be unreachable { x } " )
263-
264- def add_product (self : Self , product : Product ) -> None :
245+ def add_product (self : Self , product : Product , * args , ** kwargs ) -> None :
265246 # Give the include a prefix from the product router
266- product_router = ProductRouter (product , self )
247+ product_router = ProductRouter (product , self , * args , ** kwargs )
267248 self .include_router (product_router , prefix = f"/products/{ product .id } " )
268249 self .product_routers [product .id ] = product_router
269250
0 commit comments