diff options
author | John Estabrook <jestabro@vyos.io> | 2020-05-06 16:32:02 -0500 |
---|---|---|
committer | John Estabrook <jestabro@vyos.io> | 2020-05-06 19:12:32 -0500 |
commit | dbe9f73cc9180b5c0d06007476d7120cde51725d (patch) | |
tree | f5e30f1f1747553915131b93a3d4986ba3722265 /src/services/vyos-http-api-server | |
parent | 25c65547458a3a2f4f4f8b1b70541229f3cbcc0c (diff) | |
download | vyos-1x-dbe9f73cc9180b5c0d06007476d7120cde51725d.tar.gz vyos-1x-dbe9f73cc9180b5c0d06007476d7120cde51725d.zip |
http api: use decorator to get command data from request
Diffstat (limited to 'src/services/vyos-http-api-server')
-rwxr-xr-x | src/services/vyos-http-api-server | 56 |
1 files changed, 26 insertions, 30 deletions
diff --git a/src/services/vyos-http-api-server b/src/services/vyos-http-api-server index 4928b0bae..5cad67eb7 100755 --- a/src/services/vyos-http-api-server +++ b/src/services/vyos-http-api-server @@ -63,6 +63,20 @@ def success(data): resp = {"success": True, "data": data, "error": None} return json.dumps(resp) +def get_command(f): + @wraps(f) + def decorated_function(*args, **kwargs): + cmd = bottle.request.forms.get("data") + if not cmd: + return error(400, "Non-empty data field is required") + try: + cmd = json.loads(cmd) + except Exception as e: + return error(400, "Failed to parse JSON: {0}".format(e)) + return f(cmd, *args, **kwargs) + + return decorated_function + def auth_required(f): @wraps(f) def decorated_function(*args, **kwargs): @@ -76,8 +90,9 @@ def auth_required(f): return decorated_function @app.route('/configure', method='POST') +@get_command @auth_required -def configure_op(): +def configure_op(commands): session = app.config['vyos_session'] env = session.get_session_env() config = vyos.config.Config(session_env=env) @@ -88,15 +103,6 @@ def configure_op(): else: strict = False - commands = bottle.request.forms.get("data") - if not commands: - return error(400, "Non-empty data field is required") - else: - try: - commands = json.loads(commands) - except Exception as e: - return error(400, "Failed to parse JSON: {0}".format(e)) - # Allow users to pass just one command if not isinstance(commands, list): commands = [commands] @@ -187,15 +193,13 @@ def configure_op(): return success(None) @app.route('/retrieve', method='POST') +@get_command @auth_required -def retrieve_op(): +def retrieve_op(command): session = app.config['vyos_session'] env = session.get_session_env() config = vyos.config.Config(session_env=env) - command = bottle.request.forms.get("data") - command = json.loads(command) - try: op = command['op'] path = " ".join(command['path']) @@ -238,13 +242,11 @@ def retrieve_op(): return success(res) @app.route('/config-file', method='POST') +@get_command @auth_required -def config_file_op(): +def config_file_op(command): session = app.config['vyos_session'] - command = bottle.request.forms.get("data") - command = json.loads(command) - try: op = command['op'] except KeyError: @@ -275,13 +277,11 @@ def config_file_op(): return success(res) @app.route('/image', method='POST') +@get_command @auth_required -def image_op(): +def image_op(command): session = app.config['vyos_session'] - command = bottle.request.forms.get("data") - command = json.loads(command) - try: op = command['op'] except KeyError: @@ -312,13 +312,11 @@ def image_op(): @app.route('/generate', method='POST') +@get_command @auth_required -def generate_op(): +def generate_op(command): session = app.config['vyos_session'] - command = bottle.request.forms.get("data") - command = json.loads(command) - try: op = command['op'] path = command['path'] @@ -342,13 +340,11 @@ def generate_op(): return success(res) @app.route('/show', method='POST') +@get_command @auth_required -def show_op(): +def show_op(command): session = app.config['vyos_session'] - command = bottle.request.forms.get("data") - command = json.loads(command) - try: op = command['op'] path = command['path'] |