diff --git a/lib/api.py b/lib/api.py index c234267..6d735fe 100644 --- a/lib/api.py +++ b/lib/api.py @@ -1,12 +1,14 @@ """ This is the main API file for the RocketPy API. """ +import logging from typing import Any, Dict -from fastapi import FastAPI, status +from fastapi import FastAPI, Request, status +from fastapi.exceptions import RequestValidationError from fastapi.middleware.cors import CORSMiddleware from fastapi.openapi.utils import get_openapi -from fastapi.responses import RedirectResponse +from fastapi.responses import RedirectResponse, JSONResponse from lib.views.flight import FlightSummary, FlightCreated, FlightUpdated, FlightDeleted, FlightPickle from lib.views.environment import EnvSummary, EnvCreated, EnvUpdated, EnvDeleted, EnvPickle @@ -60,6 +62,14 @@ def custom_openapi(): return app.openapi_schema app.openapi = custom_openapi +# Logger +@app.exception_handler(RequestValidationError) +async def validation_exception_handler(request: Request, exc: RequestValidationError): + exc_str = f'{exc}'.replace('\n', ' ').replace(' ', ' ') + logging.error(f"{request}: {exc_str}") + content = {'status_code': 10422, 'message': exc_str, 'data': None} + return JSONResponse(content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY) + # Main @app.get("/", include_in_schema=False) async def main_page(): diff --git a/lib/controllers/motor.py b/lib/controllers/motor.py index e8001b4..fab9c3f 100644 --- a/lib/controllers/motor.py +++ b/lib/controllers/motor.py @@ -60,9 +60,6 @@ def __init__(self, motor: Motor, motor_kind, motor_engine): if motor_kind != MotorKind.solid: for tank in motor.tanks: - tank.geometry = TankGeometry(tank.geometry) - tank.gas = Fluid(tank.gas.name, tank.gas.density) - tank.liquid = Fluid(tank.liquid.name, tank.liquid.density) rocketpy_motor.add_tank(tank.tank, tank.position) self.rocketpy_motor = rocketpy_motor diff --git a/lib/models/motor.py b/lib/models/motor.py index 4c35aec..df395c5 100644 --- a/lib/models/motor.py +++ b/lib/models/motor.py @@ -24,7 +24,7 @@ class TankFluids(BaseModel, frozen=True): class MotorTank(BaseModel, frozen=True): #Required parameters - geometry: "Dict[Tuple[int, int], float]" = {(0, 5): 1} + geometry: "List[Tuple[Tuple[float,float],float]]" = [((0, 5), 1), ((5, 10), 2)] tank_kind: TankKinds = TankKinds.mass_flow gas: TankFluids = TankFluids() liquid: TankFluids = TankFluids() @@ -49,7 +49,7 @@ def __init__(self, **kwargs): super().__init__(**kwargs) tank_core = { "name": self.name, - "geometry": self.geometry, + "geometry": { t:f for t,f in self.geometry }, "flux_time": self.flux_time, "gas": self.gas, "liquid": self.liquid,