Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ serveur REST web ,avec base de données
- getAllEmp
- curl -i http://localhost:5000/axiome/employe
- getEmp
- curl -i http://localhost:5000/axiome/employe/{$id}
- default
- curl -i http://localhost:5000
- updateEmp
- curl -i -H "Content-type: application/json" -X PUT -d '{"title":"esclave"}' http://localhost:5000/axiome/employe/1
- createEmp
- curl -i -H "Content-type: application/json" -X POST -d '{"id":"69","name":"Angus","title":"guitar"}' http://localhost:5000/axiome/employe
- Delete
- curl -i -X Delete http://localhost:5000/axiome/employe/1
- """
- from flask import Flask
- from flask import jsonify
- from flask import request
- app = Flask(__name__)
- base_address="axiome"
- hostname ="localhost"
- employesDB=[
- {
- 'id':'1',
- 'name':'Liberto Zaragoza',
- 'title':'pdg'
- },
- {
- 'id':'2',
- 'name':'Gilles Pignatta',
- 'title':'développeur'
- },
- {
- 'id':'3',
- 'name':'Antonio Villanueva',
- 'title':'développeur'
- },
- {
- 'id':'4',
- 'name':'Franck Clerissi',
- 'title':'Charge d affaires '
- }
- ]
- @app.route("/")
- def default():
- """page Web par défaut """
- return jsonify({'Base Web address':base_address})
- @app.route('/axiome/employe',methods=['GET'])
- def getAllEmp():
- """renvoie toute la base de données """
- return jsonify({'emps':employesDB})
- @app.route('/axiome/employe/<empId>',methods=['GET'])
- def getEmp(empId):
- """retourner un seul employé <empId> """
- usr = [ emp for emp in employesDB if (emp['id'] == empId) ]
- return jsonify({'employe':usr})
- @app.route('/axiome/employe',methods=['POST'])
- def createEmp():
- """ Créer un nouvel employé"""
- dat = {
- 'id':request.json['id'],
- 'name':request.json['name'],
- 'title':request.json['title']
- }
- employesDB.append(dat)
- return jsonify(dat)
- @app.route('/axiome/employe/<empId>',methods=['PUT'])
- def updateEmp(empId):
- """mettre à jour un employé dans la base de données """
- em = [ emp for emp in employesDB if (emp['id'] == empId) ]
- print ("updateEmp ",em)
- if 'name' in request.json :
- em[0]['name'] = request.json['name']
- if 'title' in request.json:
- em[0]['title'] = request.json['title']
- return jsonify({'emp':em[0]})
- @app.route('/axiome/employe/<empId>',methods=['DELETE'])
- def deleteEmp(empId):
- """Efface employe dans employeeDB """
- em = [ emp for emp in employesDB if (emp['id'] == empId) ]
- if len(em) == 0:#Element vide pas trouve ...
- return jsonify({'response':'Erreur employe '+empId+' n \'existe pas' })
- employesDB.remove(em[0]) #Efface id num
- return jsonify({'response':'delete employe'+empId})
- if __name__ == "__main__":
- #app.run()
- app.run( host=hostname,port=5000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement