-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
196 lines (163 loc) · 5.58 KB
/
app.py
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
"""Simple transformation app for Hooke's Law."""
import logging
from fastapi import FastAPI, HTTPException
from fastapi.openapi.utils import get_openapi
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from marketplace_standard_app_api.models.transformation import (
TransformationCreateResponse,
TransformationId,
TransformationListResponse,
TransformationState,
TransformationStateResponse,
TransformationUpdateModel,
TransformationUpdateResponse,
)
from transformation import HookesLaw, TransformationInput, TransformationOutput
app = FastAPI()
app.mount("/frontend", StaticFiles(directory="frontend"), name="frontend")
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="Hooke's Law app",
description="MarketPlace simple transformation app",
version="1.0.0",
contact={
"name": "Pablo de Andres",
"url": "https://materials-marketplace.eu/",
"email": "[email protected]",
},
license_info={
"name": "MIT",
"url": "https://opensource.org/licenses/MIT",
},
servers=[{"url": "https://hookes-law.materials-data.space"}],
routes=app.routes,
)
openapi_schema["info"]["x-api-version"] = "0.4.0"
openapi_schema["info"]["x-products"] = [{"name": "Monthly"}]
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi
transformations = dict()
@app.get(
"/",
summary="Frontend",
operation_id="frontend",
responses={
200: {"content": {"text/html": {}}},
},
)
async def get_index():
return FileResponse("frontend/index.html")
@app.get("/script.js")
async def get_script():
return FileResponse("frontend/script.js")
@app.get("/styles.css")
async def get_styles():
return FileResponse("frontend/styles.css")
@app.get(
"/heartbeat", operation_id="heartbeat", summary="Check if app is alive"
)
async def heartbeat():
return "Simple transformation app up and running"
@app.post(
"/transformations",
operation_id="newTransformation",
summary="Create a new transformation",
response_model=TransformationCreateResponse,
)
async def new_transformation(
payload: TransformationInput,
) -> TransformationCreateResponse:
new_transformation = HookesLaw(payload)
transformations[new_transformation.id] = new_transformation
return {"id": new_transformation.id}
@app.patch(
"/transformations/{transformation_id}",
summary="Update the state of the transformation.",
response_model=TransformationUpdateResponse,
operation_id="updateTransformation",
responses={
404: {"description": "Not Found."},
400: {"description": "Requested state not supported"},
},
)
async def update_transformation_state(
transformation_id: TransformationId, payload: TransformationUpdateModel
) -> TransformationUpdateResponse:
state = payload.state
try:
if state == "RUNNING":
transformations[str(transformation_id)].run()
else:
msg = f"{state} is not a supported state."
raise HTTPException(status_code=400, detail=msg)
return {"id": TransformationId(transformation_id), "state": state}
except KeyError:
raise HTTPException(
status_code=404,
detail=f"Transformation not found: {transformation_id}",
)
@app.get(
"/transformations/{transformation_id}/state",
summary="Get the state of the transformation.",
response_model=TransformationStateResponse,
operation_id="getTransformationState",
responses={404: {"description": "Unknown transformation"}},
)
async def get_transformation_state(
transformation_id: TransformationId,
) -> TransformationStateResponse:
try:
state = transformations[str(transformation_id)].state
return {"id": transformation_id, "state": state}
except KeyError:
raise HTTPException(status_code=404, detail="Simulation not found")
@app.get(
"/transformations",
summary="Get all transformations.",
response_model=TransformationListResponse,
operation_id="getTransformationList",
)
async def get_transformations():
items = [
{
"id": transformation.id,
"parameters": transformation.parameters,
"state": transformation.state,
}
for transformation in transformations.values()
]
logging.info(f"transformations: {items}")
return {"items": items}
@app.delete(
"/transformations/{transformation_id}",
summary="Delete a transformation",
operation_id="deleteTransformation",
)
async def delete_transformation(transformation_id: TransformationId):
try:
del transformations[str(transformation_id)]
return {
"status": f"Simulation '{transformation_id}' deleted successfully!"
}
except KeyError as ke:
raise HTTPException(status_code=404, detail=ke)
@app.get(
"/datasets/{transformation_id}",
summary="Get a transformation's result",
operation_id="getDataset",
response_model=TransformationOutput,
)
async def get_results(transformation_id: TransformationId):
try:
transformation = transformations[str(transformation_id)]
if transformation.state != TransformationState.COMPLETED:
raise HTTPException(
"Transformation {transformation_id} has not run."
)
return transformation.result
except KeyError as ke:
raise HTTPException(status_code=404, detail=ke)