AskReference
Cover of FastAPI: Modern Python Web Development

FastAPI: Modern Python Web Development

Bill Lubanovic;

First Edition

PublisherO'Reilly Media, Inc.Published2024pages280LanguageEnglishISBN-139781098135508ISBN-101098135504FormatPDF
View ebook
Modern Web DevelopmentModern Python FeaturesFastAPI FundamentalsAsynchronous Programming and ConcurrencyPydantic and Data ValidationDependency InjectionFramework ComparisonsWeb Layer DesignService Layer Architecture

About this book

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.

“FastAPI made simple!

This book excels at simplifying FastAPI concepts, showcasing the author’s mastery. Readers will gain practical knowledge and hit the ground running.”

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. The book also gets 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., Ganesh Harke

Senior Software Engineer, Citibank

Through the course of this book, you will:

  • Learn how to build web applications with FastAPI

“This book provides a comprehensive overview of the FastAPI framework and its surrounding ecosystem, giving readers a quick yet comprehensive view of modern web development.”

  • 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

Bill Lubanovic has been a developer for over 40 years, specializing in Linux, the Web, and Python. He recently used FastAPI with his team to rewrite a large biomedical research API. Bill coauthored Linux System Administration and wrote Introducing Python, both for O’Reilly., William Jamir Silva

Senior Software Engineer, Adjust GmbH

Questions & Answers from this book

37 questions17 chapters covered18 topics

Questions and answers are connected to the referenced book and its available source material.

Chapter 4: Async, Concurrency, and Starlette Tour

Chapter 12: Testing

How does the book demonstrate mocking in pytest? Provide examples of different ways to mock the preamble function.

The book demonstrates mocking in pytest by replacing the preamble() function from mod1.py with a mock so that summer() can be tested in isolation. It shows four variations in Example 12-5: mock.patch as a context manager with return_value set, a context manager that stores the mock object and assigns its return_value, a @mock.patch decorator that passes the mock as a test argument, and a decorator form where return_value is assigned inside the test body. It also demonstrates a fake/double approach using an environment variable and import switching.

Intermediatep. 183-191
Read answer

What is the purpose of the Schemathesis tool in FastAPI testing?

Schemathesis is a property-based testing tool that reads the OpenAPI schema FastAPI generates (openapi.json) and automatically generates and runs many tests with varied data against your API endpoints. It uses the Hypothesis library under the hood, works with pytest, and can uncover failures such as server errors without writing individual endpoint tests.

Intermediatep. 183-201
Read answer

How does the Data layer unit test in Example 12-15 avoid needing fake modules, and what environment variable is set before importing data modules?

The Data layer unit test avoids fake modules by configuring the Data layer to use an in-memory SQLite database instead of a file-based one, which requires no code changes to the Data modules. The environment variable set is CRYPTID_SQLITE_DB, and it is set to ":memory:" before importing the data modules.

Intermediatep. 186-197
Read answer

You may also be interested in