-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
127 lines (106 loc) · 2.83 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from datetime import datetime
from flask import (
Flask,
jsonify,
request
)
def get_timestamp():
return datetime.now().strftime(("%Y-%m-%d %H:%M:%S"))
# Data to serve with our API
PEOPLE = {
"Farrell": {
"fname": "Doug",
"lname": "Farrell",
"timestamp": get_timestamp()
},
"Brockman": {
"fname": "Kent",
"lname": "Brockman",
"timestamp": get_timestamp()
},
"Easter": {
"fname": "Bunny",
"lname": "Easter",
"timestamp": get_timestamp()
}
}
# Create the application instance
app = Flask(__name__, template_folder="templates")
# Create a URL route in our application for "/"
@app.route('/api/people')
def get_people():
"""
This function responds to a GET request for /api/people
with the complete list of people
:return:
sorted list of people and 200
"""
return jsonify([PEOPLE[key] for key in sorted(PEOPLE.keys())]), 200
@app.route('/api/people/<lname>')
def get_person(lname):
"""
This function responds to a GET request for /api/people/<lname>
with the person with lname
:return:
person object (dict) and 200
error object (dict) and 404
"""
if lname in PEOPLE:
return jsonify(PEOPLE[lname]), 200
else:
error = {
"message": "That person does not exist"
}
return jsonify(error), 404
@app.route('/api/people', methods=['POST'])
def post_person():
"""
This function creates a new person in the PEOPLE dict
:return:
person object (dict) and 201
error object (dict) and 409
"""
data = request.json
if not data.get("lname") in PEOPLE:
PEOPLE[data.get("lname")] = data
return jsonify(data), 201
else:
error = {
"message": "That person already exists"
}
return jsonify(error), 409
@app.route('/api/people/<lname>', methods=['DELETE'])
def delete_person(lname):
"""
This function deletes a person in the PEOPLE dict
:return:
204
error object (dict) and 404
"""
if lname in PEOPLE:
del PEOPLE[lname]
return 204
else:
error = {
"message": "That person does not exist"
}
return jsonify(error), 404
@app.route('/api/people/<lname>', methods=['PUT'])
def put_person(lname):
"""
This function updates a person in the PEOPLE dict
:return:
person object (dict) and 201
error object (dict) and 404
"""
if lname in PEOPLE:
PEOPLE[lname] = request.json
return jsonify(request.json), 201
else:
error = {
"message": "That person does not exist"
}
return jsonify(error), 404
# If we're running in stand alone mode, run the application
if __name__ == '__main__':
app.run(debug=True)