from flask import Blueprint, jsonify, request from api import require_auth, limiter from db import get_db from db.models.Webhook import Webhook from db.models.WebhookSetting import WebhookSetting webhook_bp = Blueprint('webhook', __name__, url_prefix='/api/webhook') @webhook_bp.route('/', methods=['GET']) @require_auth(roles=['admin']) def list_webhooks(): with get_db() as session: hooks = session.query(Webhook).all() return jsonify([h.to_dict() for h in hooks]), 200 @webhook_bp.route('/', methods=['POST']) @require_auth(['admin']) def create_webhook(): data = request.json hook_url = data.get('hook_url') if not hook_url: return jsonify({'error': 'hook_url required'}), 400 with get_db() as session: existing = session.query(Webhook).filter_by(hook_url=hook_url).first() if existing: return jsonify({'error': 'Webhook URL already exists'}), 409 webhook = Webhook(hook_url=hook_url) session.add(webhook) session.commit() return jsonify(webhook.to_dict()), 201 @webhook_bp.route('/', methods=['PUT', 'PATCH']) @require_auth(roles=['admin']) def update_webhook(webhook_id): data = request.json if 'hook_url' not in data: return jsonify({'error': 'hook_url is required'}), 400 with get_db() as session: webhook = session.query(Webhook).get(webhook_id) if not webhook: return jsonify({'error': 'Webhook not found'}), 404 existing = session.query(Webhook).filter_by(hook_url=data['hook_url']).filter(Webhook.id != webhook_id).first() if existing: return jsonify({'error': 'Webhook URL already exists'}), 409 webhook.hook_url = data['hook_url'] session.commit() return jsonify(webhook.to_dict()), 200 @webhook_bp.route('/', methods=['DELETE']) @require_auth(roles=['admin']) def delete_webhook(webhook_id): with get_db() as session: webhook = session.query(Webhook).get(webhook_id) if not webhook: return jsonify({'error': 'Webhook not found'}), 404 session.delete(webhook) session.commit() return jsonify({'message': 'Webhook deleted'}), 200 @webhook_bp.route('/setting/', methods=['GET']) @require_auth(roles=['admin']) def list_webhook_settings(): with get_db() as session: settings = session.query(WebhookSetting).all() return jsonify([s.to_dict() for s in settings]), 200 @webhook_bp.route('/setting/', methods=['GET']) @require_auth(roles=['admin']) def webhook_setting(setting_id): with get_db() as session: setting = session.query(WebhookSetting).filter(WebhookSetting.id == setting_id).first() if not setting: return jsonify({'info': 'Webhook setting not found'}), 204 return jsonify(setting.to_dict()), 200 @webhook_bp.route('/setting/path/', methods=['GET']) @require_auth(roles=['admin']) def webhook_setting_by_path(path_id): with get_db() as session: setting = session.query(WebhookSetting).filter(WebhookSetting.path_id == path_id).first() if not setting: return jsonify({'info': 'Webhook setting not found'}), 204 return jsonify(setting.to_dict()), 200 @webhook_bp.route('/setting/', methods=['POST']) @require_auth(roles=['admin']) def create_webhook_setting(): data = request.json print(data) required = ['path_id', 'webhook_id'] for key in required: if key not in data: return jsonify({'error': 'Required field missing'}), 400 with get_db() as session: setting = WebhookSetting( path_id=data.get('path_id'), webhook_id=data.get('webhook_id'), recursive=data.get('recursive', False), additional_header=data.get('additional_header', ''), enabled=data.get('enabled', True), on_events=data.get('on_events', 1), ) session.add(setting) session.commit() return jsonify(setting.to_dict()), 201 @webhook_bp.route('/setting/', methods=['PUT', 'PATCH']) @require_auth(roles=['admin']) def update_webhook_setting(setting_id): data = request.json with get_db() as session: setting = session.query(WebhookSetting).get(setting_id) if not setting: return jsonify({'error': 'Webhook setting not found'}), 404 if 'recursive' in data: setting.recursive = data['recursive'] if 'additional_header' in data: setting.additional_header = data['additional_header'] if 'enabled' in data: setting.enabled = data['enabled'] if 'on_events' in data: setting.on_events = data['on_events'] session.commit() return jsonify(setting.to_dict()), 200 @webhook_bp.route('/setting/', methods=['DELETE']) @require_auth(roles=['admin']) def delete_webhook_setting(setting_id): with get_db() as session: setting = session.query(WebhookSetting).get(setting_id) if not setting: return jsonify({'error': 'Webhook setting not found'}), 404 session.delete(setting) session.commit() return jsonify({'message': 'Webhook setting deleted'}), 200