Skip to content

Commit 3533b25

Browse files
committed
✨ (API Endpoints) Improve API endpoints in first REST API project
This is so the endpoints use `id` instead of `name`, and also add endpoints so that they match the final project. That way there should be less to change throughout the course, as we build on the project.
1 parent c83f2a3 commit 3533b25

File tree

2 files changed

+86
-62
lines changed
  • project

2 files changed

+86
-62
lines changed

project/01-first-rest-api/app.py

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,57 @@
1+
import uuid
12
from flask import Flask, request
23

34
app = Flask(__name__)
45

5-
stores = [{"name": "My Store", "items": [{"name": "my item", "price": 15.99}]}]
6+
stores = {}
7+
items = {}
68

79

8-
@app.post("/store")
9-
def create_store():
10+
@app.get("/item/<string:id>")
11+
def get_item(id):
12+
try:
13+
return items[id]
14+
except KeyError:
15+
return {"message": "Item not found"}, 404
16+
17+
18+
@app.post("/items")
19+
def create_item():
1020
request_data = request.get_json()
11-
new_store = {"name": request_data["name"], "items": []}
12-
stores.append(new_store)
13-
return new_store, 201
21+
new_item_id = uuid.uuid4().hex
22+
new_item = {
23+
"name": request_data["name"],
24+
"price": request_data["price"],
25+
"store_id": request_data["store_id"],
26+
}
27+
items[new_item_id] = new_item
28+
return new_item
1429

1530

16-
@app.get("/store/<string:name>")
17-
def get_store(name):
18-
for store in stores:
19-
if store["name"] == name:
20-
return store
21-
return {"message": "Store not found"}, 404
31+
@app.get("/items")
32+
def get_all_items():
33+
return {"items": list(items.value())}
2234

2335

24-
@app.get("/store")
25-
def get_stores():
26-
return {"stores": stores}
36+
@app.get("/stores/<string:id>")
37+
def get_store(id):
38+
try:
39+
# Here you might also want to add the items in this store
40+
# We'll do that later on in the course
41+
return stores[id]
42+
except KeyError:
43+
return {"message": "Store not found"}, 404
2744

2845

29-
@app.post("/store/<string:name>/item")
30-
def create_item_in_store(name):
46+
@app.post("/stores")
47+
def create_store():
3148
request_data = request.get_json()
32-
for store in stores:
33-
if store["name"] == name:
34-
new_item = {"name": request_data["name"], "price": request_data["price"]}
35-
store["items"].append(new_item)
36-
return new_item
37-
return {"message": "Store not found"}, 404
38-
39-
40-
@app.get("/store/<string:name>/item")
41-
def get_item_in_store(name):
42-
for store in stores:
43-
if store["name"] == name:
44-
return {"items": store["items"]}
45-
return {"message": "Store not found"}, 404
49+
new_store_id = uuid.uuid4().hex
50+
new_store = {"id": new_store_id, "name": request_data["name"]}
51+
stores[new_store_id] = new_store
52+
return new_store, 201
53+
54+
55+
@app.get("/stores")
56+
def get_all_stores():
57+
return {"stores": list(stores.value())}
Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,57 @@
1+
import uuid
12
from flask import Flask, request
23

34
app = Flask(__name__)
45

5-
stores = [{"name": "My Store", "items": [{"name": "my item", "price": 15.99}]}]
6+
stores = {}
7+
items = {}
68

79

8-
@app.post("/store")
9-
def create_store():
10+
@app.get("/item/<string:id>")
11+
def get_item(id):
12+
try:
13+
return items[id]
14+
except KeyError:
15+
return {"message": "Item not found"}, 404
16+
17+
18+
@app.post("/items")
19+
def create_item():
1020
request_data = request.get_json()
11-
new_store = {"name": request_data["name"], "items": []}
12-
stores.append(new_store)
13-
return new_store, 201
21+
new_item_id = uuid.uuid4().hex
22+
new_item = {
23+
"name": request_data["name"],
24+
"price": request_data["price"],
25+
"store_id": request_data["store_id"],
26+
}
27+
items[new_item_id] = new_item
28+
return new_item
1429

1530

16-
@app.get("/store/<string:name>")
17-
def get_store(name):
18-
for store in stores:
19-
if store["name"] == name:
20-
return store
21-
return {"message": "Store not found"}, 404
31+
@app.get("/items")
32+
def get_all_items():
33+
return {"items": list(items.value())}
2234

2335

24-
@app.get("/store")
25-
def get_stores():
26-
return {"stores": stores}
36+
@app.get("/stores/<string:id>")
37+
def get_store(id):
38+
try:
39+
# Here you might also want to add the items in this store
40+
# We'll do that later on in the course
41+
return stores[id]
42+
except KeyError:
43+
return {"message": "Store not found"}, 404
2744

2845

29-
@app.post("/store/<string:name>/item")
30-
def create_item_in_store(name):
46+
@app.post("/stores")
47+
def create_store():
3148
request_data = request.get_json()
32-
for store in stores:
33-
if store["name"] == name:
34-
new_item = {"name": request_data["name"], "price": request_data["price"]}
35-
store["items"].append(new_item)
36-
return new_item
37-
return {"message": "Store not found"}, 404
38-
39-
40-
@app.get("/store/<string:name>/item")
41-
def get_item_in_store(name):
42-
for store in stores:
43-
if store["name"] == name:
44-
return {"items": store["items"]}
45-
return {"message": "Store not found"}, 404
49+
new_store_id = uuid.uuid4().hex
50+
new_store = {"id": new_store_id, "name": request_data["name"]}
51+
stores[new_store_id] = new_store
52+
return new_store, 201
53+
54+
55+
@app.get("/stores")
56+
def get_all_stores():
57+
return {"stores": list(stores.value())}

0 commit comments

Comments
 (0)