app.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from flask import Flask, request
  2. import traceback
  3. from engine.handle_request import handleOccGetQuery, handleGetContext, handleSingleContext
  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. @app.route('/simple_get_query', methods=['POST'])
  10. def simpleQuery():
  11. # # This is -- or WAS -- a stupid (and hopefully TEMPORARY) way of handling the JSON data, but it works;
  12. # # trying to send application/json data causes Flask to complain and I can't understand why!
  13. # queryDTO = json.loads( request.form['stringifiedDTO'] )
  14. # # UP TO HERE
  15. app.logger.info('Request successfully received by the Simple Get Query API')
  16. try:
  17. queryDTO = request.get_json() # new (and correct) way!
  18. queryList = queryDTO['queryList']
  19. cooccorrenze = queryDTO.get('cooccorrenze')
  20. output = handleOccGetQuery(queryList, cooccorrenze, app.config['DATA_CONFIG'])
  21. app.logger.info('Request successfully executed, sending output')
  22. return output, 200
  23. except Exception as err:
  24. # Log the exception? Send it back to FE?
  25. emptyOut = {}
  26. app.logger.error(traceback.format_exc())
  27. print(traceback.format_exc())
  28. return emptyOut, 500
  29. # fino a qui
  30. #################################################################
  31. # chiama funzione per contesti multipli
  32. @app.route('/get_context', methods=['POST'])
  33. def simpleContext():
  34. app.logger.info('Request successfully received by the Get Context API')
  35. try:
  36. queryDTO = request.get_json()
  37. queryList = queryDTO['queryList']
  38. listResults = queryDTO['listResults']
  39. output = handleGetContext(queryList, listResults, app.config['DATA_CONFIG'])
  40. app.logger.info('Request successfully executed, sending output')
  41. return output, 200
  42. except Exception as err:
  43. # Log the exception? Send it back to FE?
  44. emptyOut = {}
  45. app.logger.error(traceback.format_exc())
  46. print(traceback.format_exc())
  47. return emptyOut, 500
  48. if __name__ == '__main__':
  49. app.run()
  50. # 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 Context API')
  54. try:
  55. queryGSC = request.get_json()
  56. elem = queryGSC['elem']
  57. params = queryGSC['params']
  58. output = handleSingleContext(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. # Log the exception? Send it back to FE?
  63. emptyOut = {}
  64. app.logger.error(traceback.format_exc())
  65. print(traceback.format_exc())
  66. return emptyOut, 500
  67. if __name__ == '__main__':
  68. app.run()