This repository is based on the paid course with the same name from testdriven.io, with some minor custom modifications (original repo here). The project consists in the development of an asynchronous text summarization API following Test-Driven Development (TDD) practices. The API follows RESTful design principles and enables the typical CRUD operations on summaries.
These summaries represent condensed descriptions of web pages, each summary resource
will have a url
and the respective summary
. This project uses the newspaper3k
library that provides both the web scraping and summarizing parts.
The following technologies have been used:
- Python API developed in Python which supports many popular web frameworks.
- FastAPI a recent and trendy Python web framework supporting async out-of-the-box and data validation based on type hints.
- Pytest a Python test framework which makes it easy to write and run unit and integration tests.
- Tortoise ORM a Python library used as an async Object Relational Mapper (ORM), allowing us to seamlessly interact with relational database from an async context.
- PostgresSQL the relational database where the API's data will live.
- Docker container platform used to quickly, easily and reliably deploy our web application into production.
- GitHub Packages where the Docker images will be stored.
- GitHub Actions the CI/CD solution that automates the build, test and deployment of the web app code to the production.
- Heroku the PaaS solution for hosting our web app (in production).
This API implements the following routes:
Endpoint | HTTP method | CRUD method | Description |
---|---|---|---|
/summaries |
GET | READ | get all summaries |
/summaries/<id> |
GET | READ | get summary by id |
/summaries |
POST | INSERT | add a new summary |
/summaries/<id> |
DELETE | DELETE | delete summary by id |
/summaries/<id> |
PUT | UPDATE | update summary by id |
To build, test and run this API we'll be using docker-compose
. As such, the first step
is to build the images defined in the docker-compose.yml
file.
$ docker-compose build
This will build two images:
fastapi-tdd-docker_web
image with REST API.fastapi-tdd-docker_web-db
image with Postgres database.
To run the containers previously built, execute the following:
$ docker-compose up -d
This will launch two services named web
(the API) and web-db
(the underlying
database) in background. The web
service will be running on port 8002
on localhost.
Whereas the database will be exposed to the web
service. To make sure the
app is running correctly open http://localhost:8002/ping in
your web browser (and/or run docker-compose logs -f
from the command line).
The database can be created by running the following command:
$ docker-compose exec web python app/db.py
One can confirm that the database was properly created by accessing the database container and starting a psql console.
$ docker-compose exec web-db psql -U postgres
Next, one can connect to the web_dev
database and list all the tables:
# \c web_dev
# \dt
The tests can be executed with:
$ docker-compose exec web pytest
Or including a coverage check:
$ docker-compose exec web pytest --cov="."
Another step to ensure the code contains the desired quality is to perform linting, that
is, to check for stylistic or programming errors. The following command will run the
flake8
linter throughout the source code:
$ docker-compose exec web flake8 .
Next, we perform additional checks to verify, and possibly correct, the code formatting
(using black
) and the ordering and organization of import statements (using isort
).
$ docker-compose exec web black . --check
$ docker-compose exec web isort . --check-only