Skip to content

Commit

Permalink
Add Pydantic to IIB web
Browse files Browse the repository at this point in the history
  • Loading branch information
xDaile committed Dec 8, 2023
1 parent 8254277 commit 87d221c
Show file tree
Hide file tree
Showing 6 changed files with 811 additions and 5 deletions.
23 changes: 19 additions & 4 deletions iib/web/api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
User,
)
from iib.web.s3_utils import get_object_from_s3_bucket
from iib.web.pydantic_models import AddPydanticModel, RmPydanticModel
from botocore.response import StreamingBody
from iib.web.utils import pagination_metadata, str_to_bool
from iib.workers.tasks.build import (
Expand Down Expand Up @@ -576,10 +577,16 @@ def add_bundles() -> Tuple[flask.Response, int]:
:rtype: flask.Response
:raise ValidationError: if required parameters are not supplied
"""
payload: AddRequestPayload = cast(AddRequestPayload, flask.request.get_json())
if not isinstance(payload, dict):
raise ValidationError('The input data must be a JSON object')

try:
add_request_payload = AddRequestPydanticModel.model_validate(
flask.request.data, strict=True,
)
except ValidationError as e:
# If the JSON data doesn't match the Pydantic model, return a 400 Bad Request response
return flask.jsonify({'Error parsing data': str(e)}), 400

payload = add_request_payload.model_dump_json()
# Only run `_get_unique_bundles` if it is a list. If it's not, `from_json`
# will raise an error to the user.
if payload.get('bundles') and isinstance(payload['bundles'], list):
Expand Down Expand Up @@ -807,7 +814,15 @@ def rm_operators() -> Tuple[flask.Response, int]:
:rtype: flask.Response
:raise ValidationError: if required parameters are not supplied
"""
payload: RmRequestPayload = cast(RmRequestPayload, flask.request.get_json())
try:
rm_request_payload = RmRequestPydanticModel.model_validate(
flask.request.data, strict=True,
)
except ValidationError as e:
# If the JSON data doesn't match the Pydantic model, return a 400 Bad Request response
return flask.jsonify({'Error parsing data': str(e)}), 400

payload: RmRequestPayload = cast(RmRequestPayload, rm_request_payload.model_dump_json())
if not isinstance(payload, dict):
raise ValidationError('The input data must be a JSON object')

Expand Down
Loading

0 comments on commit 87d221c

Please sign in to comment.