Advertisement
Guest User

Untitled

a guest
Feb 5th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.70 KB | None | 0 0
  1. """ Rest API core that manages the creation of the rest calls """
  2.  
  3. from flask import Flask
  4. from flask import jsonify
  5. from flask import request
  6. from flask_pymongo import PyMongo
  7.  
  8. APP = Flask(__name__)
  9.  
  10. APP.config['DEBUG'] = True
  11. APP.config['MONGO_URI'] = "mongodb://myusername:mypassword@myclustername:27017/TestDB?authSource=admin&ssl=true"
  12.  
  13. MONGO = PyMongo(APP)
  14.  
  15. @APP.route('/sessions/create', methods=['PUT'])
  16. def create_session():
  17.     """Creates a new session and stores it in the database"""
  18.  
  19.     print("creation call")
  20.     test_db = MONGO.db.TestDB
  21.     test_db.insert_one(request.json)
  22.  
  23.     return jsonify(True)
  24.  
  25.  
  26. @APP.route('/sessions/update', methods=['PUT'])
  27. def update_session():
  28.     """Updates a running session and its document"""
  29.  
  30.     print("update call")
  31.     test_db = MONGO.db.TestDB
  32.     test_db.update( { "SessionParameters.guid": request.json["SessionParameters"]["guid"]}, request.json, upsert = True )
  33.  
  34.     return jsonify(True)
  35.  
  36.  
  37. @APP.route('/sessions/remove', methods=['PUT'])
  38. def remove_session():
  39.     """Removes a finished session and its document from the db"""
  40.    
  41.     print("removal call")
  42.     test_db = MONGO.db.TestDB
  43.     test_db.remove( { "SessionParameters.guid": request.json["SessionParameters"]["guid"] }, { "justOne": True })
  44.  
  45.     return jsonify(True)
  46.  
  47. @APP.route('/sessions/', methods=['GET'])
  48. def get_all_sessions():
  49.     """Returns all currently running sessions"""
  50.  
  51.     test_db = MONGO.db.TestDB
  52.     cursor = test_db.find( {}, { '_id': False }) # extract all documents and remove the "_id" field
  53.  
  54.     result = list()
  55.     for doc in cursor:
  56.         result.append(doc)
  57.  
  58.     return str(result)
  59.  
  60.  
  61. if __name__ == '__main__':
  62.     APP.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement