-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
76 lines (70 loc) · 3.39 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from flask import Flask, request, Response
from uuid import uuid4
import json
app = Flask(__name__)
# Our storage for this example will simply be an in memory dictionary
storage = {
'students': {}
}
@app.route("/")
def index():
results = []
results.append('<html><head></head><body>')
results.append('<p>HTTP Methods demo</p>')
results.append('<ul>')
results.append('<li>Get all students GET /students/</li>')
results.append('<li>Create a student POST /students/</li>')
results.append('<li>Show a single student GET /students/<id></li>')
results.append('<li>Update a single student PATCH /students/<id></li>')
results.append('<li>Delete a single student DELETE /students/<id></li>')
results.append('</body></html>')
return ''.join(results)
@app.route("/students/", methods=['GET', 'POST'])
def all_students():
if request.method == 'POST':
body = request.get_json(silent=True)
if body is not None:
# A simple way to have unique ids is to generate a v4 uuid
id = uuid4()
body['id'] = str(id)
storage['students'][str(id)] = body
# Return the resource that was just created
return Response(json.dumps(body, indent=4), status=201, mimetype='application/json')
else:
return Response(json.dumps({'status': 'bad request'}, indent=4), status=400, mimetype='application/json')
elif request.method == 'GET':
all_students = []
# Add all the items in the students dictionary to a list
for k,v in storage['students'].items():
all_students.append(v)
return Response(json.dumps(all_students, indent=4), status=200, mimetype='application/json')
@app.route("/students/<id>", methods=['GET', 'PUT', 'DELETE'])
def one_student(id):
if request.method == 'GET':
if id in storage['students']:
return Response(json.dumps(storage['students'][id], indent=4), status=200, mimetype='application/json')
else:
return Response(json.dumps({'status': 'not found'}, indent=4), status=404, mimetype='application/json')
elif request.method == 'PATCH':
if id in storage['students']:
body = request.get_json(silent=True)
if body is not None:
# Since this is just an an example we don't really care what is
# in the request body. A real application might evaluate the
# body against a series of business rules to make sure the
# resource has a valid state before saving it
body['id'] = id
storage['students'][id] = body
return Response(json.dumps(body, indent=4), status=200, mimetype='application/json')
else:
return Response(json.dumps({'status': 'bad request'}, indent=4), status=400, mimetype='application/json')
else:
return Response(json.dumps({'status': 'not found'}, indent=4), status=404, mimetype='application/json')
elif request.method == 'DELETE':
if id in storage['students']:
del storage['students'][id]
# The typical response for a delete is a 204 (Ro content) with
# an empty response body
return Response(None, status=204, mimetype='application/json')
else:
return Response(json.dumps({'status': 'not found'}, indent=4), status=404, mimetype='application/json')