from flask import Flask, request
from flask_cors import CORS
import json

from engine.handle_request import handle_basic_request

app = Flask(__name__)
CORS(app) # This will enable CORS for all routes
# CORS enabling is useful to prevent annoying CORS error when developing locally


################################################################
# parte di codice da copiare per fare un altro endpoint in Flask
@app.route('/simple_get_query', methods=['POST'])
def simpleQuery():

    # This is a stupid way of handling the JSON data, but it works;
    # trying to send application/json data causes Flask to complain and I can't understand why!    
    jsonDTO = json.loads( request.form['stringifiedDTO'] )

    appath = app.root_path

    try:
        queryList = jsonDTO['queryList']
        output = handle_basic_request(queryList, appath)

        return output, 200

    except Exception as err:
        emptyOut = {}
        return emptyOut, 500
# fino a qui
#################################################################


if __name__ == '__main__':
    app.run()