From 55d33cebb83a83d5dd0cfa26978c486be68e6949 Mon Sep 17 00:00:00 2001 From: Rui Alves Date: Mon, 16 Sep 2024 16:53:22 +0100 Subject: [PATCH] Use body for multiple events endpoints --- frigate/api/defs/events_body.py | 14 ++++++++++++++ frigate/api/event.py | 15 +++++++-------- frigate/api/fastapi_app.py | 3 ++- 3 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 frigate/api/defs/events_body.py diff --git a/frigate/api/defs/events_body.py b/frigate/api/defs/events_body.py new file mode 100644 index 000000000..3ffad2d11 --- /dev/null +++ b/frigate/api/defs/events_body.py @@ -0,0 +1,14 @@ +from typing import Union + +from pydantic import BaseModel, Field + + +class EventsSubLabelBody(BaseModel): + subLabel: str + subLabelScore: float + + +class EventsDescriptionBody(BaseModel): + description: Union[str, None] = Field( + title="The description of the event", min_length=1 + ) diff --git a/frigate/api/event.py b/frigate/api/event.py index a3b193575..420d4e496 100644 --- a/frigate/api/event.py +++ b/frigate/api/event.py @@ -18,6 +18,7 @@ from peewee import JOIN, DoesNotExist, fn, operator from PIL import Image from playhouse.shortcuts import model_to_dict +from frigate.api.defs.events_body import EventsDescriptionBody, EventsSubLabelBody from frigate.api.defs.events_query_parameters import ( DEFAULT_TIME_RANGE, EventsQueryParams, @@ -585,7 +586,7 @@ def event(event_id: str): try: return model_to_dict(Event.get(Event.id == event_id)) except DoesNotExist: - return "Event not found", 404 + return JSONResponse(content="Event not found", status_code=404) @router.post("/events/{event_id}/retain") @@ -833,7 +834,7 @@ def delete_retain(event_id: str): def set_sub_label( request: Request, event_id: str, - body: dict = None, + body: EventsSubLabelBody = None, ): try: event: Event = Event.get(Event.id == event_id) @@ -843,9 +844,8 @@ def set_sub_label( status_code=404, ) - json: dict[str, any] = body or {} - new_sub_label = json.get("subLabel") - new_score = json.get("subLabelScore") + new_sub_label = body.subLabel + new_score = body.subLabelScore if new_sub_label is None: return JSONResponse( @@ -921,7 +921,7 @@ def set_sub_label( def set_description( request: Request, event_id: str, - body: dict = None, + body: EventsDescriptionBody = None, ): try: event: Event = Event.get(Event.id == event_id) @@ -931,8 +931,7 @@ def set_description( status_code=404, ) - json: dict[str, any] = body or {} - new_description = json.get("description") + new_description = body.description if new_description is None or len(new_description) == 0: return JSONResponse( diff --git a/frigate/api/fastapi_app.py b/frigate/api/fastapi_app.py index 4a23802e4..a488b6bec 100644 --- a/frigate/api/fastapi_app.py +++ b/frigate/api/fastapi_app.py @@ -4,7 +4,7 @@ from typing import Optional from fastapi import FastAPI from frigate.api import app as main_app -from frigate.api import export, media, notification, preview, review +from frigate.api import event, export, media, notification, preview, review from frigate.embeddings import EmbeddingsContext from frigate.events.external import ExternalEventProcessor from frigate.plus import PlusApi @@ -37,6 +37,7 @@ def create_fastapi_app( app.include_router(notification.router) app.include_router(review.router) app.include_router(export.router) + app.include_router(event.router) # App Properties app.frigate_config = frigate_config app.embeddings = embeddings