Skip to content

martinp95/flask_api_chinook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎡 Chinook REST API

Tests codecov Python 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

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

πŸ”Έ 2. Start the application with Docker

make up

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


πŸ“Œ 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:

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

πŸ’Ώ GET /artists/<artist_id>/albums

Returns albums by artist:

[
  { "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:

{
  "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/<album_id>/tracks

Returns all tracks from an album:

{
  "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:

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

πŸ§ͺ Running Tests

πŸ”Ή Run all tests

make test

πŸ”Ή Run coverage

make coverage

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


πŸ›  Makefile Commands

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:

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


πŸ“„ License

Licensed under the MIT License.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors