Skip to content

Commit a0306ad

Browse files
committed
better
1 parent 644db20 commit a0306ad

File tree

4 files changed

+89
-164
lines changed

4 files changed

+89
-164
lines changed

src/opengeodeweb_back/routes/blueprint_routes.py

Lines changed: 50 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,25 @@
2121
name=blueprint_models.routes.name,
2222
)
2323

24-
schemas_folder = os.path.join(os.path.dirname(__file__), "schemas")
25-
26-
with open(
27-
os.path.join(schemas_folder, "allowed_files.json"),
28-
"r",
29-
) as file:
30-
allowed_files_json: utils_functions.SchemaDict = json.load(file)
24+
schemas_dict = utils_functions.get_schemas_dict(
25+
os.path.join(os.path.dirname(__file__), "schemas")
26+
)
3127

3228

3329
@routes.route(
34-
allowed_files_json["route"],
35-
methods=allowed_files_json["methods"],
30+
schemas_dict["allowed_files"]["route"],
31+
methods=schemas_dict["allowed_files"]["methods"],
3632
)
3733
def allowed_files() -> flask.Response:
38-
utils_functions.validate_request(flask.request, allowed_files_json)
34+
utils_functions.validate_request(flask.request, schemas_dict["allowed_files"])
3935
params = schemas.AllowedFiles(**flask.request.get_json())
4036
extensions = geode_functions.list_input_extensions(params.supported_feature)
4137
return flask.make_response({"extensions": extensions}, 200)
4238

4339

44-
with open(
45-
os.path.join(schemas_folder, "upload_file.json"),
46-
"r",
47-
) as file:
48-
upload_file_json: utils_functions.SchemaDict = json.load(file)
49-
50-
5140
@routes.route(
52-
upload_file_json["route"],
53-
methods=upload_file_json["methods"],
41+
schemas_dict["upload_file"]["route"],
42+
methods=schemas_dict["upload_file"]["methods"],
5443
)
5544
def upload_file() -> flask.Response:
5645
if flask.request.method == "OPTIONS":
@@ -65,22 +54,15 @@ def upload_file() -> flask.Response:
6554
return flask.make_response({"message": "File uploaded"}, 201)
6655

6756

68-
with open(
69-
os.path.join(schemas_folder, "allowed_objects.json"),
70-
"r",
71-
) as file:
72-
allowed_objects_json: utils_functions.SchemaDict = json.load(file)
73-
74-
7557
@routes.route(
76-
allowed_objects_json["route"],
77-
methods=allowed_objects_json["methods"],
58+
schemas_dict["allowed_objects"]["route"],
59+
methods=schemas_dict["allowed_objects"]["methods"],
7860
)
7961
def allowed_objects() -> flask.Response:
8062
if flask.request.method == "OPTIONS":
8163
return flask.make_response({}, 200)
8264

83-
utils_functions.validate_request(flask.request, allowed_objects_json)
65+
utils_functions.validate_request(flask.request, schemas_dict["allowed_objects"])
8466
params = schemas.AllowedObjects(**flask.request.get_json())
8567
file_absolute_path = geode_functions.upload_file_path(params.filename)
8668
allowed_objects = geode_functions.list_geode_objects(
@@ -89,19 +71,12 @@ def allowed_objects() -> flask.Response:
8971
return flask.make_response({"allowed_objects": allowed_objects}, 200)
9072

9173

92-
with open(
93-
os.path.join(schemas_folder, "missing_files.json"),
94-
"r",
95-
) as file:
96-
missing_files_json: utils_functions.SchemaDict = json.load(file)
97-
98-
9974
@routes.route(
100-
missing_files_json["route"],
101-
methods=missing_files_json["methods"],
75+
schemas_dict["missing_files"]["route"],
76+
methods=schemas_dict["missing_files"]["methods"],
10277
)
10378
def missing_files() -> flask.Response:
104-
utils_functions.validate_request(flask.request, missing_files_json)
79+
utils_functions.validate_request(flask.request, schemas_dict["missing_files"])
10580
params = schemas.MissingFiles(**flask.request.get_json())
10681
file_path = geode_functions.upload_file_path(params.filename)
10782

@@ -136,19 +111,14 @@ def missing_files() -> flask.Response:
136111
)
137112

