Change naming and veriy test-scheduler function
[bottlenecks.git] / test-scheduler / server / src / rest / test_service_demo.py
1 ##############################################################################
2 # Copyright (c) 2018 HUAWEI TECHNOLOGIES CO.,LTD and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 from flask import Flask
11 from flask_cors import CORS
12 from flask import request
13 from flask import jsonify
14 import time
15 import json
16 from random import randint
17
18 app = Flask(__name__)
19 CORS(app)
20
21
22 @app.route("/greet")
23 def greet():
24     return "hello"
25
26
27 @app.route("/answer", methods=["POST"])
28 def answer():
29     app.logger.debug(request.form)
30     app.logger.debug(request.data)
31     if jsonify(request.form) != {} and 'ping' in request.form:
32         return "answer: ping is: \"" + request.form['ping'] + "\" end."
33     elif request.data != "":
34         requestDict = json.loads(request.data)
35         if 'ping' in requestDict:
36             return "answer: the ping is: \"" + requestDict['ping'] + "\" end."
37     else:
38         return "answer ping is null"
39
40
41 @app.route("/answer2", methods=["POST"])
42 def answer2():
43     return "ok"
44
45
46 @app.route("/five")
47 def sleepFiveSeconds():
48     time.sleep(5)
49     return "five: receive the request."
50
51
52 @app.route("/ten")
53 def sleepTenSeconds():
54     time.sleep(10)
55     return "ten: receive the request."
56
57
58 @app.route("/switch")
59 def switchValue():
60     value = randint(0, 10)
61     if value > 4:
62         return jsonify({'code': 200, 'result': 'A'})
63     else:
64         return jsonify({'code': 200, 'result': 'B'})
65
66
67 @app.route("/switch_2")
68 def switchValue_2():
69     value = randint(0, 10)
70     if value > 4:
71         return jsonify({'code': 200, 'result': 'C'})
72     else:
73         return jsonify({'code': 200, 'result': 'D'})
74
75
76 if __name__ == "__main__":
77     app.run(host='0.0.0.0', port=5312, debug=True)