Folded all failure responses into a single function
This commit is contained in:
parent
311ae0276e
commit
d56e5e2cb2
@ -4,7 +4,7 @@ from pathlib import Path
|
||||
import json
|
||||
import re
|
||||
|
||||
from flask import Flask, redirect, request, Response
|
||||
from flask import jsonify, Flask, redirect, request, Response
|
||||
|
||||
from .wireguard import WireGuard
|
||||
from .oauth import Oauth
|
||||
@ -29,6 +29,12 @@ required_entitlement = config.get('security',
|
||||
fallback=None)
|
||||
|
||||
|
||||
def fail(message: str) -> Response:
|
||||
response = jsonify({'result': 'failed',
|
||||
'reason': message})
|
||||
response.status = 400
|
||||
return response
|
||||
|
||||
@app.before_request
|
||||
def setup() -> None:
|
||||
if request.path in public_paths:
|
||||
@ -94,8 +100,7 @@ def get_config(config_id: str) -> dict:
|
||||
try:
|
||||
return app.wg.get_config(config_id)
|
||||
except FileNotFoundError:
|
||||
return {'result': 'failed',
|
||||
'reason': 'Config id not found'}
|
||||
return fail('Config id not found')
|
||||
|
||||
@app.route('/configs/<config_id>/create', methods=['POST'])
|
||||
def create_config(config_id: str) -> dict:
|
||||
@ -104,19 +109,16 @@ def create_config(config_id: str) -> dict:
|
||||
description = data['description']
|
||||
creation_time = datetime.now()
|
||||
if not permitted_format.match(config_id):
|
||||
return {'result': 'failed',
|
||||
'reason': 'Invalid config id'}
|
||||
return fail('Invalid config id')
|
||||
if not name:
|
||||
return {'result': 'failed',
|
||||
'reason': 'Name is mandatory'}
|
||||
return fail('Name is mandatory')
|
||||
try:
|
||||
app.wg.generate_config_files(config_id,
|
||||
name,
|
||||
description,
|
||||
creation_time)
|
||||
except FileExistsError:
|
||||
return {'result': 'failed',
|
||||
'reason': 'Id already in use'}
|
||||
return fail('Id already in use')
|
||||
return get_config(config_id)
|
||||
|
||||
@app.route('/configs/<config_id>/update', methods=['POST'])
|
||||
@ -127,8 +129,7 @@ def update_config(config_id: str) -> dict:
|
||||
try:
|
||||
app.wg.update_config(config_id, name, description)
|
||||
except FileNotFoundError:
|
||||
return {'result': 'failed',
|
||||
'reason': 'Config id not found'}
|
||||
return fail('Config id not found')
|
||||
return {'result': 'success'}
|
||||
|
||||
@app.route('/configs/<config_id>/delete', methods=['POST', 'DELETE'])
|
||||
@ -136,6 +137,5 @@ def delete_config(config_id: str) -> dict:
|
||||
try:
|
||||
app.wg.delete_config(config_id)
|
||||
except FileNotFoundError:
|
||||
return {'result': 'failed',
|
||||
'reason': 'Config id not found'}
|
||||
return fail('Config id not found')
|
||||
return {'result': 'success'}
|
||||
|
Loading…
x
Reference in New Issue
Block a user