-
-
Notifications
You must be signed in to change notification settings - Fork 2
[SUGGESTION] ENH: streamline controller dependency injection in routes and tests #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
897afee
81d64f5
a85b9cb
a75f8d2
9a7f0d7
b6f27b4
24a0dfc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| from functools import lru_cache | ||
| from typing import Annotated | ||
|
|
||
| from fastapi import Depends | ||
|
|
||
| from src.controllers.rocket import RocketController | ||
| from src.controllers.motor import MotorController | ||
| from src.controllers.environment import EnvironmentController | ||
| from src.controllers.flight import FlightController | ||
|
|
||
| @lru_cache(maxsize=1) | ||
| def get_rocket_controller() -> RocketController: | ||
| """ | ||
| Provides a singleton RocketController instance. | ||
|
|
||
| The controller is stateless and can be safely reused across requests. | ||
| Using lru_cache ensures thread-safe singleton behavior. | ||
|
|
||
| Returns: | ||
| RocketController: Shared controller instance for rocket operations. | ||
| """ | ||
| return RocketController() | ||
|
|
||
|
|
||
| @lru_cache(maxsize=1) | ||
| def get_motor_controller() -> MotorController: | ||
| """ | ||
| Provides a singleton MotorController instance. | ||
|
|
||
| Returns: | ||
| MotorController: Shared controller instance for motor operations. | ||
| """ | ||
| return MotorController() | ||
|
|
||
|
|
||
| @lru_cache(maxsize=1) | ||
| def get_environment_controller() -> EnvironmentController: | ||
| """ | ||
| Provides a singleton EnvironmentController instance. | ||
|
|
||
| Returns: | ||
| EnvironmentController: Shared controller instance for environment operations. | ||
| """ | ||
| return EnvironmentController() | ||
|
|
||
|
|
||
| @lru_cache(maxsize=1) | ||
| def get_flight_controller() -> FlightController: | ||
| """ | ||
| Provides a singleton FlightController instance. | ||
|
|
||
| Returns: | ||
| FlightController: Shared controller instance for flight operations. | ||
| """ | ||
| return FlightController() | ||
|
|
||
| RocketControllerDep = Annotated[RocketController, Depends(get_rocket_controller)] | ||
| MotorControllerDep = Annotated[MotorController, Depends(get_motor_controller)] | ||
| EnvironmentControllerDep = Annotated[ | ||
| EnvironmentController, Depends(get_environment_controller) | ||
| ] | ||
| FlightControllerDep = Annotated[FlightController, Depends(get_flight_controller)] |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,5 @@ | ||||||||
| """ | ||||||||
| Flight routes | ||||||||
| Flight routes with dependency injection for improved performance. | ||||||||
| """ | ||||||||
|
|
||||||||
| from fastapi import APIRouter, Response | ||||||||
|
|
@@ -13,7 +13,7 @@ | |||||||
| from src.models.environment import EnvironmentModel | ||||||||
| from src.models.flight import FlightModel, FlightWithReferencesRequest | ||||||||
| from src.models.rocket import RocketModel | ||||||||
| from src.controllers.flight import FlightController | ||||||||
| from src.dependencies import FlightControllerDep | ||||||||
|
|
||||||||
| router = APIRouter( | ||||||||
| prefix="/flights", | ||||||||
|
|
@@ -29,21 +29,24 @@ | |||||||
|
|
||||||||
|
|
||||||||
| @router.post("/", status_code=201) | ||||||||
| async def create_flight(flight: FlightModel) -> FlightCreated: | ||||||||
| async def create_flight( | ||||||||
| flight: FlightModel, | ||||||||
| controller: FlightControllerDep, | ||||||||
| ) -> FlightCreated: | ||||||||
| """ | ||||||||
| Creates a new flight | ||||||||
|
|
||||||||
| ## Args | ||||||||
| ``` models.Flight JSON ``` | ||||||||
| """ | ||||||||
| with tracer.start_as_current_span("create_flight"): | ||||||||
| controller = FlightController() | ||||||||
| return await controller.post_flight(flight) | ||||||||
|
|
||||||||
|
|
||||||||
| @router.post("/from-references", status_code=201) | ||||||||
| async def create_flight_from_references( | ||||||||
| payload: FlightWithReferencesRequest, | ||||||||
| controller: FlightControllerDep, | ||||||||
| ) -> FlightCreated: | ||||||||
| """ | ||||||||
| Creates a flight using existing rocket and environment references. | ||||||||
|
|
@@ -56,25 +59,29 @@ async def create_flight_from_references( | |||||||
| ``` | ||||||||
| """ | ||||||||
| with tracer.start_as_current_span("create_flight_from_references"): | ||||||||
| controller = FlightController() | ||||||||
| return await controller.create_flight_from_references(payload) | ||||||||
|
|
||||||||
|
|
||||||||
| @router.get("/{flight_id}") | ||||||||
| async def read_flight(flight_id: str) -> FlightRetrieved: | ||||||||
| async def read_flight( | ||||||||
| flight_id: str, | ||||||||
| controller: FlightControllerDep, | ||||||||
| ) -> FlightRetrieved: | ||||||||
| """ | ||||||||
| Reads an existing flight | ||||||||
|
|
||||||||
| ## Args | ||||||||
| ``` flight_id: str ``` | ||||||||
| """ | ||||||||
| with tracer.start_as_current_span("read_flight"): | ||||||||
| controller = FlightController() | ||||||||
| return await controller.get_flight_by_id(flight_id) | ||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
| @router.put("/{flight_id}", status_code=204) | ||||||||
| async def update_flight(flight_id: str, flight: FlightModel) -> None: | ||||||||
| async def update_flight( | ||||||||
| flight_id: str, | ||||||||
| flight: FlightModel, | ||||||||
| controller: FlightControllerDep, | ||||||||
| ) -> None: | ||||||||
| """ | ||||||||
| Updates an existing flight | ||||||||
|
|
||||||||
|
|
@@ -85,14 +92,14 @@ async def update_flight(flight_id: str, flight: FlightModel) -> None: | |||||||
| ``` | ||||||||
| """ | ||||||||
| with tracer.start_as_current_span("update_flight"): | ||||||||
| controller = FlightController() | ||||||||
| return await controller.put_flight_by_id(flight_id, flight) | ||||||||
|
|
||||||||
|
|
||||||||
| @router.put("/{flight_id}/from-references", status_code=204) | ||||||||
| async def update_flight_from_references( | ||||||||
| flight_id: str, | ||||||||
| payload: FlightWithReferencesRequest, | ||||||||
| controller: FlightControllerDep, | ||||||||
| ) -> None: | ||||||||
| """ | ||||||||
| Updates a flight using existing rocket and environment references. | ||||||||
|
|
@@ -106,22 +113,22 @@ async def update_flight_from_references( | |||||||
| ``` | ||||||||
| """ | ||||||||
| with tracer.start_as_current_span("update_flight_from_references"): | ||||||||
| controller = FlightController() | ||||||||
| return await controller.update_flight_from_references( | ||||||||
| flight_id, payload | ||||||||
| ) | ||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
|
||||||||
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent spacing: this line uses 4 spaces for indentation instead of the blank line pattern used elsewhere. For consistency with the rest of the file, consider using a blank line without trailing spaces here.
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent spacing: this line uses 4 spaces for indentation instead of the blank line pattern used elsewhere. For consistency with the rest of the file, consider using a blank line without trailing spaces here.
| return await controller.get_flight_simulation(flight_id) | |
| return await controller.get_flight_simulation(flight_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent spacing: this line uses 4 spaces for indentation instead of the blank line pattern used elsewhere. For consistency with the rest of the file, consider using a blank line without trailing spaces here.