123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 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()
|