Advertisement
sghimire

Untitled

Jan 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import uuid
  3. from flask import Flask, jsonify, abort, make_response, request
  4. app = Flask(__name__)
  5.  
  6.  
  7. def gen_uuid(length=32):
  8. u = uuid.uuid4()
  9. return u.hex[:length]
  10.  
  11. tests = [
  12. {
  13. 'guid': gen_uuid(),
  14. 'description': u'load test 1',
  15. 'name': 'test1',
  16. 'state': 'Queued'
  17. },
  18. {
  19. 'guid': gen_uuid(),
  20. 'description': u'load test 2',
  21. 'name': 'test2',
  22. 'running': 'Queued'
  23. }
  24. ]
  25.  
  26.  
  27. @app.errorhandler(404)
  28. def not_found(error):
  29. return make_response(jsonify({'error': 'Not found'}), 404)
  30.  
  31. @app.route('/todo/api/v1.0/tasks', methods=['GET'])
  32. def get_tests():
  33. return jsonify({'tests': tests})
  34.  
  35. @app.route('/todo/api/v1.0/tasks/<str:guid>', methods=['GET'])
  36. def get_test(guid):
  37. matching_tests = [test for test in tests if test['guid'] == guid]
  38. if len(matching_tests) == 0:
  39. abort(404)
  40. return jsonify({'test': matching_tests[0]})
  41.  
  42. @app.route('/todo/api/v1.0/tests', methods=['POST'])
  43. def queue_test():
  44. if not request.json or not 'name' in request.json:
  45. abort(400)
  46. test = {
  47. 'id': gen_uuid(),
  48. 'name': request.json['name'],
  49. 'description': request.json.get('description', ""),
  50. 'state': 'Queued'
  51. }
  52. tests.append(test)
  53. return jsonify({'test': test}), 201
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement