Skip to content

GitHub repository with examples to start programming FastAPI services from beginner to Full DDD Support

License

Notifications You must be signed in to change notification settings

akhtyamovpavel/FastAPITutorials

Repository files navigation

FastAPI Tutorial - Comprehensive Guide for Beginners

πŸ‡·πŸ‡Ί Русская вСрсия

This tutorial provides a complete learning path from basic FastAPI concepts to advanced Domain-Driven Design architecture.

πŸ“š Tutorial Structure

Example 01: Basic FastAPI Requests

  • βœ… GET, POST, PUT, DELETE methods
  • βœ… Path parameters (/items/{item_id})
  • βœ… Query parameters (?category=food&limit=10)
  • βœ… Request body validation
  • βœ… Response models and status codes
  • βœ… All examples in ONE file

File: example_01/main.py

Topics covered:

  • HTTP methods and when to use them
  • URL path parameters vs query parameters
  • Request body handling with Pydantic
  • Proper HTTP status codes (200, 201, 204, 404)
  • Async endpoints with async def

Example 02: Pydantic + BaseModel

  • βœ… BaseModel basics and field types
  • βœ… Advanced validation (Field, field_validator, model_validator)
  • βœ… Nested models and complex structures
  • βœ… Model configuration (ConfigDict)
  • βœ… Custom validators and business logic
  • βœ… All examples in ONE file

File: example_02/main.py

Topics covered:

  • Pydantic v2 syntax and best practices
  • Field constraints (min_length, max_length, pattern, gt, le)
  • EmailStr, HttpUrl, and other special types
  • Nested models for complex data structures
  • Request/Response model separation
  • Error handling and validation errors

Example 03: Database Integration (SQLAlchemy Async)

  • βœ… Async SQLAlchemy 2.0 configuration
  • βœ… Session management with Depends(get_db)
  • βœ… Modern ORM: mapped_column and Mapped[type] (NOT old Column)
  • βœ… Using db.execute(select(...)) instead of db.query(...)
  • βœ… Schema vs Model explained - Key difference!
  • βœ… Complete CRUD operations
  • βœ… All examples in ONE file

File: example_03/main.py

Key Concepts:

Schema (Pydantic) - API Layer:

  • Validates incoming data from clients
  • Serializes outgoing data to clients
  • Lives in the application/API layer
  • Examples: UserCreate, UserUpdate, UserResponse

Model (SQLAlchemy) - Database Layer:

  • Represents database table structure
  • Maps Python objects to database rows
  • Lives in the data/persistence layer
  • Example: User class with mapped_column

Why both?:

  • Separation of concerns
  • Database schema can differ from API contract
  • Can hide sensitive fields (passwords, internal IDs)
  • Multiple Pydantic schemas can use same Model

Example 04: Image Handling

  • βœ… File upload with validation
  • βœ… Image processing (resize, convert format)
  • βœ… PIL/Pillow library usage
  • βœ… File download (FileResponse, StreamingResponse)
  • βœ… Static file serving
  • βœ… On-the-fly thumbnail generation
  • βœ… File system operations

File: example_04/main.py

Key Operations:

  • Upload image with type validation
  • Resize images maintaining aspect ratio
  • Convert between formats (JPEG, PNG, WEBP)
  • Download processed images
  • Stream thumbnails without saving
  • List uploaded and processed files

Technologies:

  • FastAPI file upload (UploadFile)
  • PIL/Pillow for image processing
  • FileResponse for downloads
  • StreamingResponse for streaming
  • StaticFiles for serving directories

Example 05: Dependency Injection

  • βœ… How Depends() works internally
  • βœ… Service layer pattern with DI
  • βœ… Repository pattern for data access
  • βœ… Multi-level dependency chain
  • βœ… Testing benefits of DI
  • βœ… Request-scoped dependency caching

Files: example_05/main.py, example_05/test_di.py

Dependency Chain:

Router β†’ Service β†’ Repository β†’ Database Session

How Depends() Works:

  1. FastAPI analyzes function signature
  2. Recursively resolves all dependencies
  3. Caches instances per request (same session for all)
  4. Calls cleanup code (generators with yield)
  5. Injects resolved dependencies into function

Benefits:

  • Easy to test (can override dependencies)
  • Clean separation of concerns
  • Automatic resource management
  • Type-safe dependency injection
  • Reusable components

