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