Anstelle eines Vorworts, . , , . . , , . , copy/paste , . , .
:
, , HTTP API, POST , , . FastAPI, ?
Wer ist FastAPI?
FastAPI ist ein Framework zum Erstellen präziser und relativ schneller HTTP-API-Server mit integrierter Validierung, Serialisierung und Asynchronität
. Er steht auf den Schultern der beiden anderen Frameworks: Die Arbeit mit dem Web in FastAPI hat Starlette engagiert und ist für die Validierung von Pydantic verantwortlich .
Die Erntemaschine erwies sich als leicht, nicht überlastet und mehr als ausreichend funktional.
Minimum erforderlich
Für FastAPI ist ein ASGI-Server erforderlich. Standardmäßig bietet die Dokumentation uvcorn basierend auf uvloop an . FastAPI kann jedoch auch mit anderen Servern arbeiten, z. B. c hypercorn
Hier sind meine Abhängigkeiten:
[packages]
fastapi = "*"
uvicorn = "*"
Und das ist mehr als genug.
Für gründlichere Leser gibt es am Ende des Artikels einen Link zum Repository mit einem Bot, in dem Sie die Abhängigkeiten für Entwicklung und Test anzeigen können.
Nun, wir haben pipenv install -d
angefangen!
Erstellen Sie die API
Es sollte beachtet werden, dass der Ansatz zum Entwerfen von Handlern in FastAPI extrem an den in Flask, Bottle und Tausenden von ihnen erinnert. Anscheinend können Millionen von Fliegen nicht falsch liegen.
In allererster Näherung sah meine Release-Verarbeitungsroute folgendermaßen aus:
from fastapi import FastAPI
from starlette import status
from starlette.responses import Response
from models import Body
app = FastAPI()
@app.post("/release/")
async def release(*,
body: Body,
chat_id: str = None):
await proceed_release(body, chat_id)
return Response(status_code=status.HTTP_200_OK)
, , , FastAPI Body
, chat_id
URL params
models.py
:
from datetime import datetime
from enum import Enum
from pydantic import BaseModel, HttpUrl
class Author(BaseModel):
login: str
avatar_url: HttpUrl
class Release(BaseModel):
name: str
draft: bool = False
tag_name: str
html_url: HttpUrl
author: Author
created_at: datetime
published_at: datetime = None
body: str
class Body(BaseModel):
action: str
release: Release
, Pydantic. , , , :
class Body(BaseModel):
action: str
releases: List[Release]
FastAPI . . , - — .
Pydantic , HttpUrl, URL , FastAPI . Pydantic
.
FastAPI , , , ,
— !
FastAPI , :
from fastapi import FastAPI, HTTPException, Depends
from starlette import status
from starlette.requests import Request
import settings
from router import api_router
from utils import check_auth
docs_kwargs = {}
if settings.ENVIRONMENT == 'production':
docs_kwargs = dict(docs_url=None, redoc_url=None)
app = FastAPI(**docs_kwargs)
async def check_auth_middleware(request: Request):
if settings.ENVIRONMENT in ('production', 'test'):
body = await request.body()
if not check_auth(body, request.headers.get('X-Hub-Signature', '')):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
app.include_router(api_router, dependencies=[Depends(check_auth_middleware)])
, request.body
— , . FastAPI( Starlette) , .
, FastAPI — OpenAPI Swagger/ReDoc , _/docs _/redoc .
. .
, :
from fastapi import APIRouter
from starlette import status
from starlette.responses import Response
from bot import proceed_release
from models import Body, Actions
api_router = APIRouter()
@api_router.post("/release/")
async def release(*,
body: Body,
chat_id: str = None,
release_only: bool = False):
if (body.release.draft and not release_only) \
or body.action == Actions.released:
res = await proceed_release(body, chat_id)
return Response(status_code=res.status_code)
return Response(status_code=status.HTTP_200_OK)
, HTTP API- , .
FastAPI — , , , . , (, , 2020- ?
), .
, , , , , FastAPI.
, , . — , , , , .
, , github actions
Der Bericht ist vorbei, vielen Dank!