138113

139-
with open(
140-
os.path.join(schemas_folder, "geographic_coordinate_systems.json"),
141-
"r",
142-
) as file:
143-
geographic_coordinate_systems_json: utils_functions.SchemaDict = json.load(file)
144-
145-
146114
@routes.route(
147-
geographic_coordinate_systems_json["route"],
148-
methods=geographic_coordinate_systems_json["methods"],
115+
schemas_dict["geographic_coordinate_systems"]["route"],
116+
methods=schemas_dict["geographic_coordinate_systems"]["methods"],
149117
)
150118
def crs_converter_geographic_coordinate_systems() -> flask.Response:
151-
utils_functions.validate_request(flask.request, geographic_coordinate_systems_json)
119+
utils_functions.validate_request(
120+
flask.request, schemas_dict["geographic_coordinate_systems"]
121+
)
152122
params = schemas.GeographicCoordinateSystems(**flask.request.get_json())
153123
infos = geode_functions.geographic_coordinate_systems(params.input_geode_object)
154124
crs_list = []
@@ -162,19 +132,12 @@ def crs_converter_geographic_coordinate_systems() -> flask.Response:
162132
return flask.make_response({"crs_list": crs_list}, 200)
163133

164134

165-
with open(
166-
os.path.join(schemas_folder, "inspect_file.json"),
167-
"r",
168-
) as file:
169-
inspect_file_json: utils_functions.SchemaDict = json.load(file)
170-
171-
172135
@routes.route(
173-
inspect_file_json["route"],
174-
methods=inspect_file_json["methods"],
136+
schemas_dict["inspect_file"]["route"],
137+
methods=schemas_dict["inspect_file"]["methods"],
175138
)
176139
def inspect_file() -> flask.Response:
177-
utils_functions.validate_request(flask.request, inspect_file_json)
140+
utils_functions.validate_request(flask.request, schemas_dict["inspect_file"])
178141
params = schemas.InspectFile(**flask.request.get_json())
179142
file_path = geode_functions.upload_file_path(params.filename)
180143
data = geode_functions.load(params.input_geode_object, file_path)
@@ -183,22 +146,13 @@ def inspect_file() -> flask.Response:
183146
return flask.make_response({"inspection_result": inspection_result}, 200)
184147

185148

186-
with open(
187-
os.path.join(schemas_folder, "geode_objects_and_output_extensions.json"),
188-
"r",
189-
) as file:
190-
geode_objects_and_output_extensions_json: utils_functions.SchemaDict = json.load(
191-
file
192-
)
193-
194-
195149
@routes.route(
196-
geode_objects_and_output_extensions_json["route"],
197-
methods=geode_objects_and_output_extensions_json["methods"],
150+
schemas_dict["geode_objects_and_output_extensions"]["route"],
151+
methods=schemas_dict["geode_objects_and_output_extensions"]["methods"],
198152
)
199153
def geode_objects_and_output_extensions() -> flask.Response:
200154
utils_functions.validate_request(
201-
flask.request, geode_objects_and_output_extensions_json
155+
flask.request, schemas_dict["geode_objects_and_output_extensions"]
202156
)
203157
params = schemas.GeodeObjectsAndOutputExtensions(**flask.request.get_json())
204158
file_path = geode_functions.upload_file_path(params.filename)
@@ -215,19 +169,12 @@ def geode_objects_and_output_extensions() -> flask.Response:
215169
)
216170

217171

