mirror of https://github.com/tiangolo/fastapi.git
Remove code examples for Python 3.8 in `custom_request_and_route`
This commit is contained in:
parent
2679c58cbd
commit
541d3f8e4c
|
|
@ -1,35 +0,0 @@
|
||||||
import gzip
|
|
||||||
from typing import Callable, List
|
|
||||||
|
|
||||||
from fastapi import Body, FastAPI, Request, Response
|
|
||||||
from fastapi.routing import APIRoute
|
|
||||||
|
|
||||||
|
|
||||||
class GzipRequest(Request):
|
|
||||||
async def body(self) -> bytes:
|
|
||||||
if not hasattr(self, "_body"):
|
|
||||||
body = await super().body()
|
|
||||||
if "gzip" in self.headers.getlist("Content-Encoding"):
|
|
||||||
body = gzip.decompress(body)
|
|
||||||
self._body = body
|
|
||||||
return self._body
|
|
||||||
|
|
||||||
|
|
||||||
class GzipRoute(APIRoute):
|
|
||||||
def get_route_handler(self) -> Callable:
|
|
||||||
original_route_handler = super().get_route_handler()
|
|
||||||
|
|
||||||
async def custom_route_handler(request: Request) -> Response:
|
|
||||||
request = GzipRequest(request.scope, request.receive)
|
|
||||||
return await original_route_handler(request)
|
|
||||||
|
|
||||||
return custom_route_handler
|
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
app.router.route_class = GzipRoute
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/sum")
|
|
||||||
async def sum_numbers(numbers: List[int] = Body()):
|
|
||||||
return {"sum": sum(numbers)}
|
|
||||||
|
|
@ -1,36 +0,0 @@
|
||||||
import gzip
|
|
||||||
from typing import Callable, List
|
|
||||||
|
|
||||||
from fastapi import Body, FastAPI, Request, Response
|
|
||||||
from fastapi.routing import APIRoute
|
|
||||||
from typing_extensions import Annotated
|
|
||||||
|
|
||||||
|
|
||||||
class GzipRequest(Request):
|
|
||||||
async def body(self) -> bytes:
|
|
||||||
if not hasattr(self, "_body"):
|
|
||||||
body = await super().body()
|
|
||||||
if "gzip" in self.headers.getlist("Content-Encoding"):
|
|
||||||
body = gzip.decompress(body)
|
|
||||||
self._body = body
|
|
||||||
return self._body
|
|
||||||
|
|
||||||
|
|
||||||
class GzipRoute(APIRoute):
|
|
||||||
def get_route_handler(self) -> Callable:
|
|
||||||
original_route_handler = super().get_route_handler()
|
|
||||||
|
|
||||||
async def custom_route_handler(request: Request) -> Response:
|
|
||||||
request = GzipRequest(request.scope, request.receive)
|
|
||||||
return await original_route_handler(request)
|
|
||||||
|
|
||||||
return custom_route_handler
|
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
app.router.route_class = GzipRoute
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/sum")
|
|
||||||
async def sum_numbers(numbers: Annotated[List[int], Body()]):
|
|
||||||
return {"sum": sum(numbers)}
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
from typing import Callable, List
|
|
||||||
|
|
||||||
from fastapi import Body, FastAPI, HTTPException, Request, Response
|
|
||||||
from fastapi.exceptions import RequestValidationError
|
|
||||||
from fastapi.routing import APIRoute
|
|
||||||
|
|
||||||
|
|
||||||
class ValidationErrorLoggingRoute(APIRoute):
|
|
||||||
def get_route_handler(self) -> Callable:
|
|
||||||
original_route_handler = super().get_route_handler()
|
|
||||||
|
|
||||||
async def custom_route_handler(request: Request) -> Response:
|
|
||||||
try:
|
|
||||||
return await original_route_handler(request)
|
|
||||||
except RequestValidationError as exc:
|
|
||||||
body = await request.body()
|
|
||||||
detail = {"errors": exc.errors(), "body": body.decode()}
|
|
||||||
raise HTTPException(status_code=422, detail=detail)
|
|
||||||
|
|
||||||
return custom_route_handler
|
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
app.router.route_class = ValidationErrorLoggingRoute
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/")
|
|
||||||
async def sum_numbers(numbers: List[int] = Body()):
|
|
||||||
return sum(numbers)
|
|
||||||
|
|
@ -1,30 +0,0 @@
|
||||||
from typing import Callable, List
|
|
||||||
|
|
||||||
from fastapi import Body, FastAPI, HTTPException, Request, Response
|
|
||||||
from fastapi.exceptions import RequestValidationError
|
|
||||||
from fastapi.routing import APIRoute
|
|
||||||
from typing_extensions import Annotated
|
|
||||||
|
|
||||||
|
|
||||||
class ValidationErrorLoggingRoute(APIRoute):
|
|
||||||
def get_route_handler(self) -> Callable:
|
|
||||||
original_route_handler = super().get_route_handler()
|
|
||||||
|
|
||||||
async def custom_route_handler(request: Request) -> Response:
|
|
||||||
try:
|
|
||||||
return await original_route_handler(request)
|
|
||||||
except RequestValidationError as exc:
|
|
||||||
body = await request.body()
|
|
||||||
detail = {"errors": exc.errors(), "body": body.decode()}
|
|
||||||
raise HTTPException(status_code=422, detail=detail)
|
|
||||||
|
|
||||||
return custom_route_handler
|
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
app.router.route_class = ValidationErrorLoggingRoute
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/")
|
|
||||||
async def sum_numbers(numbers: Annotated[List[int], Body()]):
|
|
||||||
return sum(numbers)
|
|
||||||
|
|
@ -6,17 +6,15 @@ import pytest
|
||||||
from fastapi import Request
|
from fastapi import Request
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from tests.utils import needs_py39, needs_py310
|
from tests.utils import needs_py310
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(
|
@pytest.fixture(
|
||||||
name="client",
|
name="client",
|
||||||
params=[
|
params=[
|
||||||
pytest.param("tutorial001"),
|
pytest.param("tutorial001_py39"),
|
||||||
pytest.param("tutorial001_py39", marks=needs_py39),
|
|
||||||
pytest.param("tutorial001_py310", marks=needs_py310),
|
pytest.param("tutorial001_py310", marks=needs_py310),
|
||||||
pytest.param("tutorial001_an"),
|
pytest.param("tutorial001_an_py39"),
|
||||||
pytest.param("tutorial001_an_py39", marks=needs_py39),
|
|
||||||
pytest.param("tutorial001_an_py310", marks=needs_py310),
|
pytest.param("tutorial001_an_py310", marks=needs_py310),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -4,17 +4,15 @@ import pytest
|
||||||
from dirty_equals import IsDict, IsOneOf
|
from dirty_equals import IsDict, IsOneOf
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from tests.utils import needs_py39, needs_py310
|
from tests.utils import needs_py310
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(
|
@pytest.fixture(
|
||||||
name="client",
|
name="client",
|
||||||
params=[
|
params=[
|
||||||
pytest.param("tutorial002"),
|
pytest.param("tutorial002_py39"),
|
||||||
pytest.param("tutorial002_py39", marks=needs_py39),
|
|
||||||
pytest.param("tutorial002_py310", marks=needs_py310),
|
pytest.param("tutorial002_py310", marks=needs_py310),
|
||||||
pytest.param("tutorial002_an"),
|
pytest.param("tutorial002_an_py39"),
|
||||||
pytest.param("tutorial002_an_py39", marks=needs_py39),
|
|
||||||
pytest.param("tutorial002_an_py310", marks=needs_py310),
|
pytest.param("tutorial002_an_py310", marks=needs_py310),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ from tests.utils import needs_py310
|
||||||
@pytest.fixture(
|
@pytest.fixture(
|
||||||
name="client",
|
name="client",
|
||||||
params=[
|
params=[
|
||||||
pytest.param("tutorial003"),
|
pytest.param("tutorial003_py39"),
|
||||||
pytest.param("tutorial003_py310", marks=needs_py310),
|
pytest.param("tutorial003_py310", marks=needs_py310),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue