Skip to content

Commit 10111cb

Browse files
committed
feat: implement middleware management API with comprehensive tests
1 parent 117f461 commit 10111cb

2 files changed

Lines changed: 44 additions & 44 deletions

File tree

pro_tes/api/middlewares/controllers.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import logging
99
import math
1010
from datetime import datetime
11-
from typing import Optional
11+
from typing import Optional, Any
1212

1313
from bson import ObjectId
1414
from flask import current_app, request
@@ -126,7 +126,7 @@ def ListMiddlewares(
126126
try:
127127
collection = get_middleware_collection()
128128

129-
filter_dict = {}
129+
filter_dict: dict = {}
130130
if enabled is not None:
131131
filter_dict["enabled"] = enabled
132132
if source is not None:
@@ -171,7 +171,7 @@ def AddMiddleware() -> tuple:
171171
collection = get_middleware_collection()
172172
data = request.json
173173

174-
middleware = MiddlewareCreate(**data)
174+
middleware = MiddlewareCreate(**data) # type: ignore[arg-type]
175175

176176
# Derive name if not provided
177177
name = middleware.name
@@ -211,12 +211,12 @@ def AddMiddleware() -> tuple:
211211
now = datetime.utcnow().isoformat() + "Z"
212212

213213
# Convert Pydantic model source to dict for MongoDB storage
214-
source_data = middleware.source
214+
source_data: Any = middleware.source
215215
if isinstance(source_data, list):
216216
source_data = [
217217
s.model_dump() if hasattr(s, 'model_dump') else s
218218
for s in source_data
219-
]
219+
] # type: ignore[misc]
220220
elif hasattr(source_data, 'model_dump'):
221221
source_data = source_data.model_dump()
222222

@@ -304,9 +304,9 @@ def UpdateMiddleware(middleware_id: str) -> dict:
304304
)
305305

306306
data = request.json
307-
update_data = MiddlewareUpdate(**data)
307+
update_data = MiddlewareUpdate(**data) # type: ignore[arg-type]
308308

309-
update_dict = {}
309+
update_dict: dict = {}
310310

311311
if update_data.name is not None:
312312
if update_data.name != existing.get("name"):
@@ -316,7 +316,7 @@ def UpdateMiddleware(middleware_id: str) -> dict:
316316
f"Middleware with name "
317317
f"'{update_data.name}' already exists"
318318
)
319-
update_dict["name"] = update_data.name
319+
update_dict["name"] = update_data.name # type: ignore[assignment]
320320

321321
if (update_data.order is not None and
322322
update_data.order != existing["order"]):
@@ -334,13 +334,13 @@ def UpdateMiddleware(middleware_id: str) -> dict:
334334
{"$inc": {"order": 1}}
335335
)
336336

337-
update_dict["order"] = new_order
337+
update_dict["order"] = new_order # type: ignore[assignment]
338338

339339
if update_data.config is not None:
340-
update_dict["config"] = update_data.config
340+
update_dict["config"] = update_data.config # type: ignore[assignment]
341341

342342
if update_data.enabled is not None:
343-
update_dict["enabled"] = update_data.enabled
343+
update_dict["enabled"] = update_data.enabled # type: ignore[assignment]
344344

345345
update_dict["updated_at"] = datetime.utcnow().isoformat() + "Z"
346346

@@ -418,7 +418,7 @@ def ReorderMiddlewares() -> dict:
418418
data = request.json
419419

420420
# Use correct field name from OpenAPI spec
421-
middleware_ids = data.get("ordered_ids", [])
421+
middleware_ids = data.get("ordered_ids", []) if data else []
422422

423423
if not middleware_ids:
424424
raise BadRequest("ordered_ids array is required")

pro_tes/api/middlewares/models.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ class MiddlewareSourceLocal(BaseModel):
2323
description=(
2424
"Class path entry point (e.g., 'package.module.ClassName')"
2525
),
26-
example=(
26+
json_schema_extra={"example": (
2727
"pro_tes.plugins.middlewares.task_distribution.distance."
2828
"TaskDistributionDistance"
29-
)
29+
)}
3030
)
3131

3232

@@ -39,18 +39,18 @@ class MiddlewareSourceGithub(BaseModel):
3939
description=(
4040
"Class path entry point (e.g., 'package.module.ClassName')"
4141
),
42-
example="custom_middleware.LoadBalancer"
42+
json_schema_extra={"example": "custom_middleware.LoadBalancer"}
4343
)
4444
repository: str = Field(
4545
...,
4646
description="Git repository URL",
4747
pattern=r'^https://github\.com/.+\.git$',
48-
example="https://github.com/user/repo.git"
48+
json_schema_extra={"example": "https://github.com/user/repo.git"}
4949
)
5050
version: Optional[str] = Field(
5151
None,
5252
description="Git tag or branch name",
53-
example="v1.0.0"
53+
json_schema_extra={"example": "v1.0.0"}
5454
)
5555

5656

@@ -63,17 +63,17 @@ class MiddlewareSourcePypi(BaseModel):
6363
description=(
6464
"Class path entry point (e.g., 'package.module.ClassName')"
6565
),
66-
example="custom.Middleware"
66+
json_schema_extra={"example": "custom.Middleware"}
6767
)
6868
package: str = Field(
6969
...,
7070
description="Package name from PyPI",
71-
example="protes-middleware-custom"
71+
json_schema_extra={"example": "protes-middleware-custom"}
7272
)
7373
version: Optional[str] = Field(
7474
None,
7575
description="Package version",
76-
example="1.0.0"
76+
json_schema_extra={"example": "1.0.0"}
7777
)
7878

7979

