Skip to content

Commit f521822

Browse files
committed
first commit
0 parents  commit f521822

File tree

6 files changed

+156
-0
lines changed

6 files changed

+156
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.venv

Diff for: README.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Python REST API Tutorial
2+
## "How to Build a Flask REST API for Beginners"
3+
---
4+
### Author Links
5+
6+
👋 Hello, I'm Dave Gray.
7+
8+
📚 [My Courses](https://courses.davegray.codes/)
9+
10+
[Check out my YouTube Channel with hundreds of tutorials](https://www.youtube.com/DaveGrayTeachesCode).
11+
12+
🚩 [Subscribe to my channel](https://bit.ly/3nGHmNn)
13+
14+
💖 [Support My Content](https://patreon.com/davegray)
15+
16+
🚀 Follow Me:
17+
18+
- [Twitter](https://twitter.com/yesdavidgray)
19+
- [LinkedIn](https://www.linkedin.com/in/davidagray/)
20+
- [Blog](https://davegray.codes)
21+
22+
---
23+
24+
### Description
25+
26+
📺 [YouTube Video](https://youtu.be/z3YMz-Gocmw) for this repository.
27+
28+
---
29+
30+
### ⚙ Usage
31+
32+
- Watch the video linked above for step-by-step instructions
33+
34+
---
35+
36+
### 🎓 Academic Honesty
37+
38+
**DO NOT COPY FOR AN ASSIGNMENT** - Avoid plagiarism and adhere to the spirit of this [Academic Honesty Policy](https://www.freecodecamp.org/news/academic-honesty-policy/).
39+
40+
---
41+
42+
### 📺 Suggest Prerequisite Video:
43+
- 🔗 [Python Full Course for Beginners](https://youtu.be/H2EJuAcrZYU)
44+
45+
### 📚 Tutorial References
46+
47+
- 🔗 [MDN REST](https://developer.mozilla.org/en-US/docs/Glossary/REST)
48+
- 🔗 [MDN API](https://developer.mozilla.org/en-US/docs/Glossary/API)
49+
- 🔗 [Visual Studio Code](https://code.visualstudio.com/)
50+
- 🔗 [Python](https://www.python.org/)
51+
- 🔗 [git & git bash](https://www.git-scm.com/downloads)
52+
- 🔗 [Flask](https://flask.palletsprojects.com/en/3.0.x/)
53+
- 🔗 [Flask Restful](https://flask-restful.readthedocs.io/en/latest/)
54+
- 🔗 [Flask SQLAlchemy](https://flask-sqlalchemy.palletsprojects.com/en/3.1.x/)

Diff for: api.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from flask import Flask
2+
from flask_sqlalchemy import SQLAlchemy
3+
from flask_restful import Resource, Api, reqparse, fields, marshal_with, abort
4+
5+
app = Flask(__name__)
6+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
7+
db = SQLAlchemy(app)
8+
api = Api(app)
9+
10+
class UserModel(db.Model):
11+
id = db.Column(db.Integer, primary_key=True)
12+
name = db.Column(db.String(80), unique=True, nullable=False)
13+
email = db.Column(db.String(80), unique=True, nullable=False)
14+
15+
def __repr__(self):
16+
return f"User(name = {self.name}, email = {self.email})"
17+
18+
user_args = reqparse.RequestParser()
19+
user_args.add_argument('name', type=str, required=True, help="Name cannot be blank")
20+
user_args.add_argument('email', type=str, required=True, help="Email cannot be blank")
21+
22+
userFields = {
23+
'id':fields.Integer,
24+
'name':fields.String,
25+
'email':fields.String,
26+
}
27+
28+
class Users(Resource):
29+
@marshal_with(userFields)
30+
def get(self):
31+
users = UserModel.query.all()
32+
return users
33+
34+
@marshal_with(userFields)
35+
def post(self):
36+
args = user_args.parse_args()
37+
user = UserModel(name=args["name"], email=args["email"])
38+
db.session.add(user)
39+
db.session.commit()
40+
users = UserModel.query.all()
41+
return users, 201
42+
43+
class User(Resource):
44+
@marshal_with(userFields)
45+
def get(self, id):
46+
user = UserModel.query.filter_by(id=id).first()
47+
if not user:
48+
abort(404, "User not found")
49+
return user
50+
51+
@marshal_with(userFields)
52+
def patch(self, id):
53+
args = user_args.parse_args()
54+
user = UserModel.query.filter_by(id=id).first()
55+
if not user:
56+
abort(404, "User not found")
57+
user.name = args["name"]
58+
user.email = args["email"]
59+
db.session.commit()
60+
return user
61+
62+
@marshal_with(userFields)
63+
def delete(self, id):
64+
user = UserModel.query.filter_by(id=id).first()
65+
if not user:
66+
abort(404, "User not found")
67+
db.session.delete(user)
68+
db.session.commit()
69+
users = UserModel.query.all()
70+
return users
71+
72+
73+
api.add_resource(Users, '/api/users/')
74+
api.add_resource(User, '/api/users/<int:id>')
75+
76+
@app.route('/')
77+
def home():
78+
return '<h1>Flask REST API</h1>'
79+
80+
if __name__ == '__main__':
81+
app.run(debug=True)

Diff for: create_db.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from api import app, db
2+
3+
with app.app_context():
4+
db.create_all()

Diff for: instance/database.db

16 KB
Binary file not shown.

Diff for: requirements.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
aniso8601==9.0.1
2+
blinker==1.8.2
3+
click==8.1.7
4+
colorama==0.4.6
5+
Flask==3.0.3
6+
Flask-RESTful==0.3.10
7+
Flask-SQLAlchemy==3.1.1
8+
greenlet==3.0.3
9+
itsdangerous==2.2.0
10+
Jinja2==3.1.4
11+
MarkupSafe==2.1.5
12+
pytz==2024.1
13+
six==1.16.0
14+
SQLAlchemy==2.0.30
15+
typing_extensions==4.12.2
16+
Werkzeug==3.0.3

0 commit comments

Comments
 (0)