## Microservices with FastAPI [Mirek Biňas](https://bletvaska.github.io) / [**Namakaný webinár**](http://www.namakanyden.sk/)
## The Traditional Monolith Approach
## Microservices
> ...a collection of **loosely coupled specialized services** that **work in unison** to provide a comprehensive service. > -- [J. Buelta: Hands-On Docker for Microservices with Python (2019)](https://www.amazon.com/Hands-Docker-Microservices-Python-microservices/dp/1838823816)
> ...is an approach to developing a single application as a **suite of small services**, each **running in its own process** and communicating with lightweight mechanisms, often an HTTP resource API. These services are **built around business capabilities** and **independently deployable** by fully automated deployment machinery. There is a **bare minimum of centralized management** of these services, which may be written in different programming languages and use different data storage technologies. > -- [J. Lewis and M. Fowler (2014)](https://martinfowler.com/articles/microservices.html)
## REST API A **REST API** (also known as RESTful API) is an **application programming interface** (API or web API) that conforms to the constraints of **REST architectural style** and allows for interaction with RESTful web services. notes: * https://www.redhat.com/en/topics/api/what-is-a-rest-api * REST stands for representational state transfer and was created by computer scientist Roy Fielding.
[![Poetry](images/logo-poetry.jpg)](https://python-poetry.org/)
## [HTTP Status Codes](https://httpstatuses.com/)
[![FastAPI](https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png)](https://fastapi.tiangolo.com/) FastAPI framework, high performance, easy to learn, fast to code, ready for production
## Features [Fast](https://www.techempower.com/benchmarks/#section=data-r20&hw=ph&test=query&l=v2qiv3-db&a=2), Fast to code, Fewer bugs, Intuitive, Easy, Short, Robust, Standards-based
![Sebastian Ramirez](images/sebastian.ramirez.jpg) ## [Sebastian Ramirez](https://github.com/tiangolo)
Talk Python To Me Podcast · Modern and fast APIs with FastAPI

FastAPI vs Flask

      
from fastapi import FastAPI
app = FastAPI()

@app.get("/")
def hello():
    return "Hello, World!"
      
      
      
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, World!'
      
      
![URL Format Explained](images/url.format.explained.png)
### HTTP GET ```http $ http GET http://app.com/files/qtOw3Uc HTTP/1.1 200 OK content-length: 179 content-type: application/json date: Tue, 16 Nov 2021 21:52:14 GMT server: uvicorn { "created": "2021-11-16T19:47:39.701326", "downloads": 0, "filename": "high.tiff", "id": 1577, "key": "qtOw3Uc", "mimetype": "image/svg+xml", "size": 58401 } ```
## HTTP DELETE ```http $ http DELETE http://app.com/files/WjaGsbI HTTP/1.1 204 No Content date: Tue, 16 Nov 2021 23:07:18 GMT server: uvicorn ```
```json { "count": 4321, "next": "http://app.com/users/?page=2", "previous": null, "results": [ ... ] } ```
```json $ http GET "app.com/files/?size=10&page=3" { "items": [ ... ], "page": 3, "size": 10, "total": 289 } ```
``` http://app.com/users/?page=3&page_size=20 ```
[![SQLModel](https://sqlmodel.tiangolo.com/img/logo-margin/logo-margin-vector.svg)](https://sqlmodel.tiangolo.com/)
## Idempotent operation (Idempotent operation is any operation that produces same result regardless of it is called one time or hundred times)
[![Pydantic](images/logo-pydantic.png)](https://pydantic-docs.helpmanual.io/)
## Generators
![How Generators Work?](images/tape.png)
[![Python Generators](images/python.generators.webp)](https://realpython.com/introduction-to-python-generators/)
![Health Check](images/health.check.jpg)
[![Jinja](images/jinja.png)](https://jinja.palletsprojects.com/)
![Middleware Overview](images/middleware.overview.jpg)
![How Middleware Works](https://miro.medium.com/max/1400/1*ptNjzuT0m2BQ9YpQTVwVLg.png)