66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
from flask import Blueprint, jsonify, request
|
|
from api import require_auth
|
|
from db import get_db
|
|
from db.models.Webhook import Webhook
|
|
|
|
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('/<int:webhook_id>', 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('/<int:webhook_id>', 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 |