Improved API input validation

This commit is contained in:
CasVT
2023-06-23 00:14:46 +02:00
parent 348b0b4f0a
commit 32d4faaa16
9 changed files with 412 additions and 266 deletions

View File

@@ -2,20 +2,18 @@
from typing import List
from apprise import Apprise
from backend.custom_exceptions import (InvalidURL, NotificationServiceInUse,
from backend.custom_exceptions import (NotificationServiceInUse,
NotificationServiceNotFound)
from backend.db import get_db
class NotificationService:
def __init__(self, notification_service_id: int) -> None:
def __init__(self, user_id: int, notification_service_id: int) -> None:
self.id = notification_service_id
if not get_db().execute(
"SELECT 1 FROM notification_services WHERE id = ? LIMIT 1;",
(self.id,)
"SELECT 1 FROM notification_services WHERE id = ? AND user_id = ? LIMIT 1;",
(self.id, user_id)
).fetchone():
raise NotificationServiceNotFound
@@ -46,9 +44,7 @@ class NotificationService:
Returns:
dict: The new info about the service
"""
if not Apprise().add(url):
raise InvalidURL
# Get current data and update it with new values
data = self.get()
new_values = {
@@ -157,7 +153,7 @@ class NotificationServices:
Returns:
NotificationService: Instance of NotificationService
"""
return NotificationService(notification_service_id)
return NotificationService(self.user_id, notification_service_id)
def add(self, title: str, url: str) -> NotificationService:
"""Add a notification service
@@ -166,15 +162,10 @@ class NotificationServices:
title (str): The title of the service
url (str): The apprise url of the service
Raises:
InvalidURL: The apprise url is invalid
Returns:
dict: The info about the new service
"""
if not Apprise().add(url):
raise InvalidURL
new_id = get_db().execute("""
INSERT INTO notification_services(user_id, title, url)
VALUES (?,?,?)