An open API service indexing awesome lists of open source software.

https://github.com/martinp95/flask_api_chinook

A production-ready RESTful API built with Flask, SQLAlchemy, and Marshmallow, based on the classic Chinook database. It exposes endpoints to browse artists, albums with support for pagination, nested resources, summaries, and OpenAPI documentation
https://github.com/martinp95/flask_api_chinook

flask flask-restful flask-sqlalchemy marshmallow sqlalchemy sqlite3

Last synced: 2 months ago
JSON representation

A production-ready RESTful API built with Flask, SQLAlchemy, and Marshmallow, based on the classic Chinook database. It exposes endpoints to browse artists, albums with support for pagination, nested resources, summaries, and OpenAPI documentation

Awesome Lists containing this project

README

          

# 🎡 Chinook REST API

![Tests](https://github.com/martinp95/flask_api_chinook/actions/workflows/ci.yml/badge.svg)
[![codecov](https://codecov.io/gh/martinp95/flask_api_chinook/graph/badge.svg?token=ZF2FMOBITA)](https://codecov.io/gh/martinp95/flask_api_chinook)
![Python](https://img.shields.io/badge/python-3.12-blue.svg)
[![License](https://img.shields.io/github/license/martinp95/flask_api_chinook.svg)](https://github.com/martinp95/flask_api_chinook/blob/main/LICENSE)

A production-ready RESTful API built with **Flask**, **SQLAlchemy**, and **Marshmallow**, based on the classic Chinook database. It exposes endpoints to browse artists, albums, and tracks with support for pagination, nested resources, summaries, and OpenAPI documentation.

---

## πŸš€ Features

- βœ… RESTful endpoints (artists, albums, tracks)
- βœ… SQLAlchemy ORM with SQLite (Chinook schema)
- βœ… Marshmallow for schema validation and serialization
- βœ… Swagger UI (via Flasgger) available at `/apidocs/`
- βœ… Centralized error handling (404, 500, etc.)
- βœ… Clean service-oriented architecture
- βœ… Auto-formatted with `black`
- βœ… 100% unit test coverage with `pytest`
- βœ… Configurable via environment (`FLASK_CONFIG`)
- βœ… Dockerized and compatible with `Makefile`

---

## πŸ“ Project Structure

```
flask_api_chinook/
β”œβ”€β”€ app/
β”‚ β”œβ”€β”€ __init__.py # Application factory
β”‚ β”œβ”€β”€ run.py # Entrypoint for running the app
β”‚ β”œβ”€β”€ common/
β”‚ β”‚ β”œβ”€β”€ config.py # Configuration classes
β”‚ β”‚ β”œβ”€β”€ logging_config.py # Logging setup
β”‚ β”‚ └── errors.py # Centralized error handling
β”‚ β”œβ”€β”€ models/ # SQLAlchemy models
β”‚ β”‚ └── models.py
β”‚ β”œβ”€β”€ schemas/ # Marshmallow schemas
β”‚ β”‚ β”œβ”€β”€ albums_schema.py
β”‚ β”‚ β”œβ”€β”€ albums_summary_schema.py
β”‚ β”‚ β”œβ”€β”€ artists_schema.py
β”‚ β”‚ └── tracks_schema.py
β”‚ β”œβ”€β”€ services/ # Business logic layer
β”‚ β”‚ └── services.py
β”‚ └── routes/ # Flask blueprints
β”‚ β”œβ”€β”€ __init__.py
β”‚ β”œβ”€β”€ artists.py
β”‚ └── albums.py
β”œβ”€β”€ instance/
β”‚ └── chinook.db # SQLite database (preloaded)
β”œβ”€β”€ tests/ # Pytest-based tests
β”‚ β”œβ”€β”€ test_albums.py
β”‚ β”œβ”€β”€ test_artists.py
β”‚ β”œβ”€β”€ test_errors.py
β”‚ └── conftest.py
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ Makefile
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ requirements-dev.txt
β”œβ”€β”€ pytest.ini
└── README.md
```

---

## πŸ”§ Setup & Usage

### πŸ”Έ 1. Clone the repository

```bash
git clone https://github.com/martinp95/flask_api_chinook.git
cd flask_api_chinook
```

### πŸ”Έ 2. Start the application with Docker

```bash
make up
```

This will build and run the app using `docker-compose`.
When it's running:

- βœ… API Base: [http://localhost:5000/artists](http://localhost:5000/artists)
- βœ… Swagger UI: [http://localhost:5000/apidocs](http://localhost:5000/apidocs)

---

## πŸ“Œ Environment Configuration

Set the Flask configuration using the `FLASK_CONFIG` env var:

| Mode | Config Path |
|--------------|------------------------------------------|
| Development | `app.common.config.DevelopmentConfig` |
| Testing | `app.common.config.TestingConfig` |
| Production | `app.common.config.ProductionConfig` |

Default is Development if not set.

---

## πŸ“š API Endpoints (Sample)

### 🎀 GET `/artists?page=1&per_page=2`

Returns paginated list of artists:

```json
{
"artists": [
{ "ArtistId": 1, "Name": "AC/DC" },
{ "ArtistId": 2, "Name": "Accept" }
],
"page": 1,
"per_page": 2,
"total": 275
}
```

---

### πŸ’Ώ GET `/artists//albums`

Returns albums by artist:

```json
[
{ "AlbumId": 1, "Title": "For Those About To Rock We Salute You" },
{ "AlbumId": 4, "Title": "Let There Be Rock" }
]
```

---

### πŸ“€ GET `/albums?page=1&per_page=1`

Returns albums and their tracks:

```json
{
"albums": [
{
"AlbumId": 1,
"Title": "For Those About To Rock We Salute You",
"tracks": [
{ "TrackId": 1, "Name": "Track A" },
{ "TrackId": 2, "Name": "Track B" }
]
}
],
"page": 1,
"per_page": 1,
"total": 347
}
```

---

### 🎢 GET `/albums//tracks`

Returns all tracks from an album:

```json
{
"album_id": 1,
"tracks": [
{ "TrackId": 1, "Name": "Track A" },
{ "TrackId": 2, "Name": "Track B" }
]
}
```

---

### πŸ“Š GET `/albums/summary`

Returns album summaries with artist and track count:

```json
[
{
"AlbumId": 1,
"AlbumTitle": "For Those About To Rock",
"ArtistName": "AC/DC",
"TrackCount": 10
}
]
```

---

## πŸ§ͺ Running Tests

### πŸ”Ή Run all tests

```bash
make test
```

### πŸ”Ή Run coverage

```bash
make coverage
```

Coverage report is also available in `htmlcov/index.html`.

---

## πŸ›  Makefile Commands

```bash
make run # Run the Flask app locally
make test # Run all tests with pytest
make coverage # Run coverage report
make format # Format code with black
make clean # Clean pyc/__pycache__
make up # Start docker container
make down # Stop docker container
```

---

## 🐍 Dependencies

Install with:

```bash
pip install -r requirements.txt
pip install -r requirements-dev.txt # For testing & development
```

---

## πŸ” Swagger API Docs

Accessible without authentication at:

```
http://localhost:5000/apidocs/
```

Fully autogenerated via Flasgger.

---

### βœ… Project Highlights

This repository demonstrates a complete, production-ready Flask REST API with:

- Modular and scalable architecture
- Full Docker support for reproducible environments
- High test coverage with `pytest` and GitHub Actions
- Swagger UI documentation at `/apidocs/`
- Error handling and logging centralized
- Clean and readable codebase with auto-formatting via `make format`

All core functionality is covered by unit tests, with automated CI validating each push and pull request. This project serves as a solid foundation or reference for building robust backend services in Flask.

---

## πŸ§‘β€πŸ’» Author

Developed by **MartΓ­n PelΓ‘ez DΓ­az**
GitHub: [@martinp95](https://github.com/martinp95)

---

## πŸ“„ License

Licensed under the [MIT License](LICENSE).