FastAPI Cookbook - Giunio De Luca - E-Book

FastAPI Cookbook E-Book

Giunio De Luca

0,0
29,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

FastAPI is a cutting-edge Python framework that is revolutionizing the way web apps and APIs are built. Known for its speed, simplicity, and scalability, FastAPI empowers developers to create high-performing applications with ease. This book will help you leverage FastAPI’s immense potential to handle high-traffic scenarios and integrate seamlessly with modern Python tools.
The book begins by familiarizing you with the basics of setting up and configuring your FastAPI environment before moving to the intricacies of building RESTful APIs, managing data with SQL and NoSQL databases, and handling authentication and authorization. Next, you'll focus on advanced topics such as custom middleware, WebSocket communication, and integration with various Python libraries. Each chapter is meticulously crafted with practical recipes, progressing from foundational concepts to advanced features and best practices. The concluding chapters show you how to optimize performance, implement rate limiting, and execute background tasks, empowering you to become a proficient FastAPI developer.
By the end of this book, you'll have gained the skills you need to migrate existing apps to FastAPI, and be equipped to tackle any challenge in the modern web development landscape, ensuring your apps are not only functional, but also efficient, secure, and scalable.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 365

Veröffentlichungsjahr: 2024

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



FastAPI Cookbook

Develop high-performance APIs and web applications with Python

Giunio De Luca

FastAPI Cookbook

Copyright © 2024 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

The author acknowledges the use of cutting-edge AI, such as ChatGPT, with the sole aim of enhancing the language and clarity within the book, thereby ensuring a smooth reading experience for readers. It’s important to note that the content itself has been crafted by the author and edited by a professional publishing team. 

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. 

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. 

Group Product Manager: Kaustubh Manglurkar

Publishing Product Manager: Bhavya Rao

Book Project Manager: Arul Viveaun S

Senior Editor: Nathanya Dias

Technical Editor: Simran Ali

Copy Editor: Safis Editing

Indexer: Manju Arasan

Production Designer: Jyoti Kadam

DevRel Marketing Coordinators: Anamika Singh and Nivedita Pandey

First published: August 2024

Production reference: 1040724

Published by Packt Publishing Ltd.

Grosvenor House

11 St Paul’s Square

Birmingham

B3 1RB, UK

ISBN 978-1-80512-785-7

www.packtpub.com

To my dear nephew, Marco.

You bring endless joy to our lives. I wish you a future filled with love, growth, and happiness. May you always find success and fulfillment in everything you do.

– Giunio De Luca

Foreword

Having worked alongside Giunio for over a year, I can confidently say that his expertise and passion for Python development are evident in every line of code he produces. I affirm this with no shadow of a doubt, as I am a developer with over 30 years of experience across several countries and prominent industries, including Microsoft, NATO, and IBM.

We naturally became friends due to our mutual respect and shared interests, both professional and personal. Giunio stood out in our team at Coreso, one of the European Regional Coordination Centers for high-voltage electricity flows. He has an impressive academic background coupled with extensive international experience. What truly sets him apart, however, is his deep and thorough understanding of Python and FastAPI. Giunio’s code is not only functional but also exceptionally clean and clear—a hallmark of a skilled developer, or in other words, a subject matter expert.

Beyond his technical prowess, Giunio possesses a quality that I find equally important: an open mind. He readily embraces new challenges, tackling them with both enthusiasm and a solid foundation of knowledge, which he diligently builds and extends if he does not already possess it. I believe that he has brought these qualities to fruition in creating this book, pushing it beyond the scope of a typical cookbook. For instance, dealing with real-time communication and WebSockets is something rarely found in other cookbooks, and finding the right solution for your needs can consume a lot of valuable time searching online.

This book promises to be a valuable resource for anyone seeking to become proficient in the use of FastAPI. Despite the excellent work the creators do with the product documentation, navigating it can easily lead to hours of frustration. Whether you’re a seasoned Python developer or just starting your journey, Giunio’s clear explanations and practical examples will guide you every step of the way.

So, let Giunio be your expert guide to the power and efficiency of FastAPI!

