web.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python3
  2. from flask import Flask,abort,jsonify
  3. from db import db
  4. app = Flask(__name__)
  5. d = db()
  6. BASEPATH="/pymnts"
  7. @app.route(BASEPATH+'/')
  8. def index():
  9. return "Hello, World!"
  10. @app.route(BASEPATH+'/articles/', methods=['GET'])
  11. @app.route(BASEPATH+'/articles', methods=['GET'])
  12. def get_articles():
  13. return jsonify({'articles': d.articles(limit=100)}) #limit
  14. @app.route(BASEPATH+'/article/<string:art_id>', methods=['GET'])
  15. def get_article(art_id):
  16. art=d.articles(id=art_id)
  17. if art is None:
  18. abort(401)
  19. return jsonify({'articles': art})
  20. @app.route(BASEPATH+'/sources/', methods=['GET'])
  21. @app.route(BASEPATH+'/sources', methods=['GET'])
  22. def get_sources():
  23. return jsonify({'sources': d.sources()})
  24. @app.route(BASEPATH+'/sources/<string:source_id>', methods=['GET'])
  25. def get_source(source_id):
  26. source=d.sources(source_id)
  27. if source is None:
  28. abort(401)
  29. return jsonify({'source': source})
  30. @app.route(BASEPATH+'/add_source', methods=['POST'])
  31. def create_task():
  32. if not request.json or not 'title' in request.json:
  33. abort(400)
  34. task = {
  35. 'id': tasks[-1]['id'] + 1,
  36. 'title': request.json['title'],
  37. 'description': request.json.get('description', ""),
  38. 'done': False
  39. }
  40. tasks.append(task)
  41. return jsonify({'task': task}), 201
  42. if __name__ == '__main__':
  43. app.run(debug=True)