Cover....1
Copyright....3
Table of Contents....6
Preface....14
Conventions Used in This Book....15
Using Code Examples....16
O’Reilly Online Learning....16
How to Contact Us....17
Acknowledgments....17
Part I. What’s New?....18
Chapter 1. The Modern Web....20
Preview....20
Services and APIs....21
Kinds of APIs....21
HTTP....22
REST(ful)....22
JSON and API Data Formats....24
JSON:API....24
GraphQL....25
Concurrency....25
Layers....26
Data....29
Review....30
Chapter 2. Modern Python....32
Preview....32
Tools....32
Getting Started....33
Python Itself....34
Package Management....34
Virtual Environments....34
Poetry....35
Source Formatting....36
Testing....36
Source Control and Continuous Integration....36
Web Tools....36
APIs and Services....37
Variables Are Names....37
Type Hints....38
Data Structures....38
Web Frameworks....38
Django....39
Flask....39
FastAPI....39
Review....40
Part II. A FastAPI Tour....42
Chapter 3. FastAPI Tour....44
Preview....44
What Is FastAPI?....44
A FastAPI Application....45
HTTP Requests....49
URL Path....50
Query Parameters....51
Body....53
HTTP Header....54
Multiple Request Data....55
Which Method Is Best?....56
HTTP Responses....56
Status Code....56
Headers....57
Response Types....57
Type Conversion....58
Model Types and response_model....59
Automated Documentation....60
Complex Data....64
Review....64
Chapter 4. Async, Concurrency, and Starlette Tour....66
Preview....66
Starlette....66
Types of Concurrency....67
Distributed and Parallel Computing....67
Operating System Processes....67
Operating System Threads....68
Green Threads....68
Callbacks....68
Python Generators....69
Python async, await, and asyncio....70
FastAPI and Async....72
Using Starlette Directly....74
Interlude: Cleaning the Clue House....74
Review....76
Chapter 5. Pydantic, Type Hints, and Models Tour....78
Preview....78
Type Hinting....78
Data Grouping....81
Alternatives....85
A Simple Example....86
Validate Types....89
Validate Values....90
Review....92
Chapter 6. Dependencies....94
Preview....94
What’s a Dependency?....94
Problems with Dependencies....95
Dependency Injection....95
FastAPI Dependencies....95
Writing a Dependency....96
Dependency Scope....97
Single Path....97
Multiple Paths....98
Global....98
Review....99
Chapter 7. Framework Comparisons....100
Preview....100
Flask....100
Path....101
Query Parameter....102
Body....102
Header....103
Django....103
Other Web Framework Features....104
Databases....105
Recommendations....105
Other Python Web Frameworks....105
Review....106
Part III. Making a Website....108
Chapter 8. Web Layer....110
Preview....110
Interlude: Top-Down, Bottom-Up, Middle-Out?....111
RESTful API Design....112
File and Directory Site Layout....114
The First Website Code....115
Requests....117
Multiple Routers....119
Build the Web Layer....120
Define Data Models....120
Stub and Fake Data....121
Create Common Functions Through the Stack....121
Create Fake Data....121
Test!....126
Using the FastAPI Automated Test Forms....127
Talking to the Service and Data Layers....129
Pagination and Sorting....130
Review....131
Chapter 9. Service Layer....132
Preview....132
Defining a Service....132
Layout....133
Protection....133
Functions....133
Test!....135
Other Service-Level Stuff....137
Logging....137
Metrics, Monitoring, Observability....137
Tracing....137
Other....138
Review....138
Chapter 10. Data Layer....140
Preview....140
DB-API....140
SQLite....142
Layout....144
Making It Work....144
Test!....148
Full Tests....148
Unit Tests....157
Review....159
Chapter 11. Authentication and Authorization....160
Preview....160
Interlude 1: Do You Need Authentication?....161
Authentication Methods....162
Global Authentication: Shared Secret....162
Simple Individual Authentication....165
Fancier Individual Authentication....166
OAuth2....167
User Model....168
User Data Layer....168
User Fake Data Layer....170
User Service Layer....171
User Web Layer....173
Test!....175
Top Layer....175
Authentication Steps....175
JWT....176
Third-Party Authentication: OIDC....176
Authorization....177
Middleware....178
CORS....179
Third-Party Packages....180
Review....180
Chapter 12. Testing....182
Preview....182
Web API Testing....182
Where to Test....183
What to Test....183
Pytest....184
Layout....185
Automated Unit Tests....185
Mocking....185
Test Doubles and Fakes....188
Web....190
Service....192
Data....194
Automated Integration Tests....195
The Repository Pattern....196
Automated Full Tests....197
Security Testing....199
Load Testing....199
Review....200
Chapter 13. Production....202
Preview....202
Deployment....202
Multiple Workers....203
HTTPS....204
Docker....204
Cloud Services....205
Kubernetes....205
Performance....205
Async....205
Caches....206
Databases, Files, and Memory....206
Queues....206
Python Itself....207
Troubleshooting....207
Kinds of Problems....207
Logging....208
Metrics....208
Review....208
Part IV. A Gallery....210
Chapter 14. Databases, Data Science, and a Little AI....212
Preview....212
Data Storage Alternatives....212
Relational Databases and SQL....213
SQLAlchemy....214
SQLModel....215
SQLite....216
PostgreSQL....216
EdgeDB....216
Nonrelational (NoSQL) Databases....217
Redis....217
MongoDB....217
Cassandra....217
Elasticsearch....218
NoSQL Features in SQL Databases....218
Database Load Testing....218
Data Science and AI....220
Review....222
Chapter 15. Files....224
Preview....224
Multipart Support....224
Uploading Files....224
File()....225
UploadFile....226
Downloading Files....227
FileResponse....227
StreamingResponse....228
Serving Static Files....228
Review....230
Chapter 16. Forms and Templates....232
Preview....232
Forms....232
Templates....234
Review....237
Chapter 17. Data Discovery and Visualization....238
Preview....238
Python and Data....238
PSV Text Output....239
csv....239
python-tabulate....240
pandas....241
SQLite Data Source and Web Output....242
Chart/Graph Packages....242
Chart Example 1: Test....243
Chart Example 2: Histogram....245
Map Packages....246
Map Example....247
Review....249
Chapter 18. Games....250
Preview....250
Python Game Packages....250
Splitting Game Logic....251
Game Design....251
Web Part One: Game Initialization....252
Web Part Two: Game Steps....253
Service Part One: Initialization....255
Service Part Two: Scoring....255
Test!....256
Data: Initialization....257
Let’s Play Cryptonamicon....257
Review....259
Appendix A. Further Reading....260
Python....260
FastAPI....261
Starlette....262
Pydantic....262
Appendix B. Creatures and Humans....264
Creatures....265
Explorers....267
Explorer Publications....267
Other Sources....268
Index....270
About the Author....278
Colophon....278
FastAPI is a young yet solid framework that takes advantage of newer Python features in a clean design. As its name implies, FastAPI is indeed fast, rivaling similar frameworks in languages such as Golang. With this practical book, developers familiar with Python will learn how FastAPI lets you accomplish more in less time with less code.
Author Bill Lubanovic covers the nuts and bolts of FastAPI development with how-to guides on various topics such as forms, database access, graphics, maps, and more that will take you beyond the basics. This book also includes how-to guides that will get you up to speed on RESTful APIs, data validation, authorization, and performance. With its similarities to frameworks like Flask and Django, you'll find it easy to get started with FastAPI.