-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathflight.py
More file actions
225 lines (193 loc) · 5.55 KB
/
flight.py
File metadata and controls
225 lines (193 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""
Flight routes with dependency injection for improved performance.
"""
from fastapi import APIRouter, Response
from opentelemetry import trace
from src.views.flight import (
FlightSimulation,
FlightCreated,
FlightRetrieved,
)
from src.models.environment import EnvironmentModel
from src.models.flight import FlightModel, FlightWithReferencesRequest
from src.models.rocket import RocketModel
from src.dependencies import FlightControllerDep
router = APIRouter(
prefix="/flights",
tags=["FLIGHT"],
responses={
404: {"description": "Not found"},
422: {"description": "Unprocessable Entity"},
500: {"description": "Internal Server Error"},
},
)
tracer = trace.get_tracer(__name__)
@router.post("/", status_code=201)
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"):
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.
## Args
```
environment_id: str
rocket_id: str
flight: Flight-only fields JSON
```
"""
with tracer.start_as_current_span("create_flight_from_references"):
return await controller.create_flight_from_references(payload)
@router.get("/{flight_id}")
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"):
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,
controller: FlightControllerDep,
) -> None:
"""
Updates an existing flight
## Args
```
flight_id: str
flight: models.flight JSON
```
"""
with tracer.start_as_current_span("update_flight"):
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.
## Args
```
flight_id: str
environment_id: str
rocket_id: str
flight: Flight-only fields JSON
```
"""
with tracer.start_as_current_span("update_flight_from_references"):
return await controller.update_flight_from_references(
flight_id, payload
)
@router.delete("/{flight_id}", status_code=204)
async def delete_flight(
flight_id: str,
controller: FlightControllerDep,
) -> None:
"""
Deletes an existing flight
## Args
``` flight_id: str ```
"""
with tracer.start_as_current_span("delete_flight"):
return await controller.delete_flight_by_id(flight_id)
@router.get(
"/{flight_id}/rocketpy",
responses={
200: {
"description": "Binary file download",
"content": {"application/octet-stream": {}},
}
},
status_code=200,
response_class=Response,
)
async def get_rocketpy_flight_binary(
flight_id: str,
controller: FlightControllerDep,
):
"""
Loads rocketpy.flight as a dill binary.
Currently only amd64 architecture is supported.
## Args
``` flight_id: str ```
"""
with tracer.start_as_current_span("get_rocketpy_flight_binary"):
headers = {
'Content-Disposition': f'attachment; filename="rocketpy_flight_{flight_id}.dill"'
}
binary = await controller.get_rocketpy_flight_binary(flight_id)
return Response(
content=binary,
headers=headers,
media_type="application/octet-stream",
status_code=200,
)
@router.put("/{flight_id}/environment", status_code=204)
async def update_flight_environment(
flight_id: str,
environment: EnvironmentModel,
controller: FlightControllerDep,
) -> None:
"""
Updates flight environment
## Args
```
flight_id: Flight ID
environment: env object as JSON
```
"""
with tracer.start_as_current_span("update_flight_environment"):
return await controller.update_environment_by_flight_id(
flight_id, environment=environment
)
@router.put("/{flight_id}/rocket", status_code=204)
async def update_flight_rocket(
flight_id: str,
rocket: RocketModel,
controller: FlightControllerDep,
) -> None:
"""
Updates flight rocket.
## Args
```
flight_id: Flight ID
rocket: RocketModel object as JSON
```
"""
with tracer.start_as_current_span("update_flight_rocket"):
return await controller.update_rocket_by_flight_id(
flight_id,
rocket=rocket,
)
@router.get("/{flight_id}/simulate")
async def get_flight_simulation(
flight_id: str,
controller: FlightControllerDep,
) -> FlightSimulation:
"""
Simulates a flight
## Args
``` flight_id: Flight ID ```
"""
with tracer.start_as_current_span("get_flight_simulation"):
return await controller.get_flight_simulation(flight_id)