Skip to content

Commit

Permalink
implements API logger
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBarberini committed Oct 28, 2023
1 parent 5625c4d commit 9bb3466
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
14 changes: 12 additions & 2 deletions lib/api.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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():
Expand Down
3 changes: 0 additions & 3 deletions lib/controllers/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/models/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand Down

0 comments on commit 9bb3466

Please sign in to comment.