@@ -100,7 +100,7 @@ class MiddlewareCreate(BaseModel):
100100
"Human-readable name. If not provided, "
101101
"derived from package/repo name."
102102
),
103-
example="Distance-based Router"
103+
json_schema_extra={"example": "Distance-based Router"}
104104
)
105105
source: Union[MiddlewareSource, List[MiddlewareSource]] = Field(
106106
...,
@@ -113,17 +113,17 @@ class MiddlewareCreate(BaseModel):
113113
"Execution order (0 = first). "
114114
"If not provided, defaults to 0."
115115
),
116-
example=0
116+
json_schema_extra={"example": 0}
117117
)
118118
config: Optional[dict] = Field(
119119
None,
120120
description="Middleware-specific configuration",
121-
example={"timeout": 30, "retries": 3}
121+
json_schema_extra={"example": {"timeout": 30}, "retries": 3}
122122
)
123123
enabled: bool = Field(
124124
True,
125125
description="Whether the middleware should be active",
126-
example=True
126+
json_schema_extra={"example": True}
127127
)
128128

129129

@@ -135,23 +135,23 @@ class MiddlewareUpdate(BaseModel):
135135
min_length=1,
136136
max_length=255,
137137
description="Human-readable name for the middleware",
138-
example="Distance-based Router v2"
138+
json_schema_extra={"example": "Distance-based Router v2"}
139139
)
140140
order: Optional[int] = Field(
141141
None,
142142
ge=0,
143143
description="Execution order",
144-
example=1
144+
json_schema_extra={"example": 1}
145145
)
146146
config: Optional[dict] = Field(
147147
None,
148148
description="Middleware-specific configuration",
149-
example={"timeout": 60, "retries": 5}
149+
json_schema_extra={"example": {"timeout": 60}, "retries": 5}
150150
)
151151
enabled: Optional[bool] = Field(
152152
None,
153153
description="Whether the middleware is active",
154-
example=False
154+
json_schema_extra={"example": False}
155155
)
156156

157157

@@ -162,12 +162,12 @@ class MiddlewareConfig(BaseModel):
162162
...,
163163
alias="_id",
164164
description="Unique identifier (MongoDB ObjectId)",
165-
example="507f1f77bcf86cd799439011"
165+
json_schema_extra={"example": "507f1f77bcf86cd799439011"}
166166
)
167167
name: Optional[str] = Field(
168168
None,
169169
description="Human-readable name for the middleware",
170-
example="Distance-based Router"
170+
json_schema_extra={"example": "Distance-based Router"}
171171
)
172172
source: Union[MiddlewareSource, List[MiddlewareSource]] = Field(
173173
...,
@@ -176,27 +176,27 @@ class MiddlewareConfig(BaseModel):
176176
order: int = Field(
177177
...,
178178
description="Execution order (0 = first)",
179-
example=0
179+
json_schema_extra={"example": 0}
180180
)
181181
config: Optional[dict] = Field(
182182
None,
183183
description="Middleware-specific configuration",
184-
example={"timeout": 30, "retries": 3}
184+
json_schema_extra={"example": {"timeout": 30}, "retries": 3}
185185
)
186186
enabled: bool = Field(
187187
...,
188188
description="Whether the middleware is active",
189-
example=True
189+
json_schema_extra={"example": True}
190190
)
191191
created_at: str = Field(
192192
...,
193193
description="Creation timestamp",
194-
example="2026-01-24T10:30:00Z"
194+
json_schema_extra={"example": "2026-01-24T10:30:00Z"}
195195
)
196196
updated_at: str = Field(
197197
...,
198198
description="Last update timestamp",
199-
example="2026-01-24T10:30:00Z"
199+
json_schema_extra={"example": "2026-01-24T10:30:00Z"}
200200
)
201201

202202
class Config:
@@ -211,22 +211,22 @@ class PaginationInfo(BaseModel):
211211
page: int = Field(
212212
...,
213213
description="Current page number (0-indexed)",
214-
example=0
214+
json_schema_extra={"example": 0}
215215
)
216216
page_size: int = Field(
217217
...,
218218
description="Number of results per page",
219-
example=50
219+
json_schema_extra={"example": 50}
220220
)
221221
total: int = Field(
222222
...,
223223
description="Total number of middlewares available",
224-
example=5
224+
json_schema_extra={"example": 5}
225225
)
226226
total_pages: int = Field(
227227
...,
228228
description="Total number of pages available",
229-
example=1
229+
json_schema_extra={"example": 1}
230230
)
231231

232232

@@ -250,17 +250,17 @@ class MiddlewareCreateResponse(BaseModel):
250250
...,
251251
alias="_id",
252252
description="Unique identifier of created middleware",
253-
example="507f1f77bcf86cd799439011"
253+
json_schema_extra={"example": "507f1f77bcf86cd799439011"}
254254
)
255255
order: int = Field(
256256
...,
257257
description="Assigned execution order",
258-
example=0
258+
json_schema_extra={"example": 0}
259259
)
260260
message: str = Field(
261261
...,
262262
description="Success message",
263-
example="Middleware added successfully"
263+
json_schema_extra={"example": "Middleware added successfully"}
264264
)
265265

266266
class Config:
@@ -274,9 +274,9 @@ class MiddlewareOrder(BaseModel):
274274

275275
ordered_ids: List[str] = Field(
276276
...,
277-
min_items=1,
277+
min_length=1,
278278
description="Array of middleware IDs in desired execution order",
279-
example=["507f1f77bcf86cd799439011", "507f1f77bcf86cd799439012"]
279+
json_schema_extra={"example": ["507f1f77bcf86cd799439011", "507f1f77bcf86cd799439012"]}
280280
)
281281

282282

0 commit comments

Comments
 (0)