Skip to content

Commit 496f7ff

Browse files
refactor json_data, tests okay
1 parent b75a456 commit 496f7ff

File tree

6 files changed

+83
-45
lines changed

6 files changed

+83
-45
lines changed

requirements.txt

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
# This file is autogenerated by pip-compile with Python 3.12
33
# by the following command:
44
#
5-
# pip-compile --output-file=./requirements.txt ./requirements.in
5+
# pip-compile --output-file=./requirements.txt ./requirements-internal.in ./requirements.in
66
#
7-
asgiref>=3
7+
asgiref==3.11.0
88
# via flask
9-
blinker>=1
9+
blinker==1.9.0
1010
# via flask
11-
click>=8
11+
click==8.3.1
1212
# via flask
13-
flask[async]>=3
13+
dataclasses-json==0.6.7
14+
# via opengeodeweb-microservice
15+
fastjsonschema==2.21.1
16+
# via opengeodeweb-microservice
17+
flask[async]==3.1.2
1418
# via
1519
# -r requirements.in
1620
# flask-cors
@@ -22,15 +26,27 @@ geode-common==33.11.3
2226
# geode-viewables
2327
geode-viewables==3.3.2
2428
# via -r requirements.in
25-
itsdangerous>=2
29+
greenlet==3.2.4
30+
# via
31+
# opengeodeweb-microservice
32+
# sqlalchemy
33+
itsdangerous==2.2.0
2634
# via flask
27-
jinja2>=3
35+
jinja2==3.1.6
2836
# via flask
29-
markupsafe>=3
37+
markupsafe==3.0.3
3038
# via
3139
# flask
3240
# jinja2
3341
# werkzeug
42+
marshmallow==3.26.1
43+
# via
44+
# dataclasses-json
45+
# opengeodeweb-microservice
46+
mypy-extensions==1.1.0
47+
# via
48+
# opengeodeweb-microservice
49+
# typing-inspect
3450
opengeode-core==15.30.4
3551
# via
3652
# -r requirements.in
@@ -54,9 +70,25 @@ opengeode-io==7.4.6
5470
# -r requirements.in
5571
# geode-viewables
5672
# opengeode-geosciencesio
73+
opengeodeweb-microservice==1.0.9
74+
# via -r requirements-internal.in
75+
packaging==25.0
76+
# via
77+
# marshmallow
78+
# opengeodeweb-microservice
79+
sqlalchemy==2.0.44
80+
# via opengeodeweb-microservice
81+
typing-extensions==4.15.0
82+
# via
83+
# opengeodeweb-microservice
84+
# sqlalchemy
85+
# typing-inspect
86+
typing-inspect==0.9.0
87+
# via
88+
# dataclasses-json
89+
# opengeodeweb-microservice
5790
werkzeug==3.1.2
5891
# via
5992
# -r requirements.in
6093
# flask
6194
# flask-cors
62-

src/opengeodeweb_back/app.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import argparse
44
import os
5-
import time
65
from typing import Any
76
import flask
87
import flask_cors # type: ignore
@@ -36,8 +35,13 @@
3635

3736

3837
@app.before_request
39-
def before_request() -> None:
38+
def before_request() -> flask.Response|None:
39+
if flask.request.method == "OPTIONS":
40+
response = flask.make_response()
41+
response.headers['Access-Control-Allow-Methods'] = 'GET,POST,PUT,DELETE,OPTIONS'
42+
return response
4043
utils_functions.before_request(flask.current_app)
44+
return None
4145

4246

