mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-02-14 15:15:22 +03:00
Convert all notifications API endpoints to FastAPI
This commit is contained in:
parent
1f11d825ef
commit
023f1431f2
@ -23,7 +23,6 @@ from frigate.api.auth import AuthBp, get_jwt_secret, limiter
|
|||||||
from frigate.api.defs.tags import Tags
|
from frigate.api.defs.tags import Tags
|
||||||
from frigate.api.event import EventBp
|
from frigate.api.event import EventBp
|
||||||
from frigate.api.export import ExportBp
|
from frigate.api.export import ExportBp
|
||||||
from frigate.api.notification import NotificationBp
|
|
||||||
from frigate.api.review import ReviewBp
|
from frigate.api.review import ReviewBp
|
||||||
from frigate.config import FrigateConfig
|
from frigate.config import FrigateConfig
|
||||||
from frigate.const import CONFIG_DIR
|
from frigate.const import CONFIG_DIR
|
||||||
@ -50,7 +49,6 @@ bp.register_blueprint(EventBp)
|
|||||||
bp.register_blueprint(ExportBp)
|
bp.register_blueprint(ExportBp)
|
||||||
bp.register_blueprint(ReviewBp)
|
bp.register_blueprint(ReviewBp)
|
||||||
bp.register_blueprint(AuthBp)
|
bp.register_blueprint(AuthBp)
|
||||||
bp.register_blueprint(NotificationBp)
|
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|||||||
@ -6,3 +6,4 @@ class Tags(Enum):
|
|||||||
preview = "Preview"
|
preview = "Preview"
|
||||||
logs = "Logs"
|
logs = "Logs"
|
||||||
media = "Media"
|
media = "Media"
|
||||||
|
notifications = "Notifications"
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import logging
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
||||||
from frigate.api import app as main_app
|
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.plus import PlusApi
|
||||||
from frigate.ptz.onvif import OnvifController
|
from frigate.ptz.onvif import OnvifController
|
||||||
from frigate.stats.emitter import StatsEmitter
|
from frigate.stats.emitter import StatsEmitter
|
||||||
@ -29,6 +29,7 @@ def create_fastapi_app(
|
|||||||
app.include_router(main_app.router)
|
app.include_router(main_app.router)
|
||||||
app.include_router(media.router)
|
app.include_router(media.router)
|
||||||
app.include_router(preview.router)
|
app.include_router(preview.router)
|
||||||
|
app.include_router(notification.router)
|
||||||
# App Properties
|
# App Properties
|
||||||
app.frigate_config = frigate_config
|
app.frigate_config = frigate_config
|
||||||
app.detected_frames_processor = detected_frames_processor
|
app.detected_frames_processor = detected_frames_processor
|
||||||
|
|||||||
@ -4,62 +4,61 @@ import logging
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from cryptography.hazmat.primitives import serialization
|
from cryptography.hazmat.primitives import serialization
|
||||||
from flask import (
|
from fastapi import APIRouter, Request
|
||||||
Blueprint,
|
from fastapi.responses import JSONResponse
|
||||||
current_app,
|
|
||||||
jsonify,
|
|
||||||
make_response,
|
|
||||||
request,
|
|
||||||
)
|
|
||||||
from peewee import DoesNotExist
|
from peewee import DoesNotExist
|
||||||
from py_vapid import Vapid01, utils
|
from py_vapid import Vapid01, utils
|
||||||
|
|
||||||
|
from frigate.api.defs.tags import Tags
|
||||||
from frigate.const import CONFIG_DIR
|
from frigate.const import CONFIG_DIR
|
||||||
from frigate.models import User
|
from frigate.models import User
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
NotificationBp = Blueprint("notifications", __name__)
|
router = APIRouter(tags=[Tags.notifications])
|
||||||
|
|
||||||
|
|
||||||
@NotificationBp.route("/notifications/pubkey", methods=["GET"])
|
@router.get("/notifications/pubkey")
|
||||||
def get_vapid_pub_key():
|
def get_vapid_pub_key(request: Request):
|
||||||
if not current_app.frigate_config.notifications.enabled:
|
if not request.app.frigate_config.notifications.enabled:
|
||||||
return make_response(
|
return JSONResponse(
|
||||||
jsonify({"success": False, "message": "Notifications are not enabled."}),
|
content=({"success": False, "message": "Notifications are not enabled."}),
|
||||||
400,
|
status_code=400,
|
||||||
)
|
)
|
||||||
|
|
||||||
key = Vapid01.from_file(os.path.join(CONFIG_DIR, "notifications.pem"))
|
key = Vapid01.from_file(os.path.join(CONFIG_DIR, "notifications.pem"))
|
||||||
raw_pub = key.public_key.public_bytes(
|
raw_pub = key.public_key.public_bytes(
|
||||||
serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint
|
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"])
|
@router.post("/notifications/register")
|
||||||
def register_notifications():
|
def register_notifications(request: Request, body: dict = None):
|
||||||
if current_app.frigate_config.auth.enabled:
|
if request.app.frigate_config.auth.enabled:
|
||||||
username = request.headers.get("remote-user", type=str) or "admin"
|
username = request.headers.get("remote-user") or "admin"
|
||||||
else:
|
else:
|
||||||
username = "admin"
|
username = "admin"
|
||||||
|
|
||||||
json: dict[str, any] = request.get_json(silent=True) or {}
|
json: dict[str, any] = body or {}
|
||||||
sub = json.get("sub")
|
sub = json.get("sub")
|
||||||
|
|
||||||
if not sub:
|
if not sub:
|
||||||
return jsonify(
|
return JSONResponse(
|
||||||
{"success": False, "message": "Subscription must be provided."}
|
content={"success": False, "message": "Subscription must be provided."},
|
||||||
), 400
|
status_code=400,
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
User.update(notification_tokens=User.notification_tokens.append(sub)).where(
|
User.update(notification_tokens=User.notification_tokens.append(sub)).where(
|
||||||
User.username == username
|
User.username == username
|
||||||
).execute()
|
).execute()
|
||||||
return make_response(
|
return JSONResponse(
|
||||||
jsonify({"success": True, "message": "Successfully saved token."}), 200
|
content=({"success": True, "message": "Successfully saved token."}),
|
||||||
|
status_code=200,
|
||||||
)
|
)
|
||||||
except DoesNotExist:
|
except DoesNotExist:
|
||||||
return make_response(
|
return JSONResponse(
|
||||||
jsonify({"success": False, "message": "Could not find user."}), 404
|
content=({"success": False, "message": "Could not find user."}),
|
||||||
|
status_code=404,
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user