218-
with open(
219-
os.path.join(schemas_folder, "save_viewable_file.json"),
220-
"r",
221-
) as file:
222-
save_viewable_file_json: utils_functions.SchemaDict = json.load(file)
223-
224-
225172
@routes.route(
226-
save_viewable_file_json["route"],
227-
methods=save_viewable_file_json["methods"],
173+
schemas_dict["save_viewable_file"]["route"],
174+
methods=schemas_dict["save_viewable_file"]["methods"],
228175
)
229176
def save_viewable_file() -> flask.Response:
230-
utils_functions.validate_request(flask.request, save_viewable_file_json)
177+
utils_functions.validate_request(flask.request, schemas_dict["save_viewable_file"])
231178
params = schemas.SaveViewableFile(**flask.request.get_json())
232179
return flask.make_response(
233180
utils_functions.generate_native_viewable_and_light_viewable_from_file(
@@ -238,35 +185,26 @@ def save_viewable_file() -> flask.Response:
238185
)
239186

240187

241-
with open(os.path.join(schemas_folder, "texture_coordinates.json"), "r") as file:
242-
texture_coordinates_json: utils_functions.SchemaDict = json.load(file)
243-
244-
245188
@routes.route(
246-
texture_coordinates_json["route"],
247-
methods=texture_coordinates_json["methods"],
189+
schemas_dict["texture_coordinates"]["route"],
190+
methods=schemas_dict["texture_coordinates"]["methods"],
248191
)
249192
def texture_coordinates() -> flask.Response:
250-
utils_functions.validate_request(flask.request, texture_coordinates_json)
193+
utils_functions.validate_request(flask.request, schemas_dict["texture_coordinates"])
251194
params = schemas.TextureCoordinates(**flask.request.get_json())
252195
data = geode_functions.load_data(params.id)
253196
texture_coordinates = data.texture_manager().texture_names()
254197
return flask.make_response({"texture_coordinates": texture_coordinates}, 200)
255198

256199

257-
with open(
258-
os.path.join(schemas_folder, "vertex_attribute_names.json"),
259-
"r",
260-
) as file:
261-
vertex_attribute_names_json: utils_functions.SchemaDict = json.load(file)
262-
263-
264200
@routes.route(
265-
vertex_attribute_names_json["route"],
266-
methods=vertex_attribute_names_json["methods"],
201+
schemas_dict["vertex_attribute_names"]["route"],
202+
methods=schemas_dict["vertex_attribute_names"]["methods"],
267203
)
268204
def vertex_attribute_names() -> flask.Response:
269-
utils_functions.validate_request(flask.request, vertex_attribute_names_json)
205+
utils_functions.validate_request(
206+
flask.request, schemas_dict["vertex_attribute_names"]
207+
)
270208
params = schemas.VertexAttributeNames(**flask.request.get_json())
271209
data = geode_functions.load_data(params.id)
272210
vertex_attribute_names = data.vertex_attribute_manager().attribute_names()
@@ -278,19 +216,14 @@ def vertex_attribute_names() -> flask.Response:
278216
)
279217

280218

281-
with open(
282-
os.path.join(schemas_folder, "polygon_attribute_names.json"),
283-
"r",
284-
) as file:
285-
polygon_attribute_names_json: utils_functions.SchemaDict = json.load(file)
286-
287-
288219
@routes.route(
289-
polygon_attribute_names_json["route"],
290-
methods=polygon_attribute_names_json["methods"],
220+
schemas_dict["polygon_attribute_names"]["route"],
221+
methods=schemas_dict["polygon_attribute_names"]["methods"],
291222
)
292223
def polygon_attribute_names() -> flask.Response:
293-
utils_functions.validate_request(flask.request, polygon_attribute_names_json)
224+
utils_functions.validate_request(
225+
flask.request, schemas_dict["polygon_attribute_names"]
226+
)
294227
params = schemas.PolygonAttributeNames(**flask.request.get_json())
295228
data = geode_functions.load_data(params.id)
296229
polygon_attribute_names = data.polygon_attribute_manager().attribute_names()
@@ -302,19 +235,14 @@ def polygon_attribute_names() -> flask.Response:
302235
)
303236

304237

305-
with open(
306-
os.path.join(schemas_folder, "polyhedron_attribute_names.json"),
307-
"r",
308-
) as file:
309-
polyhedron_attribute_names_json: utils_functions.SchemaDict = json.load(file)
310-
311-
312238
@routes.route(
313-
polyhedron_attribute_names_json["route"],
314-
methods=polyhedron_attribute_names_json["methods"],
239+
schemas_dict["polyhedron_attribute_names"]["route"],
240+
methods=schemas_dict["polyhedron_attribute_names"]["methods"],
315241
)
316242
def polyhedron_attribute_names() -> flask.Response:
317-
utils_functions.validate_request(flask.request, polyhedron_attribute_names_json)
243+
utils_functions.validate_request(
244+
flask.request, schemas_dict["polyhedron_attribute_names"]
245+
)
318246
params = schemas.PolyhedronAttributeNames(**flask.request.get_json())
319247
data = geode_functions.load_data(params.id)
320248
polyhedron_attribute_names = data.polyhedron_attribute_manager().attribute_names()
@@ -326,31 +254,17 @@ def polyhedron_attribute_names() -> flask.Response:
326254
)
327255

