This project is a simple backend API for managing blog posts built using Python’s Flask framework and SQLAlchemy. It provides CRUD (Create, Read, Update, Delete) operations for blog posts and can serve as a starting point for more advanced blog or content management systems.
The Blog API is a RESTful service that allows clients to manage blog posts. It supports the following operations:
The API is built using Flask for the web framework and SQLAlchemy for ORM (Object Relational Mapping) with a SQLite database for data persistence.
Model Definition:
The Post
model defines the structure of a blog post, including fields for title, content, and timestamps for creation and updates.
to_dict()
method before being sent as JSON in the response.pip
for installing Python packages.Clone the Repository
git clone https://github.com/your-username/blog-api.git
cd blog-api
Create a Virtual Environment (Optional but Recommended)
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
Install Dependencies
pip install flask flask_sqlalchemy
Run the Application
The application will automatically create the SQLite database and required tables on the first run.
python app.py
The API will now be accessible at http://localhost:5000.
GET /posts
Response:
[
{
"id": 1,
"title": "My First Post",
"content": "This is the content of my first post.",
"created_at": "2025-03-29T12:34:56.789123",
"updated_at": "2025-03-29T12:34:56.789123"
},
...
]
GET /posts/<post_id>
GET /posts/1
Response:
{
"id": 1,
"title": "My First Post",
"content": "This is the content of my first post.",
"created_at": "2025-03-29T12:34:56.789123",
"updated_at": "2025-03-29T12:34:56.789123"
}
POST /posts
Request Body:
{
"title": "My New Post",
"content": "Content for my new post."
}
Response:
Returns the created post with a unique ID and timestamps.
{
"id": 2,
"title": "My New Post",
"content": "Content for my new post.",
"created_at": "2025-03-29T13:00:00.000000",
"updated_at": "2025-03-29T13:00:00.000000"
}
PUT /posts/<post_id>
Request Body:
{
"title": "Updated Title",
"content": "Updated content."
}
Response:
Returns the updated post.
{
"id": 1,
"title": "Updated Title",
"content": "Updated content.",
"created_at": "2025-03-29T12:34:56.789123",
"updated_at": "2025-03-29T14:00:00.000000"
}
DELETE /posts/<post_id>
Response:
{
"message": "Post deleted successfully."
}
unittest
or pytest
modules.pytest
Install pytest:
pip install pytest
Create a test file (e.g., test_app.py
) and write tests:
import json
from app import app, db, Post
def test_get_posts():
with app.test_client() as client:
response = client.get('/posts')
assert response.status_code == 200
def test_create_post():
with app.test_client() as client:
data = {
"title": "Test Post",
"content": "This is a test."
}
response = client.post('/posts', json=data)
assert response.status_code == 201
json_data = json.loads(response.data)
assert json_data['title'] == "Test Post"
Run the tests:
pytest
Create a Dockerfile
in the project root:
FROM python:3.9-slim
WORKDIR /app
# Copy requirements file and install dependencies
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
# Copy the rest of the application code
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]
Build and run the Docker container:
docker build -t blog-api .
docker run -d -p 5000:5000 blog-api
GET /posts
endpoint.Application Not Starting:
Ensure that all dependencies are installed and the virtual environment is activated.
Database Issues:
Verify that the SQLite file is accessible or update the SQLALCHEMY_DATABASE_URI
for other database systems.
API Returns 404:
Make sure you are using the correct URL and that the server is running on the expected port.
Contributions are welcome! Please follow these steps:
git checkout -b feature/my-feature
).git push origin feature/my-feature
).This project is licensed under the MIT License. See the LICENSE file for details.
Enjoy using the Blog API, and feel free to extend it further to suit your needs!