A Beginners Guide to Building MicroServices with FastAPI

techprane 141 views 12 slides Oct 12, 2024
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

Are you eager to dive into web development but don’t know where to start? Look no further! FastAPI is a modern, fast (high-performance) web framework that makes building APIs not just simple, but enjoyable.

In this tutorial, you'll discover how to harness the power of FastAPI to create effici...


Slide Content

Samuel Folasayo
A Beginners Introduction to FastAPI
A Modern Framework for Building APIs
Joe Nyirenda

What is FastAPI?
A modern web framework for building APIs

Based on Python type hints and Pydantic

Supports asynchronous programming

Limitations
Advantages and Limitations Over Other Libraries

Advantages

High performance due to
asynchronous support

Automatic data validation and
documentation

Easy to learn and use with minimal
boilerplate
May have a smaller ecosystem compared to
Flask or Django

Less mature tooling and resources for certain
use cases

Requires an understanding of async
programming for optimal use

Why Do I Need FastAPI as a Python Developer?
Rapid development with minimal boilerplate code

Enhanced performance for high-load applications

Built-in data validation and interactive documentation

Strong community and growing ecosystem

Code Snippet:
Quick Start
Key Points:

Simple app creation with minimal code

Use fastapi dev main.py to run the app

from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello, FastAPI!"}

Path Parameters
@app.get("/items/{item_id}")
def read_item(item_id: int):
return {"item_id": item_id}

You can declare path parameters in your endpoint. They’re passed as
arguments:
Here, item_id is a path parameter and is automatically converted to int.

Query Parameters
@app.get("/items/")
def read_item(skip: int = 0, limit: int = 10):
return {"skip": skip, "limit": limit}
Query parameters are automatically parsed:
You can access them via ?skip=0&limit=10 in the URL.

Data Validation with Pydantic
from pydantic import BaseModel

class Item(BaseModel):
name: str
price: float

@app.post("/items/")
def create_item(item: Item):
return {"name": item.name, "price": item.price}
FastAPI uses Pydantic models to validate request data and define schemas:
Pydantic automatically validates the input and converts types.

Dependency Injection
from fastapi import Depends

def get_token(token: str):
return token

@app.get("/secure/", dependencies=[Depends(get_token)])
def secure_route():
return {"message": "Secure Access"}
FastAPI supports dependency injection to share logic across multiple routes:
This allows for injecting shared logic like authentication or database connections.

ReDoc: /redoc
Automatic API Documentation
Swagger UI: /docs


FastAPI automatically generates interactive API documentation using Swagger
UI and ReDoc.
SwaggerUI - http://127.0.0.1:8000/docs
ReDoc - http://127.0.0.1:8000/redoc

Asynchronous Support
@app.get("/async_items/")
async def read_async_item():
return {"message": "This is asynchronous" }
FastAPI is asynchronous by nature, which means you can define async
functions to handle requests efficiently:

Conclusion
FastAPI is simple, fast, and efficient

Built-in data validation with Pydantic

Supports dependency injection for modular code

Asynchronous programming for better performance

Ideal for rapid API development with minimal effort