Python REST API Tutorial - Building a Flask REST API
# Building a REST API with Flask: A Comprehensive Guide
## Introduction
In this guide, we will walk through the process of creating a REST API using Flask. The tutorial covers essential topics such as handling HTTP methods (GET, PUT, DELETE), integrating with a database using SQLAlchemy, and ensuring proper error handling and validation.
## Setting Up the Basic Structure
We start by setting up the basic structure of our Flask application. This involves importing necessary modules and initializing key components such as the API and database connections.
```python
from flask import Flask, jsonify, abort, request
from flask_restful import Api, Resource, reqparse
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
api = Api(app)
DB = SQLAlchemy(app)
# Configuration for the database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Initialize the database
DB.init_app(app)
with app.app_context():
DB.create_all()
```
## Handling HTTP Methods (GET, PUT, DELETE)
We define a `Video` model to store video data and create resource classes for each HTTP method.
### Video Model
```python
class Video(DB.Model):
ID = DB.Column(DB.Integer, primary_key=True)
name = DB.Column(DB.String(100), nullable=False, unique=True)
views = DB.Column(DB.Integer, nullable=False)
likes = DB.Column(DB.Integer, nullable=False)
def __repr__(self):
return f"Video('{self.name}', {self.views}, {self.likes})"
```
### Resource Classes
```python
class VideoResource(Resource):
def get(self, video_id):
result = Video.query.get(video_id)
if not result:
abort(404, message="Video not found with ID {}".format(video_id))
marshal_with(resource_fields)(result)
return jsonify(result)
@marshal_with(resource_fields)
def put(self, video_id):
args = video_put_args.parse_args()
check_video = Video.query.filter_by(ID=video_id).first()
if check_video:
abort(409, message="Video ID already exists")
new_video = Video(name=args.name, views=args.views, likes=args.likes)
DB.session.add(new_video)
DB.session.commit()
return new_video
def delete(self, video_id):
result = Video.query.get(video_id)
if not result:
abort(404, message="Video not found with ID {}".format(video_id))
DB.session.delete(result)
DB.session.commit()
return jsonify({"message": "Video deleted successfully"})
api.add_resource(VideoResource, "/video/
```
## Database Integration
We use SQLAlchemy to interact with a SQLite database. This ensures our data is stored persistently.
### Key Steps:
1. **Database Initialization**: Create tables when the application starts.
2. **Querying Data**: Retrieve, update, and delete data using SQLAlchemy ORM methods.
3. **Error Handling**: Handle cases where data might not exist or already be present.
## Error Handling and Validation
We use request parsing to validate inputs and handle errors gracefully.
### Request Parsing Example:
```python
video_put_args = reqparse.RequestParser()
video_put_args.add_argument(
"name",
type=str,
help="Name of the video is required",
required=True
)
video_put_args.add_argument(
"views",
type=int,
help="Views of the video is required",
required=True
)
video_put_args.add_argument(
"likes",
type=int,
help="Likes of the video is required",
required=True
)
```
## Testing the API
We test our API using various HTTP methods to ensure everything works as expected.
### Example Test Commands:
```bash
# Create a new video
curl -X PUT -d '{"name":"How to Code", "views":100, "likes":50}' http://localhost:5000/video/1
# Retrieve a video
curl http://localhost:5000/video/1
# Update a video
curl -X PATCH -d '{"likes": 60}' http://localhost:5000/video/1
# Delete a video
curl -X DELETE http://localhost:5000/video/1
```
## Conclusion
By following this guide, you have created a functional REST API using Flask that can handle CRUD operations with database integration. This serves as a solid foundation for more complex applications and APIs.