-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrocket.py
More file actions
183 lines (155 loc) · 4.36 KB
/
rocket.py
File metadata and controls
183 lines (155 loc) · 4.36 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
"""
Rocket routes
"""
from fastapi import APIRouter, Response
from opentelemetry import trace
from src.views.rocket import (
RocketSimulation,
RocketCreated,
RocketRetrieved,
)
from src.models.rocket import (
RocketModel,
RocketWithMotorReferenceRequest,
)
from src.dependencies import RocketControllerDep
router = APIRouter(
prefix="/rockets",
tags=["ROCKET"],
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_rocket(
rocket: RocketModel,
controller: RocketControllerDep,
) -> RocketCreated:
"""
Creates a new rocket
## Args
``` models.Rocket JSON ```
"""
with tracer.start_as_current_span("create_rocket"):
return await controller.post_rocket(rocket)
@router.post("/from-motor-reference", status_code=201)
async def create_rocket_from_motor_reference(
payload: RocketWithMotorReferenceRequest,
controller: RocketControllerDep,
) -> RocketCreated:
"""
Creates a rocket using an existing motor reference.
## Args
```
motor_id: str
rocket: Rocket-only fields JSON
```
"""
with tracer.start_as_current_span("create_rocket_from_motor_reference"):
return await controller.create_rocket_from_motor_reference(payload)
@router.get("/{rocket_id}")
async def read_rocket(
rocket_id: str,
controller: RocketControllerDep,
) -> RocketRetrieved:
"""
Reads an existing rocket
## Args
``` rocket_id: str ```
"""
with tracer.start_as_current_span("read_rocket"):
return await controller.get_rocket_by_id(rocket_id)
@router.put("/{rocket_id}", status_code=204)
async def update_rocket(
rocket_id: str,
rocket: RocketModel,
controller: RocketControllerDep,
) -> None:
"""
Updates an existing rocket
## Args
```
rocket_id: str
rocket: models.rocket JSON
```
"""
with tracer.start_as_current_span("update_rocket"):
return await controller.put_rocket_by_id(rocket_id, rocket)
@router.put("/{rocket_id}/from-motor-reference", status_code=204)
async def update_rocket_from_motor_reference(
rocket_id: str,
payload: RocketWithMotorReferenceRequest,
controller: RocketControllerDep,
) -> None:
"""
Updates a rocket using an existing motor reference.
## Args
```
rocket_id: str
motor_id: str
rocket: Rocket-only fields JSON
```
"""
with tracer.start_as_current_span("update_rocket_from_motor_reference"):
return await controller.update_rocket_from_motor_reference(
rocket_id, payload
)
@router.delete("/{rocket_id}", status_code=204)
async def delete_rocket(
rocket_id: str,
controller: RocketControllerDep,
) -> None:
"""
Deletes an existing rocket
## Args
``` rocket_id: str ```
"""
with tracer.start_as_current_span("delete_rocket"):
return await controller.delete_rocket_by_id(rocket_id)
@router.get(
"/{rocket_id}/rocketpy",
responses={
200: {
"description": "Binary file download",
"content": {"application/octet-stream": {}},
}
},
status_code=200,
response_class=Response,
)
async def get_rocketpy_rocket_binary(
rocket_id: str,
controller: RocketControllerDep,
):
"""
Loads rocketpy.rocket as a dill binary.
Currently only amd64 architecture is supported.
## Args
``` rocket_id: str ```
"""
with tracer.start_as_current_span("get_rocketpy_rocket_binary"):
headers = {
'Content-Disposition': f'attachment; filename="rocketpy_rocket_{rocket_id}.dill"'
}
binary = await controller.get_rocketpy_rocket_binary(rocket_id)
return Response(
content=binary,
headers=headers,
media_type="application/octet-stream",
status_code=200,
)
@router.get("/{rocket_id}/simulate")
async def simulate_rocket(
rocket_id: str,
controller: RocketControllerDep,
) -> RocketSimulation:
"""
Simulates a rocket
## Args
``` rocket_id: Rocket ID ```
"""
with tracer.start_as_current_span("get_rocket_simulation"):
return await controller.get_rocket_simulation(rocket_id)