4347
@app.teardown_request
@@ -111,7 +115,7 @@ def run_server() -> None:
111115
parser.add_argument(
112116
"-d",
113117
"--debug",
114-
default=FLASK_DEBUG,
118+
default=True,
115119
help="Whether to run in debug mode",
116120
action="store_true",
117121
)
@@ -145,7 +149,7 @@ def run_server() -> None:
145149
app.config.update(DATA_FOLDER_PATH=args.data_folder_path)
146150
app.config.update(UPLOAD_FOLDER=args.upload_folder_path)
147151
app.config.update(MINUTES_BEFORE_TIMEOUT=args.timeout)
148-
flask_cors.CORS(app, origins=args.allowed_origins)
152+
flask_cors.CORS(app, origins=args.allowed_origins, methods=["GET", "POST", "PUT"])
149153
print(
150154
f"Host: {args.host}, Port: {args.port}, Debug: {args.debug}, "
151155
f"Data folder path: {args.data_folder_path}, Timeout: {args.timeout}, "

src/opengeodeweb_back/routes/blueprint_routes.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
)
4444
def allowed_files() -> flask.Response:
4545
utils_functions.validate_request(flask.request, schemas_dict["allowed_files"])
46-
params = schemas.AllowedFiles.from_dict(flask.request.get_json())
4746
extensions: set[str] = set()
4847
for geode_object in geode_objects.values():
4948
for extension in geode_object.input_extensions():
@@ -71,8 +70,8 @@ def upload_file() -> flask.Response:
7170
methods=schemas_dict["allowed_objects"]["methods"],
7271
)
7372
def allowed_objects() -> flask.Response:
74-
utils_functions.validate_request(flask.request, schemas_dict["allowed_objects"])
75-
params = schemas.AllowedObjects.from_dict(flask.request.get_json())
73+
json_data = utils_functions.validate_request(flask.request, schemas_dict["allowed_objects"])
74+
params = schemas.AllowedObjects.from_dict(json_data)
7675
file_absolute_path = geode_functions.upload_file_path(params.filename)
7776
file_extension = utils_functions.extension_from_filename(
7877
os.path.basename(file_absolute_path)
@@ -95,8 +94,8 @@ def allowed_objects() -> flask.Response:
9594
methods=schemas_dict["missing_files"]["methods"],
9695
)
9796
def missing_files() -> flask.Response:
98-
utils_functions.validate_request(flask.request, schemas_dict["missing_files"])
99-
params = schemas.MissingFiles.from_dict(flask.request.get_json())
97+
json_data = utils_functions.validate_request(flask.request, schemas_dict["missing_files"])
98+
params = schemas.MissingFiles.from_dict(json_data)
10099
file_path = geode_functions.upload_file_path(params.filename)
101100
geode_object = geode_functions.geode_object_from_string(params.geode_object_type)
102101
additional_files = geode_object.additional_files(
@@ -132,10 +131,10 @@ def missing_files() -> flask.Response:
132131
methods=schemas_dict["geographic_coordinate_systems"]["methods"],
133132
)
134133
def crs_converter_geographic_coordinate_systems() -> flask.Response:
135-
utils_functions.validate_request(
134+
json_data = utils_functions.validate_request(
136135
flask.request, schemas_dict["geographic_coordinate_systems"]
137136
)
138-
params = schemas.GeographicCoordinateSystems.from_dict(flask.request.get_json())
137+
params = schemas.GeographicCoordinateSystems.from_dict(json_data)
139138
geode_object = geode_functions.geode_object_from_string(params.geode_object_type)
140139
infos = (
141140
og_geosciences.GeographicCoordinateSystem3D.geographic_coordinate_systems()
@@ -157,8 +156,8 @@ def crs_converter_geographic_coordinate_systems() -> flask.Response:
157156
methods=schemas_dict["inspect_file"]["methods"],
158157
)
159158
def inspect_file() -> flask.Response:
160-
utils_functions.validate_request(flask.request, schemas_dict["inspect_file"])
161-
params = schemas.InspectFile.from_dict(flask.request.get_json())
159+
json_data = utils_functions.validate_request(flask.request, schemas_dict["inspect_file"])
160+
params = schemas.InspectFile.from_dict(json_data)
162161
file_path = geode_functions.upload_file_path(params.filename)
163162
geode_object = geode_functions.geode_object_from_string(
164163
params.geode_object_type
@@ -201,10 +200,10 @@ def extract_inspector_result(inspection_data: Any) -> object:
201200
methods=schemas_dict["geode_objects_and_output_extensions"]["methods"],
202201
)
203202
def geode_objects_and_output_extensions() -> flask.Response:
204-
utils_functions.validate_request(
203+
json_data = utils_functions.validate_request(
205204
flask.request, schemas_dict["geode_objects_and_output_extensions"]
206205
)
207-
params = schemas.GeodeObjectsAndOutputExtensions.from_dict(flask.request.get_json())
206+
params = schemas.GeodeObjectsAndOutputExtensions.from_dict(json_data)
208207
file_path = geode_functions.upload_file_path(params.filename)
209208
geode_object = geode_functions.geode_object_from_string(
210209
params.geode_object_type
@@ -223,8 +222,8 @@ def geode_objects_and_output_extensions() -> flask.Response:
223222
methods=schemas_dict["save_viewable_file"]["methods"],
224223
)
225224
def save_viewable_file() -> flask.Response:
226-
utils_functions.validate_request(flask.request, schemas_dict["save_viewable_file"])
227-
params = schemas.SaveViewableFile.from_dict(flask.request.get_json())
225+
json_data = utils_functions.validate_request(flask.request, schemas_dict["save_viewable_file"])
226+
params = schemas.SaveViewableFile.from_dict(json_data)
228227
return flask.make_response(
229228
utils_functions.generate_native_viewable_and_light_viewable_from_file(
230229
geode_object_type=geode_object_type(params.geode_object_type),
@@ -239,8 +238,8 @@ def save_viewable_file() -> flask.Response:
239238
methods=schemas_dict["texture_coordinates"]["methods"],
240239
)
241240
def texture_coordinates() -> flask.Response:
242-
utils_functions.validate_request(flask.request, schemas_dict["texture_coordinates"])
243-
params = schemas.TextureCoordinates.from_dict(flask.request.get_json())
241+
json_data = utils_functions.validate_request(flask.request, schemas_dict["texture_coordinates"])
242+
params = schemas.TextureCoordinates.from_dict(json_data)
244243
geode_object = geode_functions.load_geode_object(params.id)
245244
if not isinstance(geode_object, GeodeSurfaceMesh2D | GeodeSurfaceMesh3D):
246245
flask.abort(500, f"{params.id} is not a GeodeSurfaceMesh")
@@ -253,10 +252,10 @@ def texture_coordinates() -> flask.Response:
253252
methods=schemas_dict["vertex_attribute_names"]["methods"],
254253
)
255254
def vertex_attribute_names() -> flask.Response:
256-
utils_functions.validate_request(
255+
json_data = utils_functions.validate_request(
257256
flask.request, schemas_dict["vertex_attribute_names"]
258257
)
259-
params = schemas.VertexAttributeNames.from_dict(flask.request.get_json())
258+
params = schemas.VertexAttributeNames.from_dict(json_data)
260259
geode_object = geode_functions.load_geode_object(params.id)
261260
if not isinstance(geode_object, GeodeMesh):
262261
flask.abort(500, f"{params.id} is not a GeodeMesh")
@@ -274,10 +273,10 @@ def vertex_attribute_names() -> flask.Response:
274273
methods=schemas_dict["polygon_attribute_names"]["methods"],
275274
)
276275
def polygon_attribute_names() -> flask.Response:
277-
utils_functions.validate_request(
276+
json_data = utils_functions.validate_request(
278277
flask.request, schemas_dict["polygon_attribute_names"]
279278
)
280-
params = schemas.PolygonAttributeNames.from_dict(flask.request.get_json())
279+
params = schemas.PolygonAttributeNames.from_dict(json_data)
281280
geode_object = geode_functions.load_geode_object(params.id)
282281
if not isinstance(geode_object, GeodeSurfaceMesh2D | GeodeSurfaceMesh3D):
283282
flask.abort(500, f"{params.id} is not a GeodeSurfaceMesh")
@@ -295,10 +294,10 @@ def polygon_attribute_names() -> flask.Response:
295294
methods=schemas_dict["polyhedron_attribute_names"]["methods"],
296295
)
297296
def polyhedron_attribute_names() -> flask.Response:
298-
utils_functions.validate_request(
297+
json_data = utils_functions.validate_request(
299298
flask.request, schemas_dict["polyhedron_attribute_names"]
300299
)
301-
params = schemas.PolyhedronAttributeNames.from_dict(flask.request.get_json())
300+
params = schemas.PolyhedronAttributeNames.from_dict(json_data)
302301
geode_object = geode_functions.load_geode_object(params.id)
303302
if not isinstance(geode_object, GeodeSolidMesh3D):
304303
flask.abort(500, f"{params.id} is not a GeodeSolidMesh")
@@ -335,8 +334,8 @@ def kill() -> flask.Response:
335334
methods=schemas_dict["export_project"]["methods"],
336335
)
337336
def export_project() -> flask.Response:
338-
utils_functions.validate_request(flask.request, schemas_dict["export_project"])
339-
params = schemas.ExportProject.from_dict(flask.request.get_json())
337+
json_data = utils_functions.validate_request(flask.request, schemas_dict["export_project"])
338+
params = schemas.ExportProject.from_dict(json_data)
340339

341340
project_folder: str = flask.current_app.config["DATA_FOLDER_PATH"]
342341
os.makedirs(project_folder, exist_ok=True)

src/opengeodeweb_back/routes/create/blueprint_create.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
)
2323
def create_point() -> flask.Response:
2424
"""Endpoint to create a single point in 3D space."""
25-
utils_functions.validate_request(flask.request, schemas_dict["create_point"])
26-
params = schemas.CreatePoint.from_dict(flask.request.get_json())
25+
json_data = utils_functions.validate_request(flask.request, schemas_dict["create_point"])
26+
params = schemas.CreatePoint.from_dict(json_data)
2727

2828
# Create the point
2929
pointset = GeodePointSet3D()
@@ -43,8 +43,8 @@ def create_point() -> flask.Response:
4343
)
4444
def create_aoi() -> flask.Response:
4545
"""Endpoint to create an Area of Interest (AOI) as an EdgedCurve3D."""
46-
utils_functions.validate_request(flask.request, schemas_dict["create_aoi"])
47-
params = schemas.CreateAoi.from_dict(flask.request.get_json())
46+
json_data = utils_functions.validate_request(flask.request, schemas_dict["create_aoi"])
47+
params = schemas.CreateAoi.from_dict(json_data)
4848

