Skip to content

Commit 0a6b95f

Browse files
committed
change typage
1 parent 9a1cade commit 0a6b95f

File tree

1 file changed

+26
-34
lines changed

1 file changed

+26
-34
lines changed

src/opengeodeweb_back/routes/create/blueprint_create.py

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,31 @@
11
# Standard library imports
22
import json
33
import os
4-
from typing import Any, TypedDict, cast
4+
from typing import cast, Any
55
# Third party imports
66
import flask
77
import opengeode
8-
import werkzeug
98
# Local application imports
109
from opengeodeweb_back import geode_functions, utils_functions
11-
from opengeodeweb_microservice.database.data import Data
12-
from opengeodeweb_microservice.database.connection import get_session
1310
from opengeodeweb_back.utils_functions import save_all_viewables_and_return_info
1411

1512
routes = flask.Blueprint("create", __name__, url_prefix="/opengeodeweb_back/create")
1613
schemas = os.path.join(os.path.dirname(__file__), "schemas")
1714

18-
# --- Type definitions for request validation ---
19-
class Point(TypedDict):
20-
x: float
21-
y: float
15+
# --- Type definitions for JSON and RPC ---
16+
type JsonPrimitive = str | int | float | bool
17+
type JsonValue = JsonPrimitive | dict[str, JsonValue] | list[JsonValue]
18+
type RpcParams = dict[str, JsonValue]
19+
type SchemaDict = dict[str, Any] # Changé pour éviter cast sur json.load
2220

23-
class CreatePointRequest(TypedDict):
24-
name: str
25-
x: float
26-
y: float
27-
z: float
28-
29-
class CreateAOIRequest(TypedDict):
30-
name: str
31-
points: list[Point]
32-
z: float
21+
# --- Specialized type aliases for each RPC endpoint ---
22+
type PointDict = dict[str, float] # {"x": float, "y": float}
23+
type CreatePointParams = dict[str, str | float] # {"name": str, "x": float, "y": float, "z": float}
24+
type CreateAOIParams = dict[str, str | float | list[PointDict]] # {"name": str, "points": list[PointDict], "z": float}
3325

3426
# Load schema for point creation
3527
with open(os.path.join(schemas, "create_point.json"), "r") as file:
36-
create_point_json = cast(dict[str, Any], json.load(file))
28+
create_point_json: SchemaDict = json.load(file)
3729

3830
@routes.route(
3931
create_point_json["route"],
@@ -45,23 +37,23 @@ def create_point() -> flask.Response:
4537
utils_functions.validate_request(flask.request, create_point_json)
4638

4739
# Extract and validate data from request
48-
request_data = cast(CreatePointRequest, flask.request.json)
49-
name: str = request_data["name"]
50-
x: float = request_data["x"]
51-
y: float = request_data["y"]
52-
z: float = request_data["z"]
40+
params: CreatePointParams = flask.request.get_json() # type: ignore
41+
name: str = params["name"] # type: ignore
42+
x: float = params["x"] # type: ignore
43+
y: float = params["y"] # type: ignore
44+
z: float = params["z"] # type: ignore
5345

54-
# Create the point set
46+
# Create the point
5547
class_ = geode_functions.geode_object_class("PointSet3D")
56-
point_set = class_.create()
57-
builder = geode_functions.create_builder("PointSet3D", point_set)
58-
builder.create_point(opengeode.Point3D([x, y, z]))
48+
pointset = class_.create()
49+
builder = geode_functions.create_builder("PointSet3D", pointset)
5950
builder.set_name(name)
51+
builder.create_point(opengeode.Point3D([x, y, z]))
6052

6153
# Save and get info
6254
result = save_all_viewables_and_return_info(
6355
geode_object="PointSet3D",
64-
data=point_set,
56+
data=pointset,
6557
)
6658
result["name"] = name
6759
if "binary_light_viewable" not in result:
@@ -70,7 +62,7 @@ def create_point() -> flask.Response:
7062

7163
# Load schema for AOI creation
7264
with open(os.path.join(schemas, "create_aoi.json"), "r") as file:
73-
create_aoi_json = cast(dict[str, Any], json.load(file))
65+
create_aoi_json: SchemaDict = json.load(file)
7466

7567
@routes.route(
7668
create_aoi_json["route"],
@@ -82,10 +74,10 @@ def create_aoi() -> flask.Response:
8274
utils_functions.validate_request(flask.request, create_aoi_json)
8375

8476
# Extract and validate data from request
85-
request_data = cast(CreateAOIRequest, flask.request.json)
86-
name: str = request_data["name"]
87-
points: list[Point] = request_data["points"]
88-
z: float = request_data["z"]
77+
params: CreateAOIParams = flask.request.get_json() # type: ignore
78+
name: str = params["name"] # type: ignore
79+
points: list[PointDict] = params["points"] # type: ignore
80+
z: float = params["z"] # type: ignore
8981

9082
# Create the edged curve
9183
class_ = geode_functions.geode_object_class("EdgedCurve3D")

0 commit comments

Comments
 (0)