Skip to content

Commit 87d221c

Browse files
committed
Add Pydantic to IIB web
1 parent 8254277 commit 87d221c

File tree

6 files changed

+811
-5
lines changed

6 files changed

+811
-5
lines changed

iib/web/api_v1.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
User,
3838
)
3939
from iib.web.s3_utils import get_object_from_s3_bucket
40+
from iib.web.pydantic_models import AddPydanticModel, RmPydanticModel
4041
from botocore.response import StreamingBody
4142
from iib.web.utils import pagination_metadata, str_to_bool
4243
from iib.workers.tasks.build import (
@@ -576,10 +577,16 @@ def add_bundles() -> Tuple[flask.Response, int]:
576577
:rtype: flask.Response
577578
:raise ValidationError: if required parameters are not supplied
578579
"""
579-
payload: AddRequestPayload = cast(AddRequestPayload, flask.request.get_json())
580-
if not isinstance(payload, dict):
581-
raise ValidationError('The input data must be a JSON object')
582580

581+
try:
582+
add_request_payload = AddRequestPydanticModel.model_validate(
583+
flask.request.data, strict=True,
584+
)
585+
except ValidationError as e:
586+
# If the JSON data doesn't match the Pydantic model, return a 400 Bad Request response
587+
return flask.jsonify({'Error parsing data': str(e)}), 400
588+
589+
payload = add_request_payload.model_dump_json()
583590
# Only run `_get_unique_bundles` if it is a list. If it's not, `from_json`
584591
# will raise an error to the user.
585592
if payload.get('bundles') and isinstance(payload['bundles'], list):
@@ -807,7 +814,15 @@ def rm_operators() -> Tuple[flask.Response, int]:
807814
:rtype: flask.Response
808815
:raise ValidationError: if required parameters are not supplied
809816
"""
810-
payload: RmRequestPayload = cast(RmRequestPayload, flask.request.get_json())
817+
try:
818+
rm_request_payload = RmRequestPydanticModel.model_validate(
819+
flask.request.data, strict=True,
820+
)
821+
except ValidationError as e:
822+
# If the JSON data doesn't match the Pydantic model, return a 400 Bad Request response
823+
return flask.jsonify({'Error parsing data': str(e)}), 400
824+
825+
payload: RmRequestPayload = cast(RmRequestPayload, rm_request_payload.model_dump_json())
811826
if not isinstance(payload, dict):
812827
raise ValidationError('The input data must be a JSON object')
813828

0 commit comments

Comments
 (0)