Skip to content

Commit 00a6f45

Browse files
committed
fix(Schemas): using typed schemas
1 parent b737e91 commit 00a6f45

File tree

8 files changed

+126
-116
lines changed

8 files changed

+126
-116
lines changed

src/opengeodeweb_back/geode_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def filter_geode_objects(key: str = None):
175175
return geode_objects_filtered_list
176176

177177

178-
def list_input_extensions(key: str = None):
178+
def list_input_extensions(key: str | None = None):
179179
extensions_list = []
180180
geode_objects_filtered_list = filter_geode_objects(key)
181181
for geode_object in geode_objects_filtered_list:
@@ -192,7 +192,7 @@ def has_creator(geode_object: str, extension: str):
192192

193193
def list_geode_objects(
194194
file_absolute_path: str,
195-
key: str = None,
195+
key: str | None = None,
196196
):
197197
return_dict = {}
198198
file_extension = utils_functions.extension_from_filename(

src/opengeodeweb_back/routes/blueprint_routes.py

Lines changed: 75 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55

66
# Third party imports
77
import flask
8-
import opengeode
98
import werkzeug
109

1110
# Local application imports
12-
from .. import geode_functions, utils_functions
13-
from opengeodeweb_microservice.database.data import Data
14-
from opengeodeweb_microservice.database.connection import get_session
11+
from opengeodeweb_back import geode_functions, utils_functions
1512
from .models import blueprint_models
13+
from . import schemas
1614

1715
routes = flask.Blueprint("routes", __name__, url_prefix="/opengeodeweb_back")
1816

@@ -23,39 +21,38 @@
2321
name=blueprint_models.routes.name,
2422
)
2523

26-
schemas = os.path.join(os.path.dirname(__file__), "schemas")
24+
schemas_folder = os.path.join(os.path.dirname(__file__), "schemas")
2725

2826
with open(
29-
os.path.join(schemas, "allowed_files.json"),
27+
os.path.join(schemas_folder, "allowed_files.json"),
3028
"r",
3129
) as file:
32-
allowed_files_json = json.load(file)
30+
allowed_files_json: utils_functions.SchemaDict = json.load(file)
3331

3432

3533
@routes.route(
3634
allowed_files_json["route"],
3735
methods=allowed_files_json["methods"],
3836
)
39-
def allowed_files():
37+
def allowed_files() -> flask.Response:
4038
utils_functions.validate_request(flask.request, allowed_files_json)
41-
extensions = geode_functions.list_input_extensions(
42-
flask.request.get_json()["supported_feature"]
43-
)
39+
params = schemas.AllowedFiles(**flask.request.get_json())
40+
extensions = geode_functions.list_input_extensions(params.supported_feature)
4441
return flask.make_response({"extensions": extensions}, 200)
4542

4643

4744
with open(
48-
os.path.join(schemas, "upload_file.json"),
45+
os.path.join(schemas_folder, "upload_file.json"),
4946
"r",
5047
) as file:
51-
upload_file_json = json.load(file)
48+
upload_file_json: utils_functions.SchemaDict = json.load(file)
5249

5350

5451
@routes.route(
5552
upload_file_json["route"],
5653
methods=upload_file_json["methods"],
5754
)
58-
def upload_file():
55+
def upload_file() -> flask.Response:
5956
if flask.request.method == "OPTIONS":
6057
return flask.make_response({}, 200)
6158

@@ -69,47 +66,47 @@ def upload_file():
6966

7067

7168
with open(
72-
os.path.join(schemas, "allowed_objects.json"),
69+
os.path.join(schemas_folder, "allowed_objects.json"),
7370
"r",
7471
) as file:
75-
allowed_objects_json = json.load(file)
72+
allowed_objects_json: utils_functions.SchemaDict = json.load(file)
7673

7774

7875
@routes.route(
7976
allowed_objects_json["route"],
8077
methods=allowed_objects_json["methods"],
8178
)
82-
def allowed_objects():
79+
def allowed_objects() -> flask.Response:
8380
if flask.request.method == "OPTIONS":
8481
return flask.make_response({}, 200)
8582

