api.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from flask import Flask, jsonify, request
  2. from xmpp import cli
  3. from flask_jwt_extended import JWTManager, jwt_required, get_jwt_identity, create_access_token
  4. from datetime import timedelta
  5. from config import Config
  6. from MyQuery import MyQuery
  7. #from OpenSSL import SSL
  8. #context = SSL.Context(SSL.PROTOCOL_TLSv1_2)
  9. #context.use_privatekey_file('server.key')
  10. #context.use_certificate_file('server.crt')
  11. ### ssl_context=context in run
  12. db_query = MyQuery()
  13. app = Flask(__name__)
  14. app.config.from_object(Config)
  15. jwt = JWTManager(app)
  16. #Генерация токена
  17. def gen_token(id, minut):
  18. token = {"access_token" : create_access_token(identity=id, expires_delta=timedelta(minutes=minut))}
  19. return jsonify(token)
  20. #Авторизация пользователя
  21. @app.route('/auth', methods=['POST'])
  22. def post_auth():
  23. try:
  24. result = db_query.auth_query(request.get_json())
  25. return gen_token(result['id'], 5), 200
  26. except Exception as ex:
  27. return jsonify({"msg":str(ex)}), 500
  28. #Добавление пользователя и проверка на сходства
  29. @app.route('/adduser', methods=['POST'])
  30. def get_add():
  31. try:
  32. result = db_query.add_query(request.get_json())
  33. return jsonify(result[0]), result[1]
  34. except Exception as ex:
  35. return jsonify({"msg":str(ex)}), 500
  36. #Запрос на получение данных об одном пользователе
  37. @app.route('/user/<int:id>', methods=['GET'])
  38. def get_user(id):
  39. try:
  40. result = db_query.get_user_query(id)
  41. return jsonify(result[0]), result[1]
  42. except Exception as ex:
  43. return jsonify({"msg":str(ex)}), 500
  44. #Запрос на получение данных всех пользователей
  45. @app.route('/users', methods=['GET'])
  46. def get_show_user():
  47. try:
  48. result = db_query.get_all_user_query()
  49. return jsonify(result[0]), result[1]
  50. except Exception as ex:
  51. return jsonify({"msg":str(ex)}), 500
  52. #Удаление пользователя
  53. @app.route('/deleteuser', methods=['POST'])
  54. def delete_user():
  55. try:
  56. result = db_query.delete_user_query(request.get_json())
  57. return jsonify(result[0]), result[1]
  58. except Exception as ex:
  59. return jsonify({"msg":str(ex)}), 500
  60. #Обновление данных пользователя
  61. @app.route('/updateuser', methods=['POST'])
  62. @jwt_required()
  63. def test_api():
  64. try:
  65. result = db_query.update_user_query(request.get_json(), get_jwt_identity())
  66. return jsonify(result[0]), result[1]
  67. except Exception as ex:
  68. return jsonify({"msg":str(ex)}), 500
  69. #Отправка сообщения через XMPP
  70. @app.route('/testmessage', methods=['POST'])
  71. def send_mess():
  72. try:
  73. request_data = request.get_json()
  74. for to in request_data['to']:
  75. cli.send_message(request_data['from'], request_data['password'], to, request_data['message'])
  76. return jsonify({"msg":"Succes send!"}), 200
  77. except:
  78. return jsonify({"msg":"Server error!"}), 500
  79. if __name__ == '__main__':
  80. #from waitress import serve
  81. #serve(app, host='127.0.0.2', port='8080')
  82. app.run(host='127.0.0.2', port='8080', debug=True, load_dotenv=True )