app.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. # chiama funzione per contesti singoli
  49. @app.route('/get_single_context', methods=['POST'])
  50. def singleContext():
  51. app.logger.info('Request successfully received by the Get Context API')
  52. try:
  53. queryGSC = request.get_json()
  54. elem = queryGSC['elem']
  55. params = queryGSC['params']
  56. output = handleSingleContext(elem, params, app.config['DATA_CONFIG'])
  57. app.logger.info('Request successfully executed, sending output')
  58. return output, 200
  59. except Exception as err:
  60. # Log the exception? Send it back to FE?
  61. emptyOut = {}
  62. app.logger.error(traceback.format_exc())
  63. print(traceback.format_exc())
  64. return emptyOut, 500
  65. if __name__ == '__main__':
  66. app.run()