Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import uuid
- from flask import Flask, jsonify, abort, make_response, request
- app = Flask(__name__)
- def gen_uuid(length=32):
- u = uuid.uuid4()
- return u.hex[:length]
- tests = [
- {
- 'guid': gen_uuid(),
- 'description': u'load test 1',
- 'name': 'test1',
- 'state': 'Queued'
- },
- {
- 'guid': gen_uuid(),
- 'description': u'load test 2',
- 'name': 'test2',
- 'running': 'Queued'
- }
- ]
- @app.errorhandler(404)
- def not_found(error):
- return make_response(jsonify({'error': 'Not found'}), 404)
- @app.route('/todo/api/v1.0/tasks', methods=['GET'])
- def get_tests():
- return jsonify({'tests': tests})
- @app.route('/todo/api/v1.0/tasks/<str:guid>', methods=['GET'])
- def get_test(guid):
- matching_tests = [test for test in tests if test['guid'] == guid]
- if len(matching_tests) == 0:
- abort(404)
- return jsonify({'test': matching_tests[0]})
- @app.route('/todo/api/v1.0/tests', methods=['POST'])
- def queue_test():
- if not request.json or not 'name' in request.json:
- abort(400)
- test = {
- 'id': gen_uuid(),
- 'name': request.json['name'],
- 'description': request.json.get('description', ""),
- 'state': 'Queued'
- }
- tests.append(test)
- return jsonify({'test': test}), 201
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement