Skip to content

Commit 4a302d0

Browse files
author
Arjan
committed
Branch main to in-progress
2 parents 958177b + 507a4b8 commit 4a302d0

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from flask import Flask, jsonify, abort
2+
from db import fetch_blogs, fetch_blog, NotFoundError, NotAuthorizedError
3+
4+
app = Flask(__name__)
5+
6+
@app.route('/')
7+
def hello_world():
8+
return 'Hello, World!'
9+
10+
@app.route('/blogs')
11+
def all_blogs():
12+
return jsonify(fetch_blogs())
13+
14+
@app.route('/blogs/<id>')
15+
def get_blog(id):
16+
try:
17+
return jsonify(fetch_blog(id))
18+
except NotFoundError:
19+
abort(404, description="Resource not found")
20+
except NotAuthorizedError:
21+
abort(403, description="Access denied")

7 - dealing with errors/after-context/db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def fetch_blog(id: str):
4343
with SQLite('application.db') as cur:
4444

4545
# execute the query and fetch the data
46-
cur.execute(f"SELECT * FROM blogs where id='{id}'")
46+
cur.execute(f"SELECT * FROM blogs where id=?", [id])
4747
result = cur.fetchone()
4848

4949
# return the result or raise an error

7 - dealing with errors/after/app.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from flask import Flask, jsonify, abort
2+
from db import fetch_blogs, fetch_blog, NotFoundError, NotAuthorizedError
3+
4+
app = Flask(__name__)
5+
6+
@app.route('/')
7+
def hello_world():
8+
return 'Hello, World!'
9+
10+
@app.route('/blogs')
11+
def all_blogs():
12+
return jsonify(fetch_blogs())
13+
14+
@app.route('/blogs/<id>')
15+
def get_blog(id):
16+
try:
17+
return jsonify(fetch_blog(id))
18+
except NotFoundError:
19+
abort(404, description="Resource not found")
20+
except NotAuthorizedError:
21+
abort(403, description="Access denied")

7 - dealing with errors/after/db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def fetch_blog(id: str):
4343
cur = con.cursor()
4444

4545
# execute the query and fetch the data
46-
cur.execute(f"SELECT * FROM blogs where id='{id}'")
46+
cur.execute(f"SELECT * FROM blogs where id=?", [id])
4747
result = cur.fetchone()
4848

4949
# return the result or raise an error

7 - dealing with errors/before/app.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from flask import Flask, jsonify, abort
2+
3+
app = Flask(__name__)
4+
5+
@app.route('/')
6+
def hello_world():
7+
return 'Hello, World!'

0 commit comments

Comments
 (0)