-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathenvironment.py
More file actions
144 lines (122 loc) · 3.64 KB
/
environment.py
File metadata and controls
144 lines (122 loc) · 3.64 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
"""
Environment routes
"""
from fastapi import APIRouter, Response
from opentelemetry import trace
from src.views.environment import (
EnvironmentSimulation,
EnvironmentCreated,
EnvironmentRetrieved,
)
from src.models.environment import EnvironmentModel
from src.dependencies import EnvironmentControllerDep
router = APIRouter(
prefix="/environments",
tags=["ENVIRONMENT"],
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_environment(
environment: EnvironmentModel,
controller: EnvironmentControllerDep,
) -> EnvironmentCreated:
"""
Creates a new environment
## Args
``` models.Environment JSON ```
"""
with tracer.start_as_current_span("create_environment"):
return await controller.post_environment(environment)
@router.get("/{environment_id}")
async def read_environment(
environment_id: str,
controller: EnvironmentControllerDep,
) -> EnvironmentRetrieved:
"""
Reads an existing environment
## Args
``` environment_id: str ```
"""
with tracer.start_as_current_span("read_environment"):
return await controller.get_environment_by_id(environment_id)
@router.put("/{environment_id}", status_code=204)
async def update_environment(
environment_id: str,
environment: EnvironmentModel,
controller: EnvironmentControllerDep,
) -> None:
"""
Updates an existing environment
## Args
```
environment_id: str
environment: models.Environment JSON
```
"""
with tracer.start_as_current_span("update_environment"):
return await controller.put_environment_by_id(
environment_id, environment
)
@router.delete("/{environment_id}", status_code=204)
async def delete_environment(
environment_id: str,
controller: EnvironmentControllerDep,
) -> None:
"""
Deletes an existing environment
## Args
``` environment_id: str ```
"""
with tracer.start_as_current_span("delete_environment"):
return await controller.delete_environment_by_id(environment_id)
@router.get(
"/{environment_id}/rocketpy",
responses={
200: {
"description": "Binary file download",
"content": {"application/octet-stream": {}},
}
},
status_code=200,
response_class=Response,
)
async def get_rocketpy_environment_binary(
environment_id: str,
controller: EnvironmentControllerDep,
):
"""
Loads rocketpy.environment as a dill binary.
Currently only amd64 architecture is supported.
## Args
``` environment_id: str ```
"""
with tracer.start_as_current_span("get_rocketpy_environment_binary"):
headers = {
'Content-Disposition': f'attachment; filename="rocketpy_environment_{environment_id}.dill"'
}
binary = await controller.get_rocketpy_environment_binary(
environment_id
)
return Response(
content=binary,
headers=headers,
media_type="application/octet-stream",
status_code=200,
)
@router.get("/{environment_id}/simulate")
async def get_environment_simulation(
environment_id: str,
controller: EnvironmentControllerDep,
) -> EnvironmentSimulation:
"""
Simulates an environment
## Args
``` environment_id: Environment ID```
"""
with tracer.start_as_current_span("get_environment_simulation"):
return await controller.get_environment_simulation(environment_id)