#!/usr/bin/env python
'''
Flask app that provides a RESTful API to
the multiscanner.

Proposed supported operations:
GET / ---> Test functionality. {'Message': 'True'}
GET /api/v1/tasks/list  ---> Receive list of tasks in multiscanner
GET /api/v1/tasks/<task_id> ---> receive report in JSON format
GET /api/v1/tasks/delete/<task_id> ----> delete task_id
POST /api/v1/tasks/create ---> POST file and receive report id
'''

from flask import Flask, jsonify, make_response, request, abort

TASKS = [
    {'id': 1, 'report': {"/tmp/example.log":{"MD5":"53f43f9591749b8cae536ff13e48d6de","SHA256":"815d310bdbc8684c1163b62f583dbaffb2df74b9104e2aadabf8f8491bafab66","libmagic":"ASCII text"}}},
    {'id': 2, 'report': {"/opt/qtip/grep_in_mem.py":{"MD5":"96b47da202ddba8d7a6b91fecbf89a41","SHA256":"26d11f0ea5cc77a59b6e47deee859440f26d2d14440beb712dbac8550d35ef1f","libmagic":"a /bin/python script text executable"}}},
]

TASK_NOT_FOUND = {'Message': 'No task with that ID not found!'}
INVALID_REQUEST = {'Message': 'Invalid request parameters'}

app = Flask(__name__)


@app.errorhandler(400)
def invalid_request(error):
    return make_response(jsonify(INVALID_REQUEST), 400)

@app.errorhandler(404)
def not_found(error):
    return make_response(jsonify(TASK_NOT_FOUND), 404)


@app.route('/')
def index():
    return jsonify({'Message': 'True'})


@app.route('/api/v1/tasks/list/', methods=['GET'])
def task_list():
    return jsonify({'tasks': TASKS})


@app.route('/api/v1/tasks/list/<int:task_id>', methods=['GET'])
def get_task(task_id):
    task = [task for task in TASKS if task['id'] == task_id]
    if len(task) == 0:
        abort(404)
    return jsonify({'Message': task[0]})


@app.route('/api/v1/tasks/delete/<int:task_id>', methods=['GET'])
def delete_task(task_id):
    task = [task for task in TASKS if task['id'] == task_id]
    if len(task) == 0:
        abort(404)
    TASKS.remove(task[0])
    return jsonify({'Message': 'Deleted'})


@app.route('/api/v1/tasks/create/', methods=['POST'])
def create_task():
    if not request.json or not 'report' in request.json:
        abort(400)
    task = {
        'id': TASKS[-1]['id'] + 1,
        'report': request.json['report'],
    }
    TASKS.append(task)
    return jsonify({'Message': 'Added'}), 201

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80, debug=True)

@@ -1032,16 +1032,94 @@
 ters'%7D%0A%0A
+HTTP_OK = 200%0AHTTP_CREATED = 201%0AHTTP_BAD_REQUEST = 400%0AHTTP_NOT_FOUND = 404%0A%0A
 app = Fl
@@ -1152,19 +1152,32 @@
 handler(
-400
+HTTP_BAD_REQUEST
 )%0Adef in
@@ -1249,19 +1249,32 @@
 QUEST), 
-400
+HTTP_BAD_REQUEST
 )%0A%0A@app.
@@ -1286,19 +1286,30 @@
 handler(
-404
+HTTP_NOT_FOUND
 )%0Adef no
@@ -1374,19 +1374,30 @@
 FOUND), 
-404
+HTTP_NOT_FOUND
 )%0A%0A%0A@app
@@ -1751,27 +1751,38 @@
       abort(
-404
+HTTP_NOT_FOUND
 )%0A    return
@@ -2004,19 +2004,30 @@
   abort(
-404
+HTTP_NOT_FOUND
 )%0A    TA
@@ -2235,19 +2235,32 @@
   abort(
-400
+HTTP_BAD_REQUEST
 )%0A    ta
@@ -2414,19 +2414,28 @@
 ded'%7D), 
-201
+HTTP_CREATED
 %0A%0Aif __n
@@ -2490,16 +2490,18 @@
  port=80
+80
 , debug=

Make status codes global constants