From 824709744847fddddf4086f2c35c7ea028d13550 Mon Sep 17 00:00:00 2001 From: GabrielBarberini Date: Thu, 28 Dec 2023 02:19:48 -0300 Subject: [PATCH] refactors application routes --- README.md | 14 +- lib/api.py | 569 +------------------------------------- lib/routes/environment.py | 81 ++++++ lib/routes/flight.py | 110 ++++++++ lib/routes/motor.py | 81 ++++++ lib/routes/rocket.py | 82 ++++++ 6 files changed, 372 insertions(+), 565 deletions(-) create mode 100644 lib/routes/environment.py create mode 100644 lib/routes/flight.py create mode 100644 lib/routes/motor.py create mode 100644 lib/routes/rocket.py diff --git a/README.md b/README.md index 1517f5c..8aa0632 100644 --- a/README.md +++ b/README.md @@ -34,19 +34,25 @@ │   │   ├── motor.py │   │   └── rocket.py │   │   -│   ├── models -│   │   ├── aerosurfaces.py +│   ├── routes │   │   ├── environment.py │   │   ├── flight.py │   │   ├── motor.py -│   │   ├── parachute.py │   │   └── rocket.py │   │   │   ├── repositories +│   │   ├── repo.py │   │   ├── environment.py │   │   ├── flight.py │   │   ├── motor.py -│   │   ├── repo.py +│   │   └── rocket.py +│   │   +│   ├── models +│   │   ├── aerosurfaces.py +│   │   ├── environment.py +│   │   ├── flight.py +│   │   ├── motor.py +│   │   ├── parachute.py │   │   └── rocket.py │   │   │   └── views diff --git a/lib/api.py b/lib/api.py index 595a131..cf53202 100644 --- a/lib/api.py +++ b/lib/api.py @@ -2,7 +2,6 @@ This is the main API file for the RocketPy API. """ import logging -from typing import Any, Dict from fastapi import FastAPI, Request, status from fastapi.exceptions import RequestValidationError @@ -10,18 +9,7 @@ from fastapi.openapi.utils import get_openapi 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 -from lib.views.rocket import RocketSummary, RocketCreated, RocketUpdated, RocketDeleted, RocketPickle -from lib.views.motor import MotorSummary, MotorCreated, MotorUpdated, MotorDeleted, MotorPickle -from lib.models.environment import Env -from lib.models.flight import Flight -from lib.models.rocket import Rocket, RocketOptions -from lib.models.motor import Motor, MotorKinds, MotorEngines -from lib.controllers.flight import FlightController -from lib.controllers.environment import EnvController -from lib.controllers.rocket import RocketController -from lib.controllers.motor import MotorController +from lib.routes import flight, environment, motor, rocket app = FastAPI(swagger_ui_parameters={"defaultModelsExpandDepth": 0, "syntaxHighlight.theme": "obsidian"}) app.add_middleware( @@ -31,15 +19,19 @@ allow_methods=["*"], allow_headers=["*"], ) +app.include_router(flight.router) +app.include_router(environment.router) +app.include_router(motor.router) +app.include_router(rocket.router) def custom_openapi(): if app.openapi_schema: return app.openapi_schema openapi_schema = get_openapi( title="RocketPy Infinity-API", - version="1.0.0 ALPHA", + version="1.0.0 BETA", description=( - "

RocketPy Infinity-API is a RESTful API for RocketPy, a rocket flight simulator.

" + "

RocketPy Infinity-API is a RESTful Open API for RocketPy, a rocket flight simulator.

" "
" "" "

Create, manage, and simulate rocket flights, environments, rockets, and motors.

" - "

Currently, the API only supports SolidMotor (calisto as power_off/on_drag and Cesaroni as thrust_source) and TrapezoidalFins. We apologize for the limitation, but we are actively working to expand its capabilities soon.

" + "

Currently, the API only supports TrapezoidalFins. We apologize for the limitation, but we are actively working to expand its capabilities soon.

" "

Please report any bugs at GitHub Issues

" ), routes=app.routes, @@ -70,551 +62,6 @@ async def main_page(): """ return RedirectResponse(url="/redoc") -# Flight routes -@app.post("/flights/", tags=["FLIGHT"]) -async def create_flight(flight: Flight, rocket_option: RocketOptions, motor_kind: MotorKinds) -> "FlightCreated": - """ - Creates a new flight - - ## Args - - ``` Flight object as JSON ``` - - ## Returns - - HTTP 200 { "message": "Flight created successfully.", id: flight_id_hash } - - ## Raises - - HTTP 422 Unprocessable Entity: If API is unable to parse flight data, usually happens when some parameter is invalid, please attend to API docs request specifications. - - HTTP 500 Internal Server Error: If API is either unable to create flight in mongoDB or valid parameter type/structure provided but content is breaking the API. - """ - return await FlightController(flight, rocket_option, motor_kind).create_flight() - -@app.get("/flights/{flight_id}", tags=["FLIGHT"]) -async def read_flight(flight_id: int) -> "Flight": - """ - Reads a flight - - ## Args - - ``` flight_id: Flight ID hash ``` - - ## Returns - - ``` Flight object as JSON ``` - - ## Raises - - HTTP 404 Not Found: If flight_id does not exist at database. - """ - return await FlightController.get_flight(flight_id) - -@app.get("/flights/rocketpy/{flight_id}", tags=["FLIGHT"]) -async def read_rocketpy_flight(flight_id: int) -> "FlightPickle": - """ - Reads a rocketpy flight object - - ## Args - - ``` flight_id: Flight ID hash. ``` - - ## Returns - - ``` RocketPy flight object as jsonpickle string ``` - - ## Raises - - HTTP 404 Not Found: If flight_id does not exist at database. - """ - return await FlightController.get_rocketpy_flight(flight_id) - -@app.put("/flights/{flight_id}/env", tags=["FLIGHT"]) -async def update_flight_env(flight_id: int, env: Env) -> "FlightUpdated": - """ - Updates flight environment - - ## Args - - ``` - flight_id: Flight ID hash - env: env object as JSON - ``` - - ## Returns - - HTTP 200 { "message": "Flight updated successfully.", new_flight_id: new_flight_id_hash } - - ## Raises - - HTTP 404 Not Found: If flight_id does not exist at database. - - HTTP 422 Unprocessable Entity: If API is unable to parse env data, usually happens when some parameter is invalid, please attend to API docs request specifications. - - HTTP 500 Internal Server Error: If API is either unable to update flight in mongoDB or valid parameter type/structure provided but content is breaking the API. - """ - return await FlightController.update_env(flight_id, env) - -@app.put("/flights/{flight_id}/rocket", tags=["FLIGHT"]) -async def update_flight_rocket(flight_id: int, rocket: Rocket, rocket_option: RocketOptions, motor_kind: MotorKinds) -> "FlightUpdated": - """ - Updates flight rocket. - - ## Args - - ``` - flight_id: Flight ID hash. - rocket: Rocket object as JSON - ``` - - ## Returns - - HTTP 200 { "message": "Flight updated successfully.", new_flight_id: new_flight_id_hash } - - ## Raises - - HTTP 404 Not Found: If flight_id does not exist at database. - - HTTP 422 Unprocessable Entity: If API is unable to parse rocket data, usually happens when some parameter is invalid, please attend to API docs request specifications. - - HTTP 500 Internal Server Error: If API is either unable to update flight in mongoDB or valid parameter type/structure provided but content is breaking the API. - """ - return await FlightController.update_rocket(flight_id, rocket, rocket_option, motor_kind) - -@app.put("/flights/{flight_id}", tags=["FLIGHT"]) -async def update_flight(flight_id: int, flight: Flight, rocket_option: RocketOptions, motor_kind: MotorKinds) -> "FlightUpdated": - """ - Updates Flight object - - ## Args - ``` - flight_id: Flight ID hash. - flight: Flight object as JSON - ``` - - ## Returns - - HTTP 200 { "message": "Flight updated successfully.", new_flight_id: new_flight_id_hash } - - ## Raises - - HTTP 404 Not Found: If flight_id does not exist at database. - - HTTP 422 Unprocessable Entity: If API is unable to parse flight data, usually happens when some parameter is invalid, please attend to API docs request specifications. - - HTTP 500 Internal Server Error: If API is either unable to update flight in mongoDB or valid parameter type/structure provided but content is breaking the API. - """ - return await FlightController(flight, rocket_option, motor_kind).update_flight(flight_id) - -@app.delete("/flights/{flight_id}", tags=["FLIGHT"]) -async def delete_flight(flight_id: int) -> "FlightDeleted": - """ - Deletes a flight - - ## Args - - ``` flight_id: Flight ID hash ``` - - ## Returns - - HTTP 200 { "message": "Flight deleted successfully." } - - ## Raises - - HTTP 404 Not Found: If flight_id does not exist at database. - """ - return await FlightController.delete_flight(flight_id) - -@app.get("/flights/{flight_id}/simulate", tags=["FLIGHT"]) -async def simulate_flight(flight_id: int) -> "FlightSummary": - """ - Simulates a flight - - ## Args - - ``` flight_id: Flight ID hash ``` - - ## Returns - - ``` Flight summary as JSON ``` - - ## Raises - - HTTP 404 Not Found: If flight_id does not exist at database. - """ - return await FlightController.simulate(flight_id) - -# Environment routes -@app.post("/environments/", tags=["ENVIRONMENT"]) -async def create_env(env: Env) -> "EnvCreated": - """ - Creates a new environment - - ## Args - - ``` Env object as a JSON ``` - - ## Returns - - HTTP 200 { "message": "Environment created successfully.", id: env_id_hash } - - ## Raises - - HTTP 422 Unprocessable Entity: If API is unable to parse env data, usually happens when some parameter is invalid, please attend to API docs request specifications. - - HTTP 500 Internal Server Error: If API is either unable to create env in mongoDB or valid parameter type/structure provided but content is breaking the API. - """ - return await EnvController(env).create_env() - -@app.get("/environments/{env_id}", tags=["ENVIRONMENT"]) -async def read_env(env_id: int) -> "Env": - """ - Reads an environment - - ## Args - - ``` env_id: Environment ID hash ``` - - ## Returns - - ``` Env object as JSON. ``` - - ## Raises - - HTTP 404 Not Found: If env_id does not exist at database. - """ - return await EnvController.get_env(env_id) - -@app.put("/environments/{env_id}", tags=["ENVIRONMENT"]) -async def update_env(env_id: int, env: Env) -> "EnvUpdated": - """ - Updates an environment - - ## Args - - ``` - env_id: Environment ID hash - env: Env object as JSON - ``` - - ## Returns - - HTTP 200 { "message": "Environment updated successfully.", new_env_id: new_env_id_hash } - - ## Raises - - HTTP 404 Not Found: If env_id does not exist at database. - - HTTP 422 Unprocessable Entity: If API is unable to parse env data, usually happens when some parameter is invalid, please attend to API docs request specifications. - - HTTP 500 Internal Server Error: If API is either unable to update env in mongoDB or valid parameter type/structure provided but content is breaking the API. - """ - return await EnvController(env).update_env(env_id) - -@app.delete("/environments/{env_id}", tags=["ENVIRONMENT"]) -async def delete_env(env_id: int) -> "EnvDeleted": - """ - Deletes an environment - - ## Args - - ``` env_id: Environment ID hash ``` - - ## Returns - - HTTP 200 { "message": "Environment deleted successfully." } - - ## Raises - - HTTP 404 Not Found: If env_id does not exist at database. - """ - return await EnvController.delete_env(env_id) - -@app.get("/environments/rocketpy/{env_id}", tags=["ENVIRONMENT"]) -async def read_rocketpy_env(env_id: int) -> "EnvPickle": - """ - Reads a rocketpy environment - - ## Args - - ``` env_id: Environment ID hash ``` - - ## Returns - - ``` Rocketpy Environment object as JSONPickle string. ``` - - ## Raises - HTTP 404 Not Found: If env_id does not exist at database. - """ - return await EnvController.get_rocketpy_env(env_id) - -@app.get("/environments/{env_id}/simulate", tags=["ENVIRONMENT"]) -async def simulate_env(env_id: int) -> "EnvSummary": - """ - Simulates an environment - - ## Args - - ``` env_id: Env ID hash ``` - - ## Returns - - ``` Env summary object containig simulation numbers and plots as JSON ``` - - ## Raises - - HTTP 404 Not Found: If env_id does not exist at database. - """ - return await EnvController.simulate(env_id) - -# Motor routes -@app.post("/motors/", tags=["MOTOR"]) -async def create_motor(motor: Motor, motor_kind: MotorKinds) -> "MotorCreated": - """ - Creates a new motor - - ## Args - - ``` Motor object as a JSON ``` - - ## Returns - - HTTP 200 { "message": "Motor created successfully.", id: motor_id_hash } - - ## Raises - - HTTP 422 Unprocessable Entity: If API is unable to parse motor data, usually happens when some parameter is invalid, please attend to API docs request specifications. - - HTTP 500 Internal Server Error: If API is either unable to create motor in mongoDB or valid parameter type/structure provided but content is breaking the API. - """ - return await MotorController(motor, motor_kind).create_motor() - -@app.get("/motors/{motor_id}", tags=["MOTOR"]) -async def read_motor(motor_id: int) -> "Motor": - """ - Reads a motor - - ## Args - - ``` motor_id: Motor ID hash ``` - - ## Returns - - Motor object as JSON. - - ## Raises - - HTTP 404 Not Found: If motor_id does not exist at database. - """ - return await MotorController.get_motor(motor_id) - -@app.put("/motors/{motor_id}", tags=["MOTOR"]) -async def update_motor(motor_id: int, motor: Motor, motor_kind: MotorKinds) -> "MotorUpdated": - """ - Updates a motor - - ## Args - - ``` - motor_id: Motor ID hash - motor: Motor object as JSON - ``` - - ## Returns - - HTTP 200 { "message": "Motor updated successfully.", new_motor_id: new_motor_id_hash } - - ## Raises - - HTTP 404 Not Found: If motor_id does not exist at database. - - HTTP 422 Unprocessable Entity: If API is unable to parse motor data, usually happens when some parameter is invalid, please attend to API docs request specifications. - - HTTP 500 Internal Server Error: If API is either unable to update motor in mongoDB or valid parameter type/structure provided but content is breaking the API. - """ - return await MotorController(motor, motor_kind).update_motor(motor_id) - -@app.delete("/motors/{motor_id}", tags=["MOTOR"]) -async def delete_motor(motor_id: int) -> "MotorDeleted": - """ - Deletes a motor - - ## Args - - ``` motor_id: Motor ID hash ``` - - ## Returns - - HTTP 200 { "message": "Motor deleted successfully." } - - ## Raises - - HTTP 404 Not Found: If motor_id does not exist at database. - """ - return await MotorController.delete_motor(motor_id) - -@app.get("/motors/rocketpy/{motor_id}", tags=["MOTOR"]) -async def read_rocketpy_motor(motor_id: int) -> "MotorPickle": - """ - Reads a rocketpy motor - - ## Args - - ``` motor_id: Motor ID hash ``` - - ## Returns - - ``` Rocketpy Motor object as JSONPickle string ``` - - ## Raises - HTTP 404 Not Found: If motor_id does not exist at database. - """ - return await MotorController.get_rocketpy_motor(motor_id) - -@app.get("/motors/{motor_id}/simulate", tags=["MOTOR"]) -async def simulate_motor(motor_id: int) -> "MotorSummary": - """ - Simulates a motor - - ## Args - - ``` motor_id: Motor ID hash ``` - - ## Returns - - ``` Motor summary as JSON ``` - - ## Raises - - HTTP 404 Not Found: If motor_id does not exist at database. - """ - return await MotorController.simulate(motor_id) - -# Rocket routes -@app.post("/rockets/", tags=["ROCKET"]) -async def create_rocket(rocket: Rocket, rocket_option: RocketOptions, motor_kind: MotorKinds) -> "RocketCreated": - """ - Creates a new rocket - - ## Args - - ``` Rocket object as a JSON ``` - - ## Returns - - HTTP 200 { "message": "Rocket created successfully.", id: rocket_id_hash } - - ## Raises - - HTTP 422 Unprocessable Entity: If API is unable to parse rocket data, usually happens when some parameter is invalid, please attend to API docs request specifications. - - HTTP 500 Internal Server Error: If API is either unable to create rocket in mongoDB or valid parameter type/structure provided but content is breaking the API. - """ - return await RocketController(rocket, rocket_option, motor_kind).create_rocket() - -@app.get("/rockets/{rocket_id}", tags=["ROCKET"]) -async def read_rocket(rocket_id: int) -> Rocket: - """ - Reads a rocket - - ## Args - - ``` rocket_id: Rocket ID hash ``` - - ## Returns - - ``` Rocket object as JSON ``` - - ## Raises - - HTTP 404 Not Found: If rocket_id does not exist at database. - """ - return await RocketController.get_rocket(rocket_id) - -@app.put("/rockets/{rocket_id}", tags=["ROCKET"]) -async def update_rocket(rocket_id: int, rocket: Rocket, rocket_option: RocketOptions, motor_kind: MotorKinds) -> "RocketUpdated": - """ - Updates a rocket - - ## Args - - ``` - rocket_id: Rocket ID hash - rocket: Rocket object as JSON - ``` - - ## Returns - - HTTP 200 { "message": "Rocket updated successfully.", new_rocket_id: new_rocket_id_hash } - - ## Raises - - HTTP 404 Not Found: If rocket_id does not exist at database. - - HTTP 422 Unprocessable Entity: If API is unable to parse rocket data, usually happens when some parameter is invalid, please attend to API docs request specifications. - - HTTP 500 Internal Server Error: If API is either unable to update rocket in mongoDB or valid parameter type/structure provided but content is breaking the API. - """ - return await RocketController(rocket, rocket_option, motor_kind).update_rocket(rocket_id) - -@app.delete("/rockets/{rocket_id}", tags=["ROCKET"]) -async def delete_rocket(rocket_id: int) -> "RocketDeleted": - """ - Deletes a rocket - - ## Args - - ``` rocket_id: Rocket ID hash ``` - - ## Returns - - HTTP 200 { "message": "Rocket deleted successfully." } - - ## Raises - - HTTP 404 Not Found: If rocket_id does not exist at database. - """ - return await RocketController.delete_rocket(rocket_id) - -@app.get("/rockets/rocketpy/{rocket_id}", tags=["ROCKET"]) -async def read_rocketpy_rocket(rocket_id: int) -> "RocketPickle": - """ - Reads a rocketpy rocket - - ## Args - - ``` rocket_id: Rocket ID hash ``` - - ## Returns - - ``` Rocketpy Rocket object as JSONPickle string ``` - - ## Raises - - HTTP 404 Not Found: If rocket_id does not exist at database. - """ - return await RocketController.get_rocketpy_rocket(rocket_id) - -@app.get("/rockets/{rocket_id}/simulate", tags=["ROCKET"]) -async def simulate_rocket(rocket_id: int) -> "RocketSummary": - """ - Simulates a rocket - - ## Args - - ``` rocket_id: Rocket ID hash ``` - - ## Returns - - HTTP 200 pydantic rocket summary object containig simulation numbers and plots as JSON. - - ## Raises - - HTTP 404 Not Found: If rocket_id does not exist at database. - """ - return await RocketController.simulate(rocket_id) - # Additional routes @app.get("/health", status_code=status.HTTP_200_OK, include_in_schema=False) async def __perform_healthcheck(): diff --git a/lib/routes/environment.py b/lib/routes/environment.py new file mode 100644 index 0000000..b115a60 --- /dev/null +++ b/lib/routes/environment.py @@ -0,0 +1,81 @@ +""" +Environment routes +""" +from fastapi import APIRouter + +from lib.views.environment import EnvSummary, EnvCreated, EnvUpdated, EnvDeleted, EnvPickle +from lib.models.environment import Env +from lib.controllers.environment import EnvController + +router = APIRouter( + prefix="/environments", + tags=["ENVIRONMENT"], + responses={ + 404: {"description": "Not found"}, + 422: {"description": "Unprocessable Entity"}, + 500: {"description": "Internal Server Error"} + } +) + +@router.post("/") +async def create_env(env: Env) -> "EnvCreated": + """ + Creates a new environment + + ## Args + ``` Env object as a JSON ``` + """ + return await EnvController(env).create_env() + +@router.get("/{env_id}") +async def read_env(env_id: int) -> "Env": + """ + Reads an environment + + ## Args + ``` env_id: Environment ID hash ``` + """ + return await EnvController.get_env(env_id) + +@router.put("/{env_id}") +async def update_env(env_id: int, env: Env) -> "EnvUpdated": + """ + Updates an environment + + ## Args + ``` + env_id: Environment ID hash + env: Env object as JSON + ``` + """ + return await EnvController(env).update_env(env_id) + +@router.delete("/{env_id}") +async def delete_env(env_id: int) -> "EnvDeleted": + """ + Deletes an environment + + ## Args + ``` env_id: Environment ID hash ``` + """ + return await EnvController.delete_env(env_id) + +@router.get("/rocketpy/{env_id}") +async def read_rocketpy_env(env_id: int) -> "EnvPickle": + """ + Reads a rocketpy environment + + ## Args + ``` env_id: Environment ID hash ``` + """ + return await EnvController.get_rocketpy_env(env_id) + +@router.get("/{env_id}/simulate") +async def simulate_env(env_id: int) -> "EnvSummary": + """ + Simulates an environment + + ## Args + ``` env_id: Env ID hash ``` + """ + return await EnvController.simulate(env_id) diff --git a/lib/routes/flight.py b/lib/routes/flight.py new file mode 100644 index 0000000..3d6f5bb --- /dev/null +++ b/lib/routes/flight.py @@ -0,0 +1,110 @@ +""" +Flight routes +""" +from fastapi import APIRouter + +from lib.views.flight import FlightSummary, FlightCreated, FlightUpdated, FlightDeleted, FlightPickle +from lib.models.environment import Env +from lib.models.flight import Flight +from lib.models.rocket import Rocket, RocketOptions +from lib.models.motor import MotorKinds +from lib.controllers.flight import FlightController + +router = APIRouter( + prefix="/flights", + tags=["FLIGHT"], + responses={ + 404: {"description": "Not found"}, + 422: {"description": "Unprocessable Entity"}, + 500: {"description": "Internal Server Error"} + } +) + +@router.post("/") +async def create_flight(flight: Flight, rocket_option: RocketOptions, motor_kind: MotorKinds) -> "FlightCreated": + """ + Creates a new flight + + ## Args + ``` Flight object as JSON ``` + """ + return await FlightController(flight, rocket_option, motor_kind).create_flight() + +@router.get("/{flight_id}") +async def read_flight(flight_id: int) -> "Flight": + """ + Reads a flight + + ## Args + ``` flight_id: Flight ID hash ``` + """ + return await FlightController.get_flight(flight_id) + +@router.get("/rocketpy/{flight_id}") +async def read_rocketpy_flight(flight_id: int) -> "FlightPickle": + """ + Reads a rocketpy flight object + + ## Args + ``` flight_id: Flight ID hash. ``` + """ + return await FlightController.get_rocketpy_flight(flight_id) + +@router.put("/{flight_id}/env") +async def update_flight_env(flight_id: int, env: Env) -> "FlightUpdated": + """ + Updates flight environment + + ## Args + ``` + flight_id: Flight ID hash + env: env object as JSON + ``` + """ + return await FlightController.update_env(flight_id, env) + +@router.put("/{flight_id}/rocket") +async def update_flight_rocket(flight_id: int, rocket: Rocket, rocket_option: RocketOptions, motor_kind: MotorKinds) -> "FlightUpdated": + """ + Updates flight rocket. + + ## Args + ``` + flight_id: Flight ID hash. + rocket: Rocket object as JSON + ``` + """ + return await FlightController.update_rocket(flight_id, rocket, rocket_option, motor_kind) + +@router.put("/{flight_id}") +async def update_flight(flight_id: int, flight: Flight, rocket_option: RocketOptions, motor_kind: MotorKinds) -> "FlightUpdated": + """ + Updates Flight object + + ## Args + ``` + flight_id: Flight ID hash. + flight: Flight object as JSON + ``` + """ + return await FlightController(flight, rocket_option, motor_kind).update_flight(flight_id) + +@router.delete("/{flight_id}") +async def delete_flight(flight_id: int) -> "FlightDeleted": + """ + Deletes a flight + + ## Args + ``` flight_id: Flight ID hash ``` + """ + return await FlightController.delete_flight(flight_id) + +@router.get("/{flight_id}/simulate") +async def simulate_flight(flight_id: int) -> "FlightSummary": + """ + Simulates a flight + + ## Args + ``` flight_id: Flight ID hash ``` + """ + return await FlightController.simulate(flight_id) diff --git a/lib/routes/motor.py b/lib/routes/motor.py new file mode 100644 index 0000000..ec9ad0c --- /dev/null +++ b/lib/routes/motor.py @@ -0,0 +1,81 @@ +""" +Motor routes +""" +from fastapi import APIRouter + +from lib.views.motor import MotorSummary, MotorCreated, MotorUpdated, MotorDeleted, MotorPickle +from lib.models.motor import Motor, MotorKinds, MotorEngines +from lib.controllers.motor import MotorController + +router = APIRouter( + prefix="/motors", + tags=["MOTOR"], + responses={ + 404: {"description": "Not found"}, + 422: {"description": "Unprocessable Entity"}, + 500: {"description": "Internal Server Error"} + } +) + +@router.post("/") +async def create_motor(motor: Motor, motor_kind: MotorKinds) -> "MotorCreated": + """ + Creates a new motor + + ## Args + ``` Motor object as a JSON ``` + """ + return await MotorController(motor, motor_kind).create_motor() + +@router.get("/{motor_id}") +async def read_motor(motor_id: int) -> "Motor": + """ + Reads a motor + + ## Args + ``` motor_id: Motor ID hash ``` + """ + return await MotorController.get_motor(motor_id) + +@router.put("/{motor_id}") +async def update_motor(motor_id: int, motor: Motor, motor_kind: MotorKinds) -> "MotorUpdated": + """ + Updates a motor + + ## Args + ``` + motor_id: Motor ID hash + motor: Motor object as JSON + ``` + """ + return await MotorController(motor, motor_kind).update_motor(motor_id) + +@router.delete("/{motor_id}") +async def delete_motor(motor_id: int) -> "MotorDeleted": + """ + Deletes a motor + + ## Args + ``` motor_id: Motor ID hash ``` + """ + return await MotorController.delete_motor(motor_id) + +@router.get("/rocketpy/{motor_id}") +async def read_rocketpy_motor(motor_id: int) -> "MotorPickle": + """ + Reads a rocketpy motor + + ## Args + ``` motor_id: Motor ID hash ``` + """ + return await MotorController.get_rocketpy_motor(motor_id) + +@router.get("/{motor_id}/simulate") +async def simulate_motor(motor_id: int) -> "MotorSummary": + """ + Simulates a motor + + ## Args + ``` motor_id: Motor ID hash ``` + """ + return await MotorController.simulate(motor_id) diff --git a/lib/routes/rocket.py b/lib/routes/rocket.py new file mode 100644 index 0000000..b40775e --- /dev/null +++ b/lib/routes/rocket.py @@ -0,0 +1,82 @@ +""" +Rocket routes +""" +from fastapi import APIRouter + +from lib.views.rocket import RocketSummary, RocketCreated, RocketUpdated, RocketDeleted, RocketPickle +from lib.models.rocket import Rocket, RocketOptions +from lib.models.motor import MotorKinds +from lib.controllers.rocket import RocketController + +router = APIRouter( + prefix="/rockets", + tags=["ROCKET"], + responses={ + 404: {"description": "Not found"}, + 422: {"description": "Unprocessable Entity"}, + 500: {"description": "Internal Server Error"} + } +) + +@router.post("/") +async def create_rocket(rocket: Rocket, rocket_option: RocketOptions, motor_kind: MotorKinds) -> "RocketCreated": + """ + Creates a new rocket + + ## Args + ``` Rocket object as a JSON ``` + """ + return await RocketController(rocket, rocket_option, motor_kind).create_rocket() + +@router.get("/{rocket_id}") +async def read_rocket(rocket_id: int) -> Rocket: + """ + Reads a rocket + + ## Args + ``` rocket_id: Rocket ID hash ``` + """ + return await RocketController.get_rocket(rocket_id) + +@router.put("/{rocket_id}") +async def update_rocket(rocket_id: int, rocket: Rocket, rocket_option: RocketOptions, motor_kind: MotorKinds) -> "RocketUpdated": + """ + Updates a rocket + + ## Args + ``` + rocket_id: Rocket ID hash + rocket: Rocket object as JSON + ``` + """ + return await RocketController(rocket, rocket_option, motor_kind).update_rocket(rocket_id) + +@router.delete("/{rocket_id}") +async def delete_rocket(rocket_id: int) -> "RocketDeleted": + """ + Deletes a rocket + + ## Args + ``` rocket_id: Rocket ID hash ``` + """ + return await RocketController.delete_rocket(rocket_id) + +@router.get("/rocketpy/{rocket_id}") +async def read_rocketpy_rocket(rocket_id: int) -> "RocketPickle": + """ + Reads a rocketpy rocket + + ## Args + ``` rocket_id: Rocket ID hash ``` + """ + return await RocketController.get_rocketpy_rocket(rocket_id) + +@router.get("/{rocket_id}/simulate") +async def simulate_rocket(rocket_id: int) -> "RocketSummary": + """ + Simulates a rocket + + ## Args + ``` rocket_id: Rocket ID hash ``` + """ + return await RocketController.simulate(rocket_id)