A RESTful API for a simple banking system built with Flask and Flask-RESTX.
- Features
- Prerequisites
- Setup
- API Documentation
- System Architecture
- Project Structure
- Testing
- Design Principles
- Future Enhancements
- Account management (create, read)
- Transaction processing (deposits, withdrawals)
- Transaction history tracking
- RESTful API with Swagger documentation
- Python 3.7 or higher
- Docker (optional, for containerized deployment)
docker-compose up --buildThe API will be available at http://localhost:5000
# Create and activate virtual environment
python -m venv venv
.\venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Set environment variables
$env:FLASK_APP = "SimpleBankingSystem"
$env:FLASK_ENV = "development"
# Create data directory
mkdir data
# Run the application
flask runNote: Always make sure you're in the virtual environment (you should see (venv) at the start of your prompt) before running any Python commands.
The API documentation is defined in SimpleBankingSystem/api.py using Flask-RESTX. It includes:
- API models and schemas
- Endpoint documentation
- Request/response examples
- Parameter descriptions
Access the interactive Swagger documentation at:
http://localhost:5000/api/v1/swagger/
The documentation is automatically generated from the API definitions in api.py and includes:
- Account operations (create, read, deposit, withdraw, transfer)
- Transaction operations (list, details)
- Health check endpoint
- Request/response models
The system uses the Command pattern to encapsulate banking operations:
class BaseCommand(ABC):
@abstractmethod
def execute(self, accounts: Dict[str, BankAccount]) -> Dict[str, Any]:
"""Perform the command action."""
pass
def save_state(self, accounts: Dict[str, BankAccount]):
"""Save the current state of all accounts to CSV files."""
pass-
Command Pattern
- Encapsulates operations as objects
- Supports undoable operations
- Enables command queuing and logging
- Provides uniform interface for all operations
-
State Management
- Transaction states: PENDING → PROCESSING → COMPLETED/FAILED
- Immutable completed transactions
- Retry mechanism for failed transactions
- Thread-safe operations
State Transition Flow:
[*] --> PENDING: Transaction Created PENDING --> PROCESSING: Start Processing PENDING --> COMPLETED: Direct Completion PENDING --> FAILED: Validation Failed PROCESSING --> COMPLETED: Success PROCESSING --> FAILED: Processing Error FAILED --> PENDING: Retry COMPLETED --> [*]: Final State -
Memory Management
- In-memory transaction queue
- Automatic archiving of completed transactions
- CSV-based persistence
- Efficient resource usage
System States Persistence:
- Accounts Data: Stored in
data/accounts.csv - Active Transactions: Stored in
data/transactions.csv - Archived Transactions: Stored in
data/archived_transactions.csv
Transaction Processing Paths:
- Fast Path: Transactions are appended to an in-memory queue for quick processing
- Slow Path: Sorting and ordering are deferred to reporting/archiving operations
-
Concurrency Control
- Account-level locking
- Thread-safe operations
- Parallel processing for different accounts
.
├── .dockerignore # Docker ignore file
├── .gitattributes # Git attributes
├── .gitignore # Git ignore file
├── Dockerfile # Docker configuration
├── README.md # Project documentation
├── assets/ # Documentation assets
├── data/ # Data storage directory
│ ├── accounts.csv # Account data
│ ├── transactions.csv # Active transactions
│ └── archived_transactions.csv # Archived transactions
├── docker-compose.yml # Docker compose configuration
├── requirements.txt # Python dependencies
├── setup.py # Package setup file
└── SimpleBankingSystem/ # Main application package
├── __init__.py
├── api.py # API routes and documentation
├── app.py # Flask application factory
├── commands.py # Command pattern implementation
├── commands/ # Command implementations
├── config.py # Application configuration
├── constants.py # System constants and enums
├── entities.py # Data models
├── logger.py # Logging configuration
├── main.py # Application entry point
├── transaction_log.py # Transaction logging
├── utils.py # Utility functions
└── tests/ # Test suite
├── test_routes.py # API test cases
└── test_commands.py # Command pattern test cases
Run tests using unittest:
# Make sure you're in the virtual environment first
.\venv\Scripts\activate
python -m unittest discover -s SimpleBankingSystem -p "test_*.py" -v- Install coverage package:
# Make sure you're in the virtual environment first
.\venv\Scripts\activate
pip install coverage- Run tests with coverage:
# Make sure you're in the virtual environment first
.\venv\Scripts\activate
# Run all tests with coverage
python -m coverage run -m unittest discover -s SimpleBankingSystem -p "test_*.py" -v
# Generate coverage report
python -m coverage report -m
# Generate HTML coverage report
python -m coverage html- View coverage results:
- Console report shows coverage percentage for each file
- HTML report (in
htmlcov/directory) provides detailed line-by-line coverage - Open
htmlcov/index.htmlin browser for interactive coverage report
- Coverage thresholds:
- Aim for at least 80% code coverage
- Focus on critical paths and business logic
- Exclude test files from coverage report
- Immutability: Completed transactions are immutable
- Thread Safety: All operations are thread-safe
- Decimal Precision: Financial calculations use Decimal
- Clean Architecture: Separation of concerns
- State Management: Clear transaction lifecycle
- System performance metrics
- Transaction processing statistics
- API endpoint monitoring
- Resource utilization tracking
- Custom business metrics dashboard
- Scheduled statement generation
- Batch processing of transaction data
- Integration with Presto for analytics
- Airflow DAGs for workflow automation
- Export to multiple formats (PDF, CSV, Excel)