Antonio Ferraro

MSC Computer Science, Business Owner

Contributors

About the author

Giunio De Luca is a software engineer with over 10 years of experience in fields such as physics, sports, and administration. He graduated in industrial engineering from the University of Basilicata and holds a PhD in numerical simulations from Paris-Saclay University. His work spans developing advanced algorithms, creating sports analytics applications, and improving administrative processes. As an independent consultant, he collaborates with research labs, government agencies, and start-ups across Europe. He also supports coding education in schools and universities through workshops, lectures, and mentorship programs, inspiring the next generation of software engineers with his expertise and dedication.

With heartfelt gratitude, I thank my family and my loved ones for their continuous encouragement. I am also thankful to all my employers who have believed in my expertise, and my current clients and colleagues who keep trusting my work and providing me with valuable opportunities for professional growth.

About the reviewer

Adarsh Divakaran is an experienced backend developer with expertise in architecting, building, and deploying APIs, primarily using Python. He is the co-founder of Digievo Labs, a global technology firm. He loves reading all sorts of Python content and runs a newsletter, Python in the Wild, showcasing Python concepts and examples from open source projects. Adarsh also speaks at global Python conferences, including PyCascades, EuroPython, and FlaskCon.

Table of Contents

Preface

1

First Steps with FastAPI

Technical requirements

Setting up your development environment

Getting ready

How to do it...

Creating a new FastAPI project

Getting ready

How to do it...

There’s more...

See also

Understanding FastAPI basics

How to do it…

See also

Defining your first API endpoint

Getting ready

How to do it...

How it works…

Working with path and query parameters

Getting ready

How to do it…

How it works…

See also

Defining and using request and response models

Getting ready

How to do it...

See also

Handling errors and exceptions

How to do it…

There’s more…

See also

2

Working with Data

Technical requirements

Setting up SQL databases

Getting ready

How to do it…

Establishing a database connection

See also

Understanding CRUD operations with SQLAlchemy

Getting ready

How to do it…

See also

Integrating MongoDB for NoSQL data storage

Getting ready

How to do it…

See also

Working with data validation and serialization

Getting ready

How to do it…

Serialization and deserialization concepts

Advanced validation techniques

See also

Working with file uploads and downloads

Getting ready

How to do it…

Implementing file uploads

Managing file downloads and storage

See also

Handling asynchronous data operations

Getting ready

How to do it…

There’s more…

See also

Securing sensitive data and best practices

Getting ready

How to do it…

There’s more…

3

Building RESTful APIs with FastAPI

Technical requirements

Creating CRUD operations

How to do it…

How it works...

Creating RESTful Endpoints

Getting ready…

How to do it…

Testing your RESTful API

Getting ready…

How to do it...

See also

Handling complex queries and filtering

Getting ready…

How to do it...

Versioning your API

Getting ready…

How to do it...

There’s more…

See also

Securing your API with OAuth2

Getting ready…

How to do it…

There’s more…

See also

Documenting your API with Swagger and Redoc

Getting ready…

How to do it...

See also

4

Authentication and Authorization

Technical requirements

Setting up user registration

Getting ready

How to do it…

How it works...

See also

Working with OAuth2 and JWT for authentication

Getting ready

How to do it...

How it works…

See also

Setting up RBAC

Getting ready

How to do it...

There’s more…

Using third-party authentication

Getting ready

How to do it...

How it works…

See also

Implementing MFA

Getting ready

How to do it...

See also

Handling API key authentication

Getting ready

How to do it...

There’s more…

Handling session cookies and logout functionality

Getting ready

How to do it...

There’s more…

See also

5

Testing and Debugging FastAPI Applications

Technical requirements

Setting up testing environments

Getting ready

How to do it...

See also

Writing and running unit tests

Getting ready

How to do it…

See also

Testing API Endpoints

Getting ready

How to do it…

See also

Running tests techniques

Getting ready

How to do it...

Check test coverage

See also

Handling logging messages

Getting ready

How to do it...

There’s more

See also

Debugging techniques

Getting ready

How to do it...

Debugging with PDB

Debugging with VS Code

