from flask import Flask, request import traceback from engine.handle_request import handleOccGetQuery, handleGetContext, handleSingleContext from Config.config_loader import config app = Flask(__name__) config(app) ################################################################ # parte di codice da copiare per fare un altro endpoint in Flask @app.route('/simple_get_query', methods=['POST']) def simpleQuery(): # # This is -- or WAS -- a stupid (and hopefully TEMPORARY) 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! # queryDTO = json.loads( request.form['stringifiedDTO'] ) # # UP TO HERE app.logger.info('Request successfully received by the Simple Get Query API') try: queryDTO = request.get_json() # new (and correct) way! queryList = queryDTO['queryList'] cooccorrenze = queryDTO.get('cooccorrenze') output = handleOccGetQuery(queryList, cooccorrenze, app.config['DATA_CONFIG']) app.logger.info('Request successfully executed, sending output') return output, 200 except Exception as err: # Log the exception? Send it back to FE? emptyOut = {} app.logger.error(traceback.format_exc()) print(traceback.format_exc()) return emptyOut, 500 # fino a qui ################################################################# # chiama funzione per contesti multipli @app.route('/get_context', methods=['POST']) def simpleContext(): app.logger.info('Request successfully received by the Get Context API') try: queryDTO = request.get_json() queryList = queryDTO['queryList'] listResults = queryDTO['listResults'] output = handleGetContext(queryList, listResults, app.config['DATA_CONFIG']) app.logger.info('Request successfully executed, sending output') return output, 200 except Exception as err: # Log the exception? Send it back to FE? emptyOut = {} app.logger.error(traceback.format_exc()) print(traceback.format_exc()) return emptyOut, 500 # chiama funzione per contesti singoli @app.route('/get_single_context', methods=['POST']) def singleContext(): app.logger.info('Request successfully received by the Get Context API') try: queryGSC = request.get_json() elem = queryGSC['elem'] params = queryGSC['params'] output = handleSingleContext(elem, params, app.config['DATA_CONFIG']) app.logger.info('Request successfully executed, sending output') return output, 200 except Exception as err: # Log the exception? Send it back to FE? emptyOut = {} app.logger.error(traceback.format_exc()) print(traceback.format_exc()) return emptyOut, 500 if __name__ == '__main__': app.run()