refactor: made pydantic check the input.
no longer by the code itself. ref: N25B-198
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from pydantic import ValidationError
|
||||
from fastapi import APIRouter, Request
|
||||
|
||||
from control_backend.schemas.message import Message
|
||||
from control_backend.schemas.program import Program
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -11,20 +9,12 @@ router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/program", status_code=202)
|
||||
async def receive_message(program: Message, request: Request):
|
||||
async def receive_message(program: Program, request: Request):
|
||||
"""
|
||||
Receives a BehaviorProgram as a stringified JSON list inside `message`.
|
||||
Receives a BehaviorProgram, pydantic checks it.
|
||||
Converts it into real Phase objects.
|
||||
"""
|
||||
logger.debug("Received raw program: %s", program)
|
||||
raw_str = program.message # This is the JSON string
|
||||
|
||||
# Validate program
|
||||
try:
|
||||
program = Program.model_validate_json(raw_str)
|
||||
except ValidationError as e:
|
||||
logger.error("Failed to validate program JSON: %s", e)
|
||||
raise HTTPException(status_code=400, detail="Not a valid program") from None
|
||||
|
||||
# send away
|
||||
topic = b"program"
|
||||
|
||||
Reference in New Issue
Block a user