|
37 | 37 | User,
|
38 | 38 | )
|
39 | 39 | from iib.web.s3_utils import get_object_from_s3_bucket
|
| 40 | +from iib.web.pydantic_models import AddPydanticModel, RmPydanticModel |
40 | 41 | from botocore.response import StreamingBody
|
41 | 42 | from iib.web.utils import pagination_metadata, str_to_bool
|
42 | 43 | from iib.workers.tasks.build import (
|
@@ -576,10 +577,16 @@ def add_bundles() -> Tuple[flask.Response, int]:
|
576 | 577 | :rtype: flask.Response
|
577 | 578 | :raise ValidationError: if required parameters are not supplied
|
578 | 579 | """
|
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') |
582 | 580 |
|
| 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() |
583 | 590 | # Only run `_get_unique_bundles` if it is a list. If it's not, `from_json`
|
584 | 591 | # will raise an error to the user.
|
585 | 592 | if payload.get('bundles') and isinstance(payload['bundles'], list):
|
@@ -807,7 +814,15 @@ def rm_operators() -> Tuple[flask.Response, int]:
|
807 | 814 | :rtype: flask.Response
|
808 | 815 | :raise ValidationError: if required parameters are not supplied
|
809 | 816 | """
|
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()) |
811 | 826 | if not isinstance(payload, dict):
|
812 | 827 | raise ValidationError('The input data must be a JSON object')
|
813 | 828 |
|
|
0 commit comments