-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
97 lines (73 loc) · 2.57 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
import json
from flask import Flask, Response, request
import helper
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World! I\'m just a basic API.'
@app.route('/item/new', methods = ['POST'])
def add_item():
#Get item from the POST body
req_data = request.get_json()
item = req_data['item']
#Add item to the list
res_data = helper.add_to_list(item)
#Return error if item not added
if res_data is None:
response = Response("{'error': 'Item not added'}", status=400 , mimetype='application/json')
return response
#Return response
response = Response(json.dumps(res_data), mimetype='application/json')
return response
@app.route('/items/all')
def get_all_items():
# Get items from the helper
res_data = helper.get_all_items()
#Return response
response = Response(json.dumps(res_data), mimetype='application/json')
return response
@app.route('/item/status', methods=['GET'])
def get_item():
#Get parameter from the URL
item_name = request.args.get('name')
# Get items from the helper
status = helper.get_item(item_name)
#Return 404 if item not found
if status is None:
response = Response(f"{'error': 'Item Not Found - {item_name}'}", status=404 , mimetype='application/json')
return response
#Return status
res_data = {
'status': status
}
response = Response(json.dumps(res_data), status=200, mimetype='application/json')
return response
@app.route('/item/update', methods = ['PUT'])
def update_status():
#Get item from the POST body
req_data = request.get_json()
item = req_data['item']
status = req_data['status']
#Update item in the list
res_data = helper.update_status(item, status)
if res_data is None:
response = Response(f"{'error': 'Error updating item - {item}, {status}'}", status=400 , mimetype='application/json')
return response
#Return response
response = Response(json.dumps(res_data), mimetype='application/json')
return response
@app.route('/item/remove', methods = ['DELETE'])
def delete_item():
#Get item from the POST body
req_data = request.get_json()
item = req_data['item']
#Delete item from the list
res_data = helper.delete_item(item)
if res_data is None:
response = Response(f"{'error': 'Error deleting item - {item}'}", status=400 , mimetype='application/json')
return response
#Return response
response = Response(json.dumps(res_data), mimetype='application/json')
return response
if __name__ == "__main__":
app.run(debug=True)