-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
49 lines (36 loc) · 1.33 KB
/
api.py
File metadata and controls
49 lines (36 loc) · 1.33 KB
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
import flask
import json
import traceback
# We're going to mock this one since the save package doesn't behave as Codility specifies
#from save import save
app = flask.Flask(__name__)
def save(json_object):
return json_object
@app.route("/users", methods=["POST"])
def users():
# Verify that the payload is JSON
if flask.request.is_json:
try:
json_data = flask.request.get_json()
except:
return "Payload must be JSON", 400
# Verify that the name and age attributes exist in the JSON payload
if 'name' not in json_data or 'age' not in json_data:
return "Payload must include the 'name' and 'age' attributes", 400
name = json_data['name']
age = json_data['age']
# Verify that name isn't longer than 32 characters
if len(name) > 32:
return "Name cannot be longer than 32 characters", 400
# Verify that age is a number (int)
if not isinstance(age, int):
return "Age must be a number", 400
# Verify that age is over 16
if age < 16:
return "Age cannot be less than 16", 400
return_object = save(json_data)
return flask.jsonify(return_object), 201
else:
return "Payload must be JSON", 400
if __name__ == "__main__":
app.run(host='0.0.0.0')