Example 06: Domain-Driven Design (Full Architecture)

Complete professional architecture with clear separation of layers.

Files:

example_06/
β”œβ”€β”€ domain/
β”‚   β”œβ”€β”€ models.py      # DAO: Database models (SQLAlchemy)
β”‚   └── schemas.py     # DTO: Data Transfer Objects (Pydantic)
β”œβ”€β”€ repositories/
β”‚   β”œβ”€β”€ base.py        # Generic CRUD repository
β”‚   └── user_repository.py  # User-specific queries
β”œβ”€β”€ services/
β”‚   └── user_service.py     # Business logic layer
β”œβ”€β”€ factories/
β”‚   └── user_factory.py     # Object creation patterns
β”œβ”€β”€ routers/
β”‚   └── users.py       # HTTP API endpoints (thin layer)
β”œβ”€β”€ database.py        # Database configuration
β”œβ”€β”€ unit_of_work.py    # Transaction management
└── main.py            # Application entry point

Architecture Layers:

  1. DAO (Data Access Objects) - domain/models.py

    • SQLAlchemy ORM models
    • Database table representation
  2. DTO (Data Transfer Objects) - domain/schemas.py

    • Pydantic models for API contracts
    • Validation and serialization
  3. Repositories - Data access abstraction

  4. Services - Business logic layer

  5. Factories - Object creation patterns

  6. UnitOfWork - Transaction management

  7. Routers - Thin HTTP layer


πŸš€ Getting Started

Installation

# Install all dependencies
pip install -r requirements.txt

# Or install for specific example
cd example_01
pip install fastapi uvicorn pydantic[email]

Running Examples

Example 01-03 (single file):

cd example_01  # or example_02, example_03
uvicorn main:app --reload

Example 04-06 (multiple files or single file):

cd example_04  # or example_05, example_06
python main.py

Access API docs:


πŸ“– Learning Path

Beginner

  1. Start with Example 01 - Learn basic HTTP methods
  2. Move to Example 02 - Master Pydantic validation
  3. Study Example 03 - Understand database integration

Intermediate

  1. Example 04 - Learn file handling and image processing
  2. Example 05 - Learn Dependency Injection pattern
  3. Understand Service and Repository layers

Advanced

  1. Example 06 - Full DDD architecture
  2. Build production-ready applications

πŸ”‘ Key Differences Explained

Schema vs Model (Example 03)

Schema (Pydantic):

  • For API validation and serialization
  • Lives in application layer
  • Examples: UserCreate, UserResponse

Model (SQLAlchemy):

  • For database representation
  • Lives in persistence layer
  • Example: User with mapped_column

Old vs New SQLAlchemy

❌ OLD (SQLAlchemy 1.x):

id = Column(Integer, primary_key=True)
user = db.query(User).filter(User.id == 1).first()

βœ… NEW (SQLAlchemy 2.0) - Used in this tutorial:

id: Mapped[int] = mapped_column(Integer, primary_key=True)
result = await db.execute(select(User).where(User.id == 1))
user = result.scalar_one_or_none()

Benefits:

  • Better type checking with Mapped[type]
  • IDE autocomplete support
  • Async/await native support
  • Unified query API
  • Better performance

🎯 Critical Requirements

βœ… 100% Async Code

  • All endpoints use async def
  • All database operations use await
  • AsyncSession for database
  • Async context managers

βœ… SQLAlchemy 2.0 Patterns

  • mapped_column instead of Column
  • Mapped[type] for type hints
  • db.execute(select(...)) instead of db.query(...)

βœ… File Organization

  • Examples 1-4: All code in ONE file (simple structure)
  • Examples 5-6: Proper layer separation (advanced architecture)
  • No files in root folder (all in subdirectories)

πŸ§ͺ Testing

Run tests for Example 05:

cd example_05
pytest test_di.py -v

Tests demonstrate:

  • Integration testing with test database
  • Dependency override patterns
  • Mocking repositories
  • Benefits of DI for testing

πŸ“š Additional Resources


🀝 Contributing

This is a teaching project. Each example is self-contained and can be studied independently.

Suggestions for improvement are welcome!


πŸ“„ License

Educational material under MIT License.

Happy Learning! πŸš€

About

GitHub repository with examples to start programming FastAPI services from beginner to Full DDD Support

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages