-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
73 lines (65 loc) · 2.44 KB
/
Copy pathapp.py
File metadata and controls
73 lines (65 loc) · 2.44 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from flask import Flask, render_template, jsonify, abort, request
app = Flask(__name__)
customers = [
{
'id': 1,
'firstName': 'Stacey',
'lastName': 'Smith',
'isPremiumMember': False
},
{
'id': 2,
'firstName': 'Jack',
'lastName': 'Jackson',
'isPremiumMember': True
}
]
@app.route('/api/1.0/customers', methods=['GET'])
def getCustomers():
return jsonify({'customers': customers})
@app.route('/api/1.0/customers/<int:customerId>', methods=['GET'])
def geCustomer(customerId):
customer = [customer for customer in customers if customer['id'] == customerId]
if len(customer) == 0:
return render_template("notFound.html")
#abort(404)
return jsonify({'customer': customer[0]})
@app.route('/api/1.0/customers', methods=['POST'])
def createCustomer():
if not request.json:
abort(400)
customer = {
'id': customers[-1]['id'] + 1,
'firstName': request.json.get('firstName'),
'lastName': request.json.get('lastName'),
'isPremiumMember': request.json.get('isPremiumMember')
}
customers.append(customer)
return jsonify({'customer': customer}), 201
@app.route('/api/1.0/customers/<int:customerId>', methods=['DELETE'])
def deleteCustomer(customerId):
customer = [customer for customer in customers if customer['id'] == customerId]
if len(customer) == 0:
abort(404)
customers.remove(customer[0])
return jsonify({'result': True})
@app.route('/api/1.0/customers/<int:customerId>', methods=['PUT'])
def updateCustomer(customerId):
customer = [customer for customer in customers if customer['id'] == customerId]
if len(customer) == 0:
abort(404)
if not request.json:
abort(400)
if 'firstName' in request.json and type(request.json['firstName']) != str:
abort(400)
if 'lastName' in request.json and type(request.json['lastName']) is not str:
abort(400)
if 'isPremiumMember' in request.json and type(request.json['isPremiumMember']) is not bool:
abort(400)
customer[0]['firstName'] = request.json.get('firstName', customer[0]['firstName'])
customer[0]['lastName'] = request.json.get('lastName', customer[0]['lastName'])
customer[0]['isPremiumMember'] = request.json.get('isPremiumMember', customer[0]['isPremiumMember'])
return jsonify({'customer': customer[0]})
@app.route("/")
def home():
return render_template("index.html")