8683
utils_functions.validate_request(flask.request, allowed_objects_json)
87-
file_absolute_path = geode_functions.upload_file_path(
88-
flask.request.get_json()["filename"]
89-
)
84+
params = schemas.AllowedObjects(**flask.request.get_json())
85+
file_absolute_path = geode_functions.upload_file_path(params.filename)
9086
allowed_objects = geode_functions.list_geode_objects(
91-
file_absolute_path, flask.request.get_json()["supported_feature"]
87+
file_absolute_path, params.supported_feature
9288
)
9389
return flask.make_response({"allowed_objects": allowed_objects}, 200)
9490

9591

9692
with open(
97-
os.path.join(schemas, "missing_files.json"),
93+
os.path.join(schemas_folder, "missing_files.json"),
9894
"r",
9995
) as file:
100-
missing_files_json = json.load(file)
96+
missing_files_json: utils_functions.SchemaDict = json.load(file)
10197

10298

10399
@routes.route(
104100
missing_files_json["route"],
105101
methods=missing_files_json["methods"],
106102
)
107-
def missing_files():
103+
def missing_files() -> flask.Response:
108104
utils_functions.validate_request(flask.request, missing_files_json)
109-
file_path = geode_functions.upload_file_path(flask.request.get_json()["filename"])
105+
params = schemas.MissingFiles(**flask.request.get_json())
106+
file_path = geode_functions.upload_file_path(params.filename)
110107

111108
additional_files = geode_functions.additional_files(
112-
flask.request.get_json()["input_geode_object"],
109+
params.input_geode_object,
113110
file_path,
114111
)
115112

@@ -140,21 +137,20 @@ def missing_files():
140137

141138

142139
with open(
143-
os.path.join(schemas, "geographic_coordinate_systems.json"),
140+
os.path.join(schemas_folder, "geographic_coordinate_systems.json"),
144141
"r",
145142
) as file:
146-
geographic_coordinate_systems_json = json.load(file)
143+
geographic_coordinate_systems_json: utils_functions.SchemaDict = json.load(file)
147144

148145

149146
@routes.route(
150147
geographic_coordinate_systems_json["route"],
151148
methods=geographic_coordinate_systems_json["methods"],
152149
)
153-
def crs_converter_geographic_coordinate_systems():
150+
def crs_converter_geographic_coordinate_systems() -> flask.Response:
154151
utils_functions.validate_request(flask.request, geographic_coordinate_systems_json)
155-
infos = geode_functions.geographic_coordinate_systems(
156-
flask.request.get_json()["input_geode_object"]
157-
)
152+
params = schemas.GeographicCoordinateSystems(**flask.request.get_json())
153+
infos = geode_functions.geographic_coordinate_systems(params.input_geode_object)
158154
crs_list = []
159155
for info in infos:
160156
crs = {}
@@ -167,54 +163,51 @@ def crs_converter_geographic_coordinate_systems():
167163

168164

169165
with open(
170-
os.path.join(schemas, "inspect_file.json"),
166+
os.path.join(schemas_folder, "inspect_file.json"),
171167
"r",
172168
) as file:
173-
inspect_file_json = json.load(file)
169+
inspect_file_json: utils_functions.SchemaDict = json.load(file)
174170

175171

176172
@routes.route(
177173
inspect_file_json["route"],
178174
methods=inspect_file_json["methods"],
179175
)
180-
def inspect_file():
176+
def inspect_file() -> flask.Response:
181177
utils_functions.validate_request(flask.request, inspect_file_json)
182-
183-
file_path = geode_functions.upload_file_path(flask.request.get_json()["filename"])
184-
data = geode_functions.load(
185-
flask.request.get_json()["input_geode_object"], file_path
186-
)
187-
class_inspector = geode_functions.inspect(
188-
flask.request.get_json()["input_geode_object"], data
189-
)
178+
params = schemas.InspectFile(**flask.request.get_json())
179+
file_path = geode_functions.upload_file_path(params.filename)
180+
data = geode_functions.load(params.input_geode_object, file_path)
181+
class_inspector = geode_functions.inspect(params.input_geode_object, data)
190182
inspection_result = geode_functions.get_inspector_children(class_inspector)
191183
return flask.make_response({"inspection_result": inspection_result}, 200)
192184

