Get the FastAPI example running in under 5 minutes.
- Python 3.8 or higher
- pip (Python package manager)
pip install -r requirements-fastapi.txtOr install manually:
pip install fastapi uvicorn pydantic[email]python fastapi_oop_example.pyYou should see:
╔══════════════════════════════════════════════════════════════════╗
║ Cloud Engineers API - OOP FastAPI Example ║
╚══════════════════════════════════════════════════════════════════╝
Starting server...
API Documentation: http://localhost:8000/docs
Alternative Docs: http://localhost:8000/redoc
Navigate to: http://localhost:8000/docs
You'll see the interactive API documentation (Swagger UI).
- Click on
GET /engineers - Click "Try it out"
- Click "Execute"
- See the pre-loaded sample data
- Click on
POST /engineers - Click "Try it out"
- Edit the request body:
{
"name": "Your Name",
"email": "yourname@rockstars.com",
"specialty": "Cloud Engineering",
"hourly_rate": 110,
"certification_level": "mid"
}- Click "Execute"
- See the response with the created engineer
- Click on
POST /engineers/{engineer_id}/certifications - Click "Try it out"
- Enter
1for engineer_id - Edit request body:
{
"cert_code": "AZ-305",
"platform": "azure"
}- Click "Execute"
- Click on
GET /engineers/platform/{platform} - Click "Try it out"
- Select
azurefrom dropdown - Click "Execute"
- See all engineers with Azure certifications
- Click on
GET /reports/revenue - Click "Try it out"
- Click "Execute"
- See the revenue analysis
curl http://localhost:8000/engineerscurl -X POST http://localhost:8000/engineers \
-H "Content-Type: application/json" \
-d '{
"name": "Jane Smith",
"email": "jane@rockstars.com",
"specialty": "Kubernetes",
"hourly_rate": 115,
"certification_level": "senior"
}'curl http://localhost:8000/engineers/1curl -X PATCH http://localhost:8000/engineers/1 \
-H "Content-Type: application/json" \
-d '{
"hourly_rate": 125,
"is_available": false
}'curl -X POST http://localhost:8000/engineers/1/certifications \
-H "Content-Type: application/json" \
-d '{
"cert_code": "AZ-500",
"platform": "azure"
}'curl http://localhost:8000/engineers/platform/azurecurl http://localhost:8000/reports/revenuepip install pytest pytest-asyncio httpxpytest test_fastapi_oop.py -vpytest test_fastapi_oop.py::TestEngineerDomainModel -vpytest test_fastapi_oop.py --cov=fastapi_oop_example --cov-report=htmlfastapi_oop_example.py- Main application (800+ lines)FASTAPI_README.md- Detailed documentationtest_fastapi_oop.py- Comprehensive tests
API Endpoints (FastAPI)
↓
Service Layer (Business Logic)
↓
Repository (Data Access)
↓
Domain Models (Entities)
- Abstraction -
IEngineerRepositoryinterface - Encapsulation - Business logic in
Engineerclass - Dependency Injection - FastAPI's
Depends() - Singleton -
RepositoryManager - Validation - Pydantic models
If port 8000 is taken:
# Run on different port
uvicorn fastapi_oop_example:app --port 8001Make sure you're in the correct directory:
# Should contain fastapi_oop_example.py
ls -la
# Run from this directory
python fastapi_oop_example.py# Reinstall all dependencies
pip install -r requirements-fastapi.txt --upgrade- Read the full documentation:
FASTAPI_README.md - Study the code: Open
fastapi_oop_example.pyin your IDE - Run the tests:
pytest test_fastapi_oop.py -v - Modify the example: Add new endpoints or features
- Learn the patterns: Read about Repository and Service patterns
After mastering this example:
- Section 400 - Inheritance - Extend the Engineer class
- Section 600 - Abstraction - More on abstract classes
- Section 800 - Design Patterns - Patterns used in this example
- Interactive API Docs: http://localhost:8000/docs
- Alternative Docs: http://localhost:8000/redoc
- FastAPI Tutorial: https://fastapi.tiangolo.com/tutorial/
- Repository Pattern: https://martinfowler.com/eaaCatalog/repository.html
The code is heavily commented. Read through:
- Domain Models section
- Repository Pattern section
- Service Layer section
- Dependency Injection section
Each section has detailed comments explaining the OOP concepts.
Happy Learning! 🚀
Part of the learning-python-object-oriented repository