Convert all notifications API endpoints to FastAPI

This commit is contained in:
Rui Alves 2024-09-15 17:08:41 +01:00
parent 1f11d825ef
commit 023f1431f2
4 changed files with 29 additions and 30 deletions

View File

@ -23,7 +23,6 @@ from frigate.api.auth import AuthBp, get_jwt_secret, limiter
from frigate.api.defs.tags import Tags
from frigate.api.event import EventBp
from frigate.api.export import ExportBp
from frigate.api.notification import NotificationBp
from frigate.api.review import ReviewBp
from frigate.config import FrigateConfig
from frigate.const import CONFIG_DIR
@ -50,7 +49,6 @@ bp.register_blueprint(EventBp)
bp.register_blueprint(ExportBp)
bp.register_blueprint(ReviewBp)
bp.register_blueprint(AuthBp)
bp.register_blueprint(NotificationBp)
router = APIRouter()

View File

@ -6,3 +6,4 @@ class Tags(Enum):
preview = "Preview"
logs = "Logs"
media = "Media"
notifications = "Notifications"

View File

@ -3,7 +3,7 @@ import logging
from fastapi import FastAPI
from frigate.api import app as main_app
from frigate.api import media, preview
from frigate.api import media, notification, preview
from frigate.plus import PlusApi
from frigate.ptz.onvif import OnvifController
from frigate.stats.emitter import StatsEmitter
@ -29,6 +29,7 @@ def create_fastapi_app(
app.include_router(main_app.router)
app.include_router(media.router)
app.include_router(preview.router)
app.include_router(notification.router)
# App Properties
app.frigate_config = frigate_config
app.detected_frames_processor = detected_frames_processor

View File

@ -4,62 +4,61 @@ import logging
import os
from cryptography.hazmat.primitives import serialization
from flask import (
Blueprint,
current_app,
jsonify,
make_response,
request,
)
from fastapi import APIRouter, Request
from fastapi.responses import JSONResponse
from peewee import DoesNotExist
from py_vapid import Vapid01, utils
from frigate.api.defs.tags import Tags
from frigate.const import CONFIG_DIR
from frigate.models import User
logger = logging.getLogger(__name__)
NotificationBp = Blueprint("notifications", __name__)
router = APIRouter(tags=[Tags.notifications])
@NotificationBp.route("/notifications/pubkey", methods=["GET"])
def get_vapid_pub_key():
if not current_app.frigate_config.notifications.enabled:
return make_response(
jsonify({"success": False, "message": "Notifications are not enabled."}),
400,
@router.get("/notifications/pubkey")
def get_vapid_pub_key(request: Request):
if not request.app.frigate_config.notifications.enabled:
return JSONResponse(
content=({"success": False, "message": "Notifications are not enabled."}),
status_code=400,
)
key = Vapid01.from_file(os.path.join(CONFIG_DIR, "notifications.pem"))
raw_pub = key.public_key.public_bytes(
serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint
)
return jsonify(utils.b64urlencode(raw_pub)), 200
return JSONResponse(content=utils.b64urlencode(raw_pub), status_code=200)
@NotificationBp.route("/notifications/register", methods=["POST"])
def register_notifications():
if current_app.frigate_config.auth.enabled:
username = request.headers.get("remote-user", type=str) or "admin"
@router.post("/notifications/register")
def register_notifications(request: Request, body: dict = None):
if request.app.frigate_config.auth.enabled:
username = request.headers.get("remote-user") or "admin"
else:
username = "admin"
json: dict[str, any] = request.get_json(silent=True) or {}
json: dict[str, any] = body or {}
sub = json.get("sub")
if not sub:
return jsonify(
{"success": False, "message": "Subscription must be provided."}
), 400
return JSONResponse(
content={"success": False, "message": "Subscription must be provided."},
status_code=400,
)
try:
User.update(notification_tokens=User.notification_tokens.append(sub)).where(
User.username == username
).execute()
return make_response(
jsonify({"success": True, "message": "Successfully saved token."}), 200
return JSONResponse(
content=({"success": True, "message": "Successfully saved token."}),
status_code=200,
)
except DoesNotExist:
return make_response(
jsonify({"success": False, "message": "Could not find user."}), 404
return JSONResponse(
content=({"success": False, "message": "Could not find user."}),
status_code=404,
)