193185

194186
with open(
195-
os.path.join(schemas, "geode_objects_and_output_extensions.json"),
187+
os.path.join(schemas_folder, "geode_objects_and_output_extensions.json"),
196188
"r",
197189
) as file:
198-
geode_objects_and_output_extensions_json = json.load(file)
190+
geode_objects_and_output_extensions_json: utils_functions.SchemaDict = json.load(
191+
file
192+
)
199193

200194

201195
@routes.route(
202196
geode_objects_and_output_extensions_json["route"],
203197
methods=geode_objects_and_output_extensions_json["methods"],
204198
)
205-
def geode_objects_and_output_extensions():
199+
def geode_objects_and_output_extensions() -> flask.Response:
206200
utils_functions.validate_request(
207201
flask.request, geode_objects_and_output_extensions_json
208202
)
209-
file_path = geode_functions.upload_file_path(flask.request.get_json()["filename"])
203+
params = schemas.GeodeObjectsAndOutputExtensions(**flask.request.get_json())
204+
file_path = geode_functions.upload_file_path(params.filename)
210205
data = geode_functions.load(
211-
flask.request.get_json()["input_geode_object"],
206+
params.input_geode_object,
212207
file_path,
213208
)
214209
geode_objects_and_output_extensions = (
215-
geode_functions.geode_objects_output_extensions(
216-
flask.request.get_json()["input_geode_object"], data
217-
)
210+
geode_functions.geode_objects_output_extensions(params.input_geode_object, data)
218211
)
219212
return flask.make_response(
220213
{"geode_objects_and_output_extensions": geode_objects_and_output_extensions},
@@ -223,56 +216,59 @@ def geode_objects_and_output_extensions():
223216

224217

225218
with open(
226-
os.path.join(schemas, "save_viewable_file.json"),
219+
os.path.join(schemas_folder, "save_viewable_file.json"),
227220
"r",
228221
) as file:
229-
save_viewable_file_json = json.load(file)
222+
save_viewable_file_json: utils_functions.SchemaDict = json.load(file)
230223

231224

232225
@routes.route(
233226
save_viewable_file_json["route"],
234227
methods=save_viewable_file_json["methods"],
235228
)
236-
def save_viewable_file():
229+
def save_viewable_file() -> flask.Response:
237230
utils_functions.validate_request(flask.request, save_viewable_file_json)
231+
params = schemas.SaveViewableFile(**flask.request.get_json())
238232
return flask.make_response(
239233
utils_functions.generate_native_viewable_and_light_viewable_from_file(
240-
geode_object=flask.request.get_json()["input_geode_object"],
241-
input_filename=flask.request.get_json()["filename"],
234+
geode_object=params.input_geode_object,
235+
input_filename=params.filename,
242236
),
243237
200,
244238
)
245239

246240

247-
with open(os.path.join(schemas, "texture_coordinates.json"), "r") as file:
248-
texture_coordinates_json = json.load(file)
241+
with open(os.path.join(schemas_folder, "texture_coordinates.json"), "r") as file:
242+
texture_coordinates_json: utils_functions.SchemaDict = json.load(file)
249243

250244

251245
@routes.route(
252246
texture_coordinates_json["route"],
253247
methods=texture_coordinates_json["methods"],
254248
)
255-
def texture_coordinates():
249+
def texture_coordinates() -> flask.Response:
256250
utils_functions.validate_request(flask.request, texture_coordinates_json)
257-
data = geode_functions.load_data(flask.request.get_json().get("id"))
251+
params = schemas.TextureCoordinates(**flask.request.get_json())
252+
data = geode_functions.load_data(params.id)
258253
texture_coordinates = data.texture_manager().texture_names()
259254
return flask.make_response({"texture_coordinates": texture_coordinates}, 200)
260255

261256

262257
with open(
263-
os.path.join(schemas, "vertex_attribute_names.json"),
258+
os.path.join(schemas_folder, "vertex_attribute_names.json"),
264259
"r",
265260
) as file:
266-
vertex_attribute_names_json = json.load(file)
261+
vertex_attribute_names_json: utils_functions.SchemaDict = json.load(file)
267262

268263

269264
@routes.route(
270265
vertex_attribute_names_json["route"],
271266
methods=vertex_attribute_names_json["methods"],
272267
)
273-
def vertex_attribute_names():
268+
def vertex_attribute_names() -> flask.Response:
274269
utils_functions.validate_request(flask.request, vertex_attribute_names_json)
275-
data = geode_functions.load_data(flask.request.get_json().get("id"))
270+
params = schemas.VertexAttributeNames(**flask.request.get_json())
271+
data = geode_functions.load_data(params.id)
276272
vertex_attribute_names = data.vertex_attribute_manager().attribute_names()
277273
return flask.make_response(
278274
{
@@ -283,19 +279,20 @@ def vertex_attribute_names():
283279

284280

285281
with open(
286-
os.path.join(schemas, "polygon_attribute_names.json"),
282+
os.path.join(schemas_folder, "polygon_attribute_names.json"),
287283
"r",
288284
) as file:
289-
polygon_attribute_names_json = json.load(file)
285+
polygon_attribute_names_json: utils_functions.SchemaDict = json.load(file)
290286

291287

292288
@routes.route(
293289
polygon_attribute_names_json["route"],
294290
methods=polygon_attribute_names_json["methods"],
295291
)
296-
def polygon_attribute_names():
292+
def polygon_attribute_names() -> flask.Response:
297293
utils_functions.validate_request(flask.request, polygon_attribute_names_json)
298-
data = geode_functions.load_data(flask.request.get_json().get("id"))
294+
params = schemas.PolygonAttributeNames(**flask.request.get_json())
295+
data = geode_functions.load_data(params.id)
299296
polygon_attribute_names = data.polygon_attribute_manager().attribute_names()
300297
return flask.make_response(
301298
{
@@ -306,19 +303,20 @@ def polygon_attribute_names():
306303

307304

308305
with open(
309-
os.path.join(schemas, "polyhedron_attribute_names.json"),
306+
os.path.join(schemas_folder, "polyhedron_attribute_names.json"),
310307
"r",
311308
) as file:
312-
polyhedron_attribute_names_json = json.load(file)
309+
polyhedron_attribute_names_json: utils_functions.SchemaDict = json.load(file)
313310

314311

315312
@routes.route(
316313
polyhedron_attribute_names_json["route"],
317314
methods=polyhedron_attribute_names_json["methods"],
318315
)
319-
def polyhedron_attribute_names():
316+
def polyhedron_attribute_names() -> flask.Response:
320317
utils_functions.validate_request(flask.request, polyhedron_attribute_names_json)
321-
data = geode_functions.load_data(flask.request.get_json().get("id"))
318+
params = schemas.PolyhedronAttributeNames(**flask.request.get_json())
319+
data = geode_functions.load_data(params.id)
322320
polyhedron_attribute_names = data.polyhedron_attribute_manager().attribute_names()
323321
return flask.make_response(
324322
{
@@ -329,27 +327,27 @@ def polyhedron_attribute_names():
329327

330328

331329
with open(
332-
os.path.join(schemas, "ping.json"),
330+
os.path.join(schemas_folder, "ping.json"),
333331
"r",
334332
) as file:
335-
ping_json = json.load(file)
333+
ping_json: utils_functions.SchemaDict = json.load(file)
336334

337335

338336
@routes.route(
339337
ping_json["route"],
340338
methods=ping_json["methods"],
341339
)
342-
def ping():
340+
def ping() -> flask.Response:
343341
utils_functions.validate_request(flask.request, ping_json)
344342
flask.current_app.config.update(LAST_PING_TIME=time.time())
345343
return flask.make_response({"message": "Flask server is running"}, 200)
346344

347345

348346
with open(
349-
os.path.join(schemas, "kill.json"),
347+
os.path.join(schemas_folder, "kill.json"),
350348
"r",
351349
) as file:
352-
kill_json = json.load(file)
350+
kill_json: utils_functions.SchemaDict = json.load(file)
353351

354352

355353
@routes.route(kill_json["route"], methods=kill_json["methods"])

0 commit comments

Comments
 (0)