from flask import Flask, request import traceback from engine.request_handlers import handleGetOccurrences, handleGetContexts, handleGetSingleContext from Config.config_loader import config app = Flask(__name__) config(app) ################################################################ # parte di codice da copiare per fare un altro endpoint in Flask ################################################################ # ENDPOINT: Basic queries @app.route('/simple_get_query', methods=['POST']) def simpleQuery(): 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 = handleGetOccurrences(queryList, cooccorrenze, app.config['DATA_CONFIG']) app.logger.info('Request successfully executed, sending output') return output, 200 except Exception as err: # Exceptions get printed and logged; nothing is sent to the FE emptyOut = {} app.logger.error(traceback.format_exc()) print(traceback.format_exc()) return emptyOut, 500 ################################################################# # fino a qui ################################################################# # ENDPOINT: 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 = {} if len(listResults)>0: output = handleGetContexts(queryList, listResults, app.config['DATA_CONFIG']) app.logger.info('Request successfully executed, sending output') return output, 200 except Exception as err: # Exceptions get printed and logged; nothing is sent to the FE emptyOut = {} app.logger.error(traceback.format_exc()) print(traceback.format_exc()) return emptyOut, 500 ################################################################# # ENDPOINT: chiama funzione per contesti singoli @app.route('/get_single_context', methods=['POST']) def singleContext(): app.logger.info('Request successfully received by the Get Single Context API') try: queryGSC = request.get_json() elem = queryGSC['elem'] params = queryGSC['params'] output = handleGetSingleContext(elem, params, app.config['DATA_CONFIG']) app.logger.info('Request successfully executed, sending output') return output, 200 except Exception as err: # Exceptions get printed and logged; nothing is sent to the FE emptyOut = {} app.logger.error(traceback.format_exc()) print(traceback.format_exc()) return emptyOut, 500 ################################################################# # Start the app! if __name__ == '__main__': app.run()