4949
# Create the edged curve
5050
edged_curve = GeodeEdgedCurve3D()

src/opengeodeweb_back/routes/models/blueprint_models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
methods=schemas_dict["vtm_component_indices"]["methods"],
1717
)
1818
def uuid_to_flat_index() -> flask.Response:
19-
utils_functions.validate_request(
19+
json_data = utils_functions.validate_request(
2020
flask.request, schemas_dict["vtm_component_indices"]
2121
)
22-
params = schemas.VtmComponentIndices.from_dict(flask.request.get_json())
22+
params = schemas.VtmComponentIndices.from_dict(json_data)
2323
vtm_file_path = geode_functions.data_file_path(params.id, "viewable.vtm")
2424
tree = ET.parse(vtm_file_path)
2525
root = tree.find("vtkMultiBlockDataSet")
@@ -39,8 +39,8 @@ def uuid_to_flat_index() -> flask.Response:
3939
methods=schemas_dict["mesh_components"]["methods"],
4040
)
4141
def extract_uuids_endpoint() -> flask.Response:
42-
utils_functions.validate_request(flask.request, schemas_dict["mesh_components"])
43-
params = schemas.MeshComponents.from_dict(flask.request.get_json())
42+
json_data = utils_functions.validate_request(flask.request, schemas_dict["mesh_components"])
43+
params = schemas.MeshComponents.from_dict(json_data)
4444
model = geode_functions.load_geode_object(params.id)
4545
if not isinstance(model, GeodeModel):
4646
flask.abort(500, f"{params.id} is not a GeodeModel")

src/opengeodeweb_back/utils_functions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Standard library imports
2+
import json
23
import os
34
import threading
45
import time
@@ -101,7 +102,7 @@ def versions(list_packages: list[str]) -> list[dict[str, str]]:
101102
return list_with_versions
102103

103104

104-
def validate_request(request: flask.Request, schema: SchemaDict) -> None:
105+
def validate_request(request: flask.Request, schema: SchemaDict) -> dict:
105106
json_data = request.get_json(force=True, silent=True)
106107

107108
if json_data is None:
@@ -113,6 +114,7 @@ def validate_request(request: flask.Request, schema: SchemaDict) -> None:
113114
error_msg = str(e)
114115
print("Validation failed:", error_msg, flush=True)
115116
flask.abort(400, error_msg)
117+
return json_data
116118

117119

118120
def set_interval(
@@ -169,6 +171,7 @@ def handle_exception(exception: HTTPException) -> flask.Response:
169171
"description": exception.description or "An error occurred",
170172
}
171173
)
174+
response.content_type = "application/json"
172175
response.status_code = exception.code or 500
173176
return response
174177

0 commit comments

Comments
 (0)