Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Building Python Microservices with FastAPI

You're reading from   Building Python Microservices with FastAPI Build secure, scalable, and structured Python microservices from design concepts to infrastructure

Arrow left icon
Product type Paperback
Published in Aug 2022
Publisher Packt
ISBN-13 9781803245966
Length 420 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Sherwin John C. Tragura Sherwin John C. Tragura
Author Profile Icon Sherwin John C. Tragura
Sherwin John C. Tragura
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Part 1: Application-Related Architectural Concepts for FastAPI microservice development
2. Chapter 1: Setting Up FastAPI for Starters FREE CHAPTER 3. Chapter 2: Exploring the Core Features 4. Chapter 3: Investigating Dependency Injection 5. Chapter 4: Building the Microservice Application 6. Part 2: Data-Centric and Communication-Focused Microservices Concerns and Issues
7. Chapter 5: Connecting to a Relational Database 8. Chapter 6: Using a Non-Relational Database 9. Chapter 7: Securing the REST APIs 10. Chapter 8: Creating Coroutines, Events, and Message-Driven Transactions 11. Part 3: Infrastructure-Related Issues, Numerical and Symbolic Computations, and Testing Microservices
12. Chapter 9: Utilizing Other Advanced Features 13. Chapter 10: Solving Numerical, Symbolic, and Graphical Problems 14. Chapter 11: Adding Other Microservice Features 15. Index 16. Other Books You May Enjoy

Initializing and configuring FastAPI

Learning how to create applications using FastAPI is easy and straightforward. A simple application can be created just by creating a main.py file inside your /ch01 project folder. In our online academic discussion forum, for instance, the application started with this code:

from fastapi import FastAPI
app = FastAPI()

This initializes the FastAPI framework. The application needs to instantiate the core FastAPI class from the fastapi module and use app as the reference variable to the object. Then, this object is used later as a Python @app decorator, which provides our application with some features such as routes, middleware, exception handlers, and path operations.

Important note

You can replace app with your preferred but valid Python variable name, such as main_app, forum, or myapp.

Now, your application is ready to manage REST APIs that are technically Python functions. But to declare them as REST service methods, we need to decorate them with the appropriate HTTP request method provided by the path operation @app decorator. This decorator contains the get(), post(), delete(), put(), head(), patch(), trace(), and options() path operations, which correspond to the eight HTTP request methods. And these path operations are decorated or annotated on top of the Python functions that we want to handle the request and response.

In our specimen, the first sample that the REST API created was this:

@app.get("/ch01/index")
def index():
    return {"message": "Welcome FastAPI Nerds"} 

The preceding is a GET API service method that returns a JSON object. To locally run our application, we need to execute the following command:

uvicorn main:app --reload

This command will load the forum application to the uvicorn live server through the application’s main.py file with FastAPI object referencing. Live reload is allowed by adding the --reload option, which enables the restart of the development server whenever there are changes in the code.

Figure 1.1 – The uvicorn console log

Figure 1.1 – The uvicorn console log

Figure 1.1 shows that uvicorn uses localhost to run the application with the default port 8000. We can access our index page through http://localhost:8000/ch01/index. To stop the server, you just need to press the Ctrl + C keyboard keys.

After running our first endpoint, let us now explore how to implement the other types of HTTP methods, namely POST, DELETE, PUT, and PATCH.

You have been reading a chapter from
Building Python Microservices with FastAPI
Published in: Aug 2022
Publisher: Packt
ISBN-13: 9781803245966
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime