Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ Rest API core that manages the creation of the rest calls """
- from flask import Flask
- from flask import jsonify
- from flask import request
- from flask_pymongo import PyMongo
- APP = Flask(__name__)
- APP.config['DEBUG'] = True
- APP.config['MONGO_URI'] = "mongodb://myusername:mypassword@myclustername:27017/TestDB?authSource=admin&ssl=true"
- MONGO = PyMongo(APP)
- @APP.route('/sessions/create', methods=['PUT'])
- def create_session():
- """Creates a new session and stores it in the database"""
- print("creation call")
- test_db = MONGO.db.TestDB
- test_db.insert_one(request.json)
- return jsonify(True)
- @APP.route('/sessions/update', methods=['PUT'])
- def update_session():
- """Updates a running session and its document"""
- print("update call")
- test_db = MONGO.db.TestDB
- test_db.update( { "SessionParameters.guid": request.json["SessionParameters"]["guid"]}, request.json, upsert = True )
- return jsonify(True)
- @APP.route('/sessions/remove', methods=['PUT'])
- def remove_session():
- """Removes a finished session and its document from the db"""
- print("removal call")
- test_db = MONGO.db.TestDB
- test_db.remove( { "SessionParameters.guid": request.json["SessionParameters"]["guid"] }, { "justOne": True })
- return jsonify(True)
- @APP.route('/sessions/', methods=['GET'])
- def get_all_sessions():
- """Returns all currently running sessions"""
- test_db = MONGO.db.TestDB
- cursor = test_db.find( {}, { '_id': False }) # extract all documents and remove the "_id" field
- result = list()
- for doc in cursor:
- result.append(doc)
- return str(result)
- if __name__ == '__main__':
- APP.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement