app.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from flask import Flask, request
  2. import traceback
  3. from engine.request_handlers import handleGetOccurrences, handleGetContexts, handleGetSingleContext
  4. from Config.config_loader import config
  5. app = Flask(__name__)
  6. config(app)
  7. ################################################################
  8. # parte di codice da copiare per fare un altro endpoint in Flask
  9. ################################################################
  10. # ENDPOINT: Basic queries
  11. @app.route('/simple_get_query', methods=['POST'])
  12. def simpleQuery():
  13. app.logger.info('Request successfully received by the Simple Get Query API')
  14. try:
  15. queryDTO = request.get_json() # new (and correct) way!
  16. queryList = queryDTO['queryList']
  17. cooccorrenze = queryDTO.get('cooccorrenze')
  18. output = handleGetOccurrences(queryList, cooccorrenze, app.config['DATA_CONFIG'])
  19. app.logger.info('Request successfully executed, sending output')
  20. return output, 200
  21. except Exception as err:
  22. # Exceptions get printed and logged; nothing is sent to the FE
  23. emptyOut = {}
  24. app.logger.error(traceback.format_exc())
  25. print(traceback.format_exc())
  26. return emptyOut, 500
  27. #################################################################
  28. # fino a qui
  29. #################################################################
  30. # ENDPOINT: chiama funzione per contesti multipli
  31. @app.route('/get_context', methods=['POST'])
  32. def simpleContext():
  33. app.logger.info('Request successfully received by the Get Context API')
  34. try:
  35. queryDTO = request.get_json()
  36. queryList = queryDTO['queryList']
  37. listResults = queryDTO['listResults']
  38. output = {}
  39. if len(listResults)>0:
  40. output = handleGetContexts(queryList, listResults, app.config['DATA_CONFIG'])
  41. app.logger.info('Request successfully executed, sending output')
  42. return output, 200
  43. except Exception as err:
  44. # Exceptions get printed and logged; nothing is sent to the FE
  45. emptyOut = {}
  46. app.logger.error(traceback.format_exc())
  47. print(traceback.format_exc())
  48. return emptyOut, 500
  49. #################################################################
  50. # ENDPOINT: chiama funzione per contesti singoli
  51. @app.route('/get_single_context', methods=['POST'])
  52. def singleContext():
  53. app.logger.info('Request successfully received by the Get Single Context API')
  54. try:
  55. queryGSC = request.get_json()
  56. elem = queryGSC['elem']
  57. params = queryGSC['params']
  58. output = handleGetSingleContext(elem, params, app.config['DATA_CONFIG'])
  59. app.logger.info('Request successfully executed, sending output')
  60. return output, 200
  61. except Exception as err:
  62. # Exceptions get printed and logged; nothing is sent to the FE
  63. emptyOut = {}
  64. app.logger.error(traceback.format_exc())
  65. print(traceback.format_exc())
  66. return emptyOut, 500
  67. #################################################################
  68. # Start the app!
  69. if __name__ == '__main__':
  70. app.run()