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
- Host: GitHub
- URL: https://github.com/martinp95/flask_api_chinook
- Owner: martinp95
- License: mit
- Created: 2025-03-27T21:26:44.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-27T22:21:54.000Z (over 1 year ago)
- Last Synced: 2025-03-27T22:33:27.460Z (over 1 year ago)
- Topics: flask, flask-restful, flask-sqlalchemy, marshmallow, sqlalchemy, sqlite3
- Language: Python
- Homepage:
- Size: 339 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# π΅ Chinook REST API

[](https://codecov.io/gh/martinp95/flask_api_chinook)

[](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).