-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
68 lines (52 loc) · 1.85 KB
/
Copy pathapi.py
File metadata and controls
68 lines (52 loc) · 1.85 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
### Put and Delete-HTTP Verbs
### Working With API's--Json
from flask import Flask, jsonify, request
app = Flask(__name__)
##Initial Data in my to do list
items = [
{"id": 1, "name": "Item 1", "description": "This is item 1"},
{"id": 2, "name": "Item 2", "description": "This is item 2"}
]
@app.route('/')
def home():
return "Welcome To The Sample To DO List App"
## Get: Retrieve all the items
@app.route('/items',methods=['GET'])
def get_items():
return jsonify(items)
## get: Retireve a specific item by Id
@app.route('/items/<int:item_id>',methods=['GET'])
def get_item(item_id):
item=next((item for item in items if item["id"]==item_id),None)
if item is None:
return jsonify({"error":"item not found"})
return jsonify(item)
## Post :create a new task- API
@app.route('/items',methods=['POST'])
def create_item():
if not request.json or not 'name' in request.json:
return jsonify({"error":"item not found"})
new_item={
"id": items[-1]["id"] + 1 if items else 1,
"name":request.json['name'],
"description":request.json["description"]
}
items.append(new_item)
return jsonify(new_item)
# Put: Update an existing item
@app.route('/items/<int:item_id>',methods=['PUT'])
def update_item(item_id):
item = next((item for item in items if item["id"] == item_id), None)
if item is None:
return jsonify({"error": "Item not found"})
item['name'] = request.json.get('name', item['name'])
item['description'] = request.json.get('description', item['description'])
return jsonify(item)
# DELETE: Delete an item
@app.route('/items/<int:item_id>', methods=['DELETE'])
def delete_item(item_id):
global items
items = [item for item in items if item["id"] != item_id]
return jsonify({"result": "Item deleted"})
if __name__ == '__main__':
app.run(debug=True)