328256

329-
with open(
330-
os.path.join(schemas_folder, "ping.json"),
331-
"r",
332-
) as file:
333-
ping_json: utils_functions.SchemaDict = json.load(file)
334-
335-
336257
@routes.route(
337-
ping_json["route"],
338-
methods=ping_json["methods"],
258+
schemas_dict["ping"]["route"],
259+
methods=schemas_dict["ping"]["methods"],
339260
)
340261
def ping() -> flask.Response:
341-
utils_functions.validate_request(flask.request, ping_json)
262+
utils_functions.validate_request(flask.request, schemas_dict["ping"])
342263
flask.current_app.config.update(LAST_PING_TIME=time.time())
343264
return flask.make_response({"message": "Flask server is running"}, 200)
344265

345266

346-
with open(
347-
os.path.join(schemas_folder, "kill.json"),
348-
"r",
349-
) as file:
350-
kill_json: utils_functions.SchemaDict = json.load(file)
351-
352-
353-
@routes.route(kill_json["route"], methods=kill_json["methods"])
267+
@routes.route(schemas_dict["kill"]["route"], methods=schemas_dict["kill"]["methods"])
354268
def kill() -> flask.Response:
355269
print("Manual server kill, shutting down...", flush=True)
356270
os._exit(0)

src/opengeodeweb_back/routes/create/blueprint_create.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
from . import schemas
1313

1414
routes = flask.Blueprint("create", __name__, url_prefix="/create")
15-
schemas_folder = os.path.join(os.path.dirname(__file__), "schemas")
15+
schemas_dict = utils_functions.get_schemas_dict(
16+
os.path.join(os.path.dirname(__file__), "schemas")
17+
)
1618

1719

18-
# Load schemas
19-
with open(os.path.join(schemas_folder, "create_point.json"), "r") as file:
20-
create_point_json: utils_functions.SchemaDict = json.load(file)
21-
22-
23-
@routes.route(create_point_json["route"], methods=create_point_json["methods"])
20+
@routes.route(
21+
schemas_dict["create_point"]["route"],
22+
methods=schemas_dict["create_point"]["methods"],
23+
)
2424
def create_point() -> flask.Response:
2525
"""Endpoint to create a single point in 3D space."""
2626
print(f"create_point : {flask.request=}", flush=True)
27-
utils_functions.validate_request(flask.request, create_point_json)
27+
utils_functions.validate_request(flask.request, schemas_dict["create_point"])
2828
params = schemas.CreatePoint(**flask.request.get_json())
2929

3030
# Create the point
@@ -42,16 +42,13 @@ def create_point() -> flask.Response:
4242
return flask.make_response(result, 200)
4343

4444

45-
# Load schema for AOI creation
46-
with open(os.path.join(schemas_folder, "create_aoi.json"), "r") as file:
47-
create_aoi_json: utils_functions.SchemaDict = json.load(file)
48-
49-
50-
@routes.route(create_aoi_json["route"], methods=create_aoi_json["methods"])
45+
@routes.route(
46+
schemas_dict["create_aoi"]["route"], methods=schemas_dict["create_aoi"]["methods"]
47+
)
5148
def create_aoi() -> flask.Response:
5249
"""Endpoint to create an Area of Interest (AOI) as an EdgedCurve3D."""
5350
print(f"create_aoi : {flask.request=}", flush=True)
54-
utils_functions.validate_request(flask.request, create_aoi_json)
51+
utils_functions.validate_request(flask.request, schemas_dict["create_aoi"])
5552
params = schemas.CreateAoi(**flask.request.get_json())
5653

5754
# Create the edged curve

0 commit comments

Comments
 (0)