Debugging with PyCharm

See also

Performance testing for high traffic applications

Getting ready

How to do it...

See also

6

Integrating FastAPI with SQL Databases

Technical requirements

Setting up SQLAlchemy

Getting ready

How to do it...

How it works…

See also

Implementing CRUD operations

Getting ready

How to do it…

How it works…

Working with migrations

Getting ready

How to do it…

How it works…

See also

Handling relationships in SQL databases

Getting ready

How to do it…

See also

Optimizing SQL queries for performance

Getting ready

How to do it…

There’s more…

Securing sensitive data in SQL databases

Getting ready

How to do it…

See also

Handling transactions and concurrency

Getting ready

How to do it…

There’s more…

See also

7

Integrating FastAPI with NoSQL Databases

Technical requirements

Setting up MongoDB with FastAPI

Getting ready

How to do it…

See also

CRUD operations in MongoDB

Getting ready

How to do it…

See also

Handling relationships in NoSQL databases

Getting ready

How to do it…

See also

Working with indexes in MongoDB

Getting ready

How to do it…

How it works…

There’s more…

See also

Exposing sensitive data from NoSQL databases

Getting ready

How to do it…

There’s more…

See also

Integrating FastAPI with Elasticsearch

Getting ready

How to do it…

See also

Using Redis for caching in FastAPI

Getting ready

How to do it…

There’s more…

See also

8

Advanced Features and Best Practices

Technical requirements

Implementing dependency injection

Getting ready

How to do it…

How it works…

There’s more…

See also

Creating custom middleware

Getting ready…

How to do it…

How it works…

See also

Internationalization and localization

Getting ready

How to do it…

See also

Optimizing application performance

Getting ready

How to do it…

There’s more...

See also

Implementing rate limiting

Getting ready

How to do it…

There's more...

See also

Implementing background tasks

Getting ready

How to do it…

How it works…

See also

9

Working with WebSocket

Technical requirements

Setting up WebSockets in FastAPI

Getting ready

How to do it…

How it works…

See also

Sending and receiving messages over WebSockets

Getting ready

How to do it…

See also

Handling WebSocket connections and disconnections

Getting ready

How to do it…

Handling WebSocket errors and exceptions

Getting ready

How to do it…

See also

Implementing chat functionality with WebSockets

Getting ready

How to do it…

How it works…

See also

Optimizing WebSocket performance

Getting ready

How to do it…

There’s more…

See also

Securing WebSocket connections with OAuth2

Getting ready

How to do it…

See also

10

Integrating FastAPI with other Python Libraries

Technical requirements

Integrating FastAPI with gRPC

Getting ready

How to do it…

There’s more…

See also

Connecting FastAPI with GraphQL

Getting ready

How to do it…

See also

Using ML models with Joblib

Getting ready

How to do it…

See also

Integrating FastAPI with Cohere

Getting ready

How to do it…

See also

Integrating FastAPI with LangChain

Getting ready

How to do it…

See also

11

Middleware and Webhooks

Technical requirements

Creating custom ASGI middleware

Getting ready

How to do it…

How it works…

There’s more…

See also

Developing middleware for request modification

Getting ready

How to do it…

See also

Developing middleware for response modification

Getting ready

How to do it…

See also

Handling CORS with middleware

Getting ready

How to do it…

See also

Restricting incoming requests from hosts

Getting ready

How to do it…

See also

Implementing webhooks

Getting ready

How to do it…

How it works…

There’s more…

See also

12

Deploying and Managing FastAPI Applications

Technical requirements

Running the server with the FastAPI CLI

Getting ready

How to do it…

There’s more…

See also

Enabling HTTPS on FastAPI applications

Getting ready

How to do it…

There’s more…

See also

Running FastAPI applications in Docker containers

Getting ready

How to do it…

See also

Running the server across multiple workers

Getting ready

How to do it…

There’s more…

See also

Deploying your FastAPI application on the cloud

Getting started

How to do it…

There’s more…

See also

Shipping FastAPI applications with Hatch

Getting ready

How to do it…

There’s more…

See also

Index

Other Books You May Enjoy