FastAPI

0. Quelle

https://realpython.com/api-integration-in-python/#django-rest-framework

1. FastAPI und Uvicorn installieren

2. Quellcode: Datei app.py

# app.py from fastapi import FastAPI from pydantic import BaseModel, Field app = FastAPI() def _find_next_id(): return max(country.country_id for country in countries) + 1 class Country(BaseModel): country_id: int = Field(default_factory=_find_next_id, alias="id") name: str capital: str area: int countries = [ Country(id=1, name="Thailand", capital="Bangkok", area=513120), Country(id=2, name="Australia", capital="Canberra", area=7617930), Country(id=3, name="Egypt", capital="Cairo", area=1010408), ] @app.get("/countries") async def get_countries(): return countries @app.post("/countries", status_code=201) async def add_country(country: Country): countries.append(country) return country

3. Server starten

uvicorn app:app --reload

4. Server testen

Daten

http://127.0.0.1:8000/countries

Dokumentationen