11"""
2- Flight routes
2+ Flight routes with dependency injection for improved performance.
33"""
44
55from fastapi import APIRouter , Response
1313from src .models .environment import EnvironmentModel
1414from src .models .flight import FlightModel , FlightWithReferencesRequest
1515from src .models .rocket import RocketModel
16- from src .controllers . flight import FlightController
16+ from src .dependencies import FlightControllerDep
1717
1818router = APIRouter (
1919 prefix = "/flights" ,
2929
3030
3131@router .post ("/" , status_code = 201 )
32- async def create_flight (flight : FlightModel ) -> FlightCreated :
32+ async def create_flight (
33+ flight : FlightModel ,
34+ controller : FlightControllerDep ,
35+ ) -> FlightCreated :
3336 """
3437 Creates a new flight
3538
3639 ## Args
3740 ``` models.Flight JSON ```
3841 """
3942 with tracer .start_as_current_span ("create_flight" ):
40- controller = FlightController ()
4143 return await controller .post_flight (flight )
4244
4345
4446@router .post ("/from-references" , status_code = 201 )
4547async def create_flight_from_references (
4648 payload : FlightWithReferencesRequest ,
49+ controller : FlightControllerDep ,
4750) -> FlightCreated :
4851 """
4952 Creates a flight using existing rocket and environment references.
@@ -56,25 +59,29 @@ async def create_flight_from_references(
5659 ```
5760 """
5861 with tracer .start_as_current_span ("create_flight_from_references" ):
59- controller = FlightController ()
6062 return await controller .create_flight_from_references (payload )
6163
6264
6365@router .get ("/{flight_id}" )
64- async def read_flight (flight_id : str ) -> FlightRetrieved :
66+ async def read_flight (
67+ flight_id : str ,
68+ controller : FlightControllerDep ,
69+ ) -> FlightRetrieved :
6570 """
6671 Reads an existing flight
6772
6873 ## Args
6974 ``` flight_id: str ```
7075 """
7176 with tracer .start_as_current_span ("read_flight" ):
72- controller = FlightController ()
7377 return await controller .get_flight_by_id (flight_id )
7478
75-
7679@router .put ("/{flight_id}" , status_code = 204 )
77- async def update_flight (flight_id : str , flight : FlightModel ) -> None :
80+ async def update_flight (
81+ flight_id : str ,
82+ flight : FlightModel ,
83+ controller : FlightControllerDep ,
84+ ) -> None :
7885 """
7986 Updates an existing flight
8087
@@ -85,14 +92,14 @@ async def update_flight(flight_id: str, flight: FlightModel) -> None:
8592 ```
8693 """
8794 with tracer .start_as_current_span ("update_flight" ):
88- controller = FlightController ()
8995 return await controller .put_flight_by_id (flight_id , flight )
9096
9197
9298@router .put ("/{flight_id}/from-references" , status_code = 204 )
9399async def update_flight_from_references (
94100 flight_id : str ,
95101 payload : FlightWithReferencesRequest ,
102+ controller : FlightControllerDep ,
96103) -> None :
97104 """
98105 Updates a flight using existing rocket and environment references.
@@ -106,22 +113,22 @@ async def update_flight_from_references(
106113 ```
107114 """
108115 with tracer .start_as_current_span ("update_flight_from_references" ):
109- controller = FlightController ()
110116 return await controller .update_flight_from_references (
111117 flight_id , payload
112118 )
113119
114-
115120@router .delete ("/{flight_id}" , status_code = 204 )
116- async def delete_flight (flight_id : str ) -> None :
121+ async def delete_flight (
122+ flight_id : str ,
123+ controller : FlightControllerDep ,
124+ ) -> None :
117125 """
118126 Deletes an existing flight
119127
120128 ## Args
121129 ``` flight_id: str ```
122130 """
123131 with tracer .start_as_current_span ("delete_flight" ):
124- controller = FlightController ()
125132 return await controller .delete_flight_by_id (flight_id )
126133
127134
@@ -136,7 +143,11 @@ async def delete_flight(flight_id: str) -> None:
136143 status_code = 200 ,
137144 response_class = Response ,
138145)
139- async def get_rocketpy_flight_binary (flight_id : str ):
146+
147+ async def get_rocketpy_flight_binary (
148+ flight_id : str ,
149+ controller : FlightControllerDep ,
150+ ):
140151 """
141152 Loads rocketpy.flight as a dill binary.
142153 Currently only amd64 architecture is supported.
@@ -145,7 +156,6 @@ async def get_rocketpy_flight_binary(flight_id: str):
145156 ``` flight_id: str ```
146157 """
147158 with tracer .start_as_current_span ("get_rocketpy_flight_binary" ):
148- controller = FlightController ()
149159 headers = {
150160 'Content-Disposition' : f'attachment; filename="rocketpy_flight_{ flight_id } .dill"'
151161 }
@@ -160,7 +170,9 @@ async def get_rocketpy_flight_binary(flight_id: str):
160170
161171@router .put ("/{flight_id}/environment" , status_code = 204 )
162172async def update_flight_environment (
163- flight_id : str , environment : EnvironmentModel
173+ flight_id : str ,
174+ environment : EnvironmentModel ,
175+ controller : FlightControllerDep ,
164176) -> None :
165177 """
166178 Updates flight environment
@@ -172,14 +184,17 @@ async def update_flight_environment(
172184 ```
173185 """
174186 with tracer .start_as_current_span ("update_flight_environment" ):
175- controller = FlightController ()
176187 return await controller .update_environment_by_flight_id (
177188 flight_id , environment = environment
178189 )
179190
180191
181192@router .put ("/{flight_id}/rocket" , status_code = 204 )
182- async def update_flight_rocket (flight_id : str , rocket : RocketModel ) -> None :
193+ async def update_flight_rocket (
194+ flight_id : str ,
195+ rocket : RocketModel ,
196+ controller : FlightControllerDep ,
197+ ) -> None :
183198 """
184199 Updates flight rocket.
185200
@@ -190,21 +205,21 @@ async def update_flight_rocket(flight_id: str, rocket: RocketModel) -> None:
190205 ```
191206 """
192207 with tracer .start_as_current_span ("update_flight_rocket" ):
193- controller = FlightController ()
194208 return await controller .update_rocket_by_flight_id (
195209 flight_id ,
196210 rocket = rocket ,
197211 )
198212
199-
200213@router .get ("/{flight_id}/simulate" )
201- async def get_flight_simulation (flight_id : str ) -> FlightSimulation :
214+ async def get_flight_simulation (
215+ flight_id : str ,
216+ controller : FlightControllerDep ,
217+ ) -> FlightSimulation :
202218 """
203219 Simulates a flight
204220
205221 ## Args
206222 ``` flight_id: Flight ID ```
207223 """
208224 with tracer .start_as_current_span ("get_flight_simulation" ):
209- controller = FlightController ()
210- return await controller .get_flight_simulation (flight_id )
225+ return await controller .get_flight_simulation (flight_id )
0 commit comments