इसके बजाय एक अग्रदूत, . , , . . , , . , copy/paste , . , .
:
, , HTTP API, POST , , . FastAPI, ?
कौन है FastAPI?
FastAPI संक्षिप्त और काफी तेज HTTP API सर्वर बनाने के लिए एक फ्रेमवर्क है जिसमें अंतर्निहित मान्यता, क्रमांकन और अतुल्यकालिक,
जैसा कि वे कहते हैं, बॉक्स से बाहर है। उन्होंने कहा कि अन्य दो चौखटे के कंधों पर खड़ा है: FastAPI में वेब के साथ काम लगे हुए Starlette और सत्यापन करने के लिए जिम्मेदार है Pydantic ।
हार्वेस्टर हल्का हो गया है, अतिभारित नहीं है और कार्यक्षमता में पर्याप्त से अधिक है।
न्यूनतम आवश्यक
FastAPI काम करने के लिए एक ASGI सर्वर की आवश्यकता है, डिफ़ॉल्ट रूप से प्रलेखन प्रदान करता है uvcorn , के आधार पर uvloop , तथापि FastAPI भी कर सकते हैं अन्य सर्वर के साथ काम करते हैं, उदाहरण के लिए, ग hypercorn
यहाँ मेरी निर्भरताएँ हैं:
[packages]
fastapi = "*"
uvicorn = "*"
और यह पर्याप्त से अधिक है।
अधिक गहन पाठकों के लिए, लेख के अंत में एक बॉट के साथ भंडार का एक लिंक है, जहां आप विकास और परीक्षण के लिए निर्भरता को देख सकते हैं।
खैर, हमने pipenv install -d
शुरू किया!
एपीआई का निर्माण
यह ध्यान दिया जाना चाहिए कि फास्टैपी में हैंडलर डिजाइन करने का दृष्टिकोण फ्लास्क, बोतल और उनमें से हजारों के समान है। जाहिर है, लाखों मक्खियाँ गलत नहीं हो सकतीं।
पहले ही सन्निकटन में, मेरा रिलीज़ प्रोसेसिंग रूट कुछ इस तरह दिखाई दिया:
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
रिपोर्ट खत्म हो गई है, आप सभी को धन्यवाद!