FastAPI

Book description

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.

Through the course of this book, you will:

  • Learn how to build web applications with FastAPI
  • Understand the differences between FastAPI, Starlette, and pydantic
  • Learn two features that set FastAPI apart: asynchronous functions and data type checking and validation
  • Examine new features of Python 3.8+, especially type annotations
  • Understand the differences between sync and async Python
  • Learn how to connect with external APIs and services

Publisher resources

View/Submit Errata

Table of contents

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

Product information

  • Title: FastAPI
  • Author(s): Bill Lubanovic
  • Release date: November 2023
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098135508