FastAPI: Modern Python Web Development (Final)
9781098135508
FastAPI is a young yet solid framework that takes advantage of newer Python features in a clean design. As its name impl
156
98
7MB
English
Pages 277
Year 2023
Report DMCA / Copyright
DOWNLOAD EPUB FILE
Table of contents :
Preface
Conventions Used in This Book
Using Code Examples
O’Reilly Online Learning
How to Contact Us
Acknowledgments
I. What’s New?
1. The Modern Web
Preview
Services and APIs
Kinds of APIs
HTTP
REST(ful)
JSON and API Data Formats
JSON:API
GraphQL
Concurrency
Layers
Data
Review
2. Modern Python
Preview
Tools
Getting Started
Python Itself
Package Management
Virtual Environments
Poetry
Source Formatting
Testing
Source Control and Continuous Integration
Web Tools
APIs and Services
Variables Are Names
Type Hints
Data Structures
Web Frameworks
Django
Flask
FastAPI
Review
II. A FastAPI Tour
3. FastAPI Tour
Preview
What Is FastAPI?
A FastAPI Application
HTTP Requests
URL Path
Query Parameters
Body
HTTP Header
Multiple Request Data
Which Method Is Best?
HTTP Responses
Status Code
Headers
Response Types
Type Conversion
Model Types and response_model
Automated Documentation
Complex Data
Review
4. Async, Concurrency, and Starlette Tour
Preview
Starlette
Types of Concurrency
Distributed and Parallel Computing
Operating System Processes
Operating System Threads
Green Threads
Callbacks
Python Generators
Python async, await, and asyncio
FastAPI and Async
Using Starlette Directly
Interlude: Cleaning the Clue House
Review
5. Pydantic, Type Hints, and Models Tour
Preview
Type Hinting
Data Grouping
Alternatives
A Simple Example
Validate Types
Validate Values
Review
6. Dependencies
Preview
What’s a Dependency?
Problems with Dependencies
Dependency Injection
FastAPI Dependencies
Writing a Dependency
Dependency Scope
Single Path
Multiple Paths
Global
Review
7. Framework Comparisons
Preview
Flask
Path
Query Parameter
Body
Header
Django
Other Web Framework Features
Databases
Recommendations
Other Python Web Frameworks
Review
III. Making a Website
8. Web Layer
Preview
Interlude: Top-Down, Bottom-Up, Middle-Out?
RESTful API Design
File and Directory Site Layout
The First Website Code
Requests
Multiple Routers
Build the Web Layer
Define Data Models
Stub and Fake Data
Create Common Functions Through the Stack
Create Fake Data
Test!
Using the FastAPI Automated Test Forms
Talking to the Service and Data Layers
Pagination and Sorting
Review
9. Service Layer
Preview
Defining a Service
Layout
Protection
Functions
Test!
Other Service-Level Stuff
Logging
Metrics, Monitoring, Observability
Tracing
Other
Review
10. Data Layer
Preview
DB-API
SQLite
Layout
Making It Work
Test!
Full Tests
Get all explorers
Get one explorer
Missing and duplicate data
Unit Tests
Review
11. Authentication and Authorization
Preview
Interlude 1: Do You Need Authentication?
Authentication Methods
Global Authentication: Shared Secret
Simple Individual Authentication
Fancier Individual Authentication
OAuth2
User Model
User Data Layer
User Fake Data Layer
User Service Layer
User Web Layer
Test!
Top Layer
Authentication Steps
JWT
Third-Party Authentication: OIDC
Authorization
Middleware
CORS
Third-Party Packages
Review
12. Testing
Preview
Web API Testing
Where to Test
What to Test
Pytest
Layout
Automated Unit Tests
Mocking
Test Doubles and Fakes
Web
Service
Data
Automated Integration Tests
The Repository Pattern
Automated Full Tests
Security Testing
Load Testing
Review
13. Production
Preview
Deployment
Multiple Workers
HTTPS
Docker
Cloud Services
Kubernetes
Performance
Async
Caches
Databases, Files, and Memory
Queues
Python Itself
Troubleshooting
Kinds of Problems
Logging
Metrics
Review
IV. A Gallery
14. Databases, Data Science, and a Little AI
Preview
Data Storage Alternatives
Relational Databases and SQL
SQLAlchemy
Core
SQLAlchemy Expression Language
ORM
SQLModel
SQLite
PostgreSQL
EdgeDB
Nonrelational (NoSQL) Databases
Redis
MongoDB
Cassandra
Elasticsearch
NoSQL Features in SQL Databases
Database Load Testing
Data Science and AI
Review
15. Files
Preview
Multipart Support
Uploading Files
File()
UploadFile
Downloading Files
FileResponse
StreamingResponse
Serving Static Files
Review
16. Forms and Templates
Preview
Forms
Templates
Review
17. Data Discovery and Visualization
Preview
Python and Data
PSV Text Output
csv
python-tabulate
pandas
SQLite Data Source and Web Output
Chart/Graph Packages
Chart Example 1: Test
Chart Example 2: Histogram
Map Packages
Map Example
Review
18. Games
Preview
Python Game Packages
Splitting Game Logic
Game Design
Web Part One: Game Initialization
Web Part Two: Game Steps
Service Part One: Initialization
Service Part Two: Scoring
Test!
Data: Initialization
Let’s Play Cryptonamicon
Review
A. Further Reading
Python
FastAPI
Starlette
Pydantic
B. Creatures and Humans
Creatures
Explorers
Explorer Publications
Other Sources
Index