Guest User

Untitled

a guest
Oct 22nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. from flask import Flask
  2. from flask_restful import Api
  3.  
  4. app = Flask(__name__)
  5. ...
  6. api = Api(app)
  7.  
  8. api.add_resource(ContactList, "/contacts")
  9.  
  10.  
  11. if __name__ == '__main__':
  12. from object_SQLAlchemy import db
  13. db.init_app(app)
  14. app.run(port=5000)
  15.  
  16.  
  17. class Contact(Resource):
  18. parser = reqparse.RequestParser()
  19. parser.add_argument(
  20. 'contact_no',
  21. type=str,
  22. required=True,
  23. help="This field cannot be left blank"
  24. )
  25.  
  26. @throttling.Throttle("10/m", strategy=2)
  27. def get(self, name):
  28. contact = Contacts.findbyname(name)
  29. if contact:
  30. return contact.json()
  31. return {"message": "Contact does not exist."}, 404
  32.  
  33. from ..app_alchemy import api, app
  34.  
  35.  
  36. @api.errorhandler(Exception)
  37. def handle_error(e):
  38. return {"error": str(e)}
  39.  
  40.  
  41. @app.errorhandler(500)
  42. def handle_error_app(e):
  43. return {"error": str(e.args[0])}
  44.  
  45.  
  46. @app.handle_exception(Exception)
  47. def handle_it_app(e):
  48. return {"error": str(e.args[0])}
  49.  
  50.  
  51. @api.handle_exception(Exception)
  52. def handle_it(e):
  53. return {"error": str(e.args[0])}
  54.  
  55. {"message": "Internal Server Error"}
Add Comment
Please, Sign In to add comment