A study project focused on building a clean, maintainable, and testable REST API using FastAPI and architectural patterns inspired by Clean Architecture and Hexagonal Architecture.
This project implements a simple Task Management API while exploring software design principles such as:
- Separation of concerns
- Dependency inversion
- Repository pattern
- Domain-driven organization
- Error handling
- Automated testing
- API design with FastAPI
Although the business domain is intentionally simple, the project structure aims to simulate how larger applications can be organized and scaled.
The application is organized into distinct layers:
app/
├── application/
│ └── task_use_cases.py
│
├── domain/
│ ├── exceptions.py
│ ├── models.py
│ └── ports.py
│
├── infrastructure/
│ └── persistence/
│ └── json_task_store.py
│
└── main.py
Contains the core business definitions and contracts.
Defines the application's entities and data structures:
TaskTaskCreateTaskUpdate
Defines repository contracts used by the application layer.
Example:
class TaskRepositoryPort(ABC):
...The domain does not know how data is stored; it only knows which operations must be available.
Custom domain exceptions:
DomainErrorTaskNotFoundError
Contains the use cases and application services.
Acts as the application's orchestration layer, coordinating interactions between the API and the repository abstraction.
Responsibilities include:
- Listing tasks
- Creating tasks
- Updating tasks
- Deleting tasks
- Retrieving pending tasks
The service depends on abstractions rather than concrete implementations.
Contains technical implementations and persistence details.
Implements the TaskRepositoryPort contract using a JSON file as the storage backend.
Features:
- Persistent storage
- Thread-safe writes
- Atomic file replacement
- Repository pattern implementation
Because the application depends on the repository interface, the storage backend can be replaced with SQLite, PostgreSQL, MongoDB, or any other persistence mechanism without modifying the business logic.
The FastAPI application exposes REST endpoints for task management.
Available endpoints:
| Method | Endpoint | Description |
|---|---|---|
| GET | /ping |
Health check |
| GET | /tasks |
List all tasks |
| GET | /tasks/pending |
List pending tasks |
| GET | /tasks/{id} |
Retrieve a task by ID |
| POST | /tasks |
Create a new task |
| PUT | /tasks/{id} |
Update an existing task |
| DELETE | /tasks/{id} |
Delete a task |
git clone <repository-url>
cd fastAPI-studyspython -m venv .venvActivate it:
Linux/macOS
source .venv/bin/activateWindows
.venv\Scripts\activatepip install -r requirements.txtuvicorn app.main:app --reloadThe API will be available at:
http://127.0.0.1:8000
Swagger UI:
http://127.0.0.1:8000/docs
ReDoc:
http://127.0.0.1:8000/redoc
Run the test suite with:
pytestFor verbose output:
pytest -vThis project was created as a learning environment to explore:
- FastAPI development
- API design
- Clean code practices
- Dependency inversion
- Repository abstractions
- Domain-driven organization
- Testability
- Maintainability
The goal is not to build a production-ready task manager, but to understand how scalable software architecture can be applied even to small applications.
Possible next steps include:
- SQLite/PostgreSQL support
- SQLAlchemy integration
- Authentication and authorization
- Dependency injection container
- Docker support
- CI/CD pipeline
- Structured logging
- Metrics and observability
- Domain events
- Service interfaces and use-case separation
This project is intended for educational and experimentation purposes.
""" API simples de tarefas usando FastAPI.
Objetivo do exercício:
- Entender como uma API recebe requisições HTTP.
- Entender como criar rotas com FastAPI.
- Entender como enviar e receber dados em JSON.
- Entender como validar dados usando Pydantic.
- Entender os métodos HTTP principais: GET, POST, PUT e DELETE.
Requisitos da API:
- Listar todas as tarefas.
- Buscar uma tarefa pelo ID.
- Criar uma nova tarefa.
- Atualizar uma tarefa existente.
- Deletar uma tarefa.
- Listar apenas tarefas pendentes.
Como rodar:
-
Instale as dependências: pip install fastapi uvicorn
-
Rode o servidor: uvicorn main:app --reload
-
Abra a documentação automática: http://127.0.0.1:8000/docs """