-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
45 lines (35 loc) · 1.01 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
import json
from flask import Flask, Response, abort, request
app = Flask(__name__)
data = {
'123': 100,
'321': 1,
'50': 98
}
@app.route('/update', methods=['POST'])
def item_update():
if not request.json:
abort(400)
pk = request.json['pk']
score = request.json['score']
data[str(pk)] = int(score)
return Response("pk " + str(pk) + " updated with score " + str(score), status=200)
@app.route('/get/<int:pk>', methods=['GET'])
def item_score(pk):
if str(pk) in data.keys():
score = data[str(pk)]
else:
return Response('pk ' + str(pk) + ' not found', status=400)
if score is None:
abort(404)
content = json.dumps({ 'pk': str(pk), 'score': str(score) })
return Response(content, status=200, mimetype='application/json')
@app.route('/', methods=['GET'])
def sample():
return 'Welcome to this sample app :)'
@app.errorhandler(404)
def not_found(e):
return '', 404
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0')