Coverage for docs_src / custom_request_and_route / tutorial001_an_py310.py: 100%
25 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1import gzip 1abcdef
2from collections.abc import Callable 1abcdef
3from typing import Annotated 1abcdef
5from fastapi import Body, FastAPI, Request, Response 1abcdef
6from fastapi.routing import APIRoute 1abcdef
9class GzipRequest(Request): 1abcdef
10 async def body(self) -> bytes: 1abcdef
11 if not hasattr(self, "_body"): 1kghlimj
12 body = await super().body() 1kghlimj
13 if "gzip" in self.headers.getlist("Content-Encoding"): 1kghlimj
14 body = gzip.decompress(body) 1ghij
15 self._body = body 1kghlimj
16 return self._body 1kghlimj
19class GzipRoute(APIRoute): 1abcdef
20 def get_route_handler(self) -> Callable: 1abcdef
21 original_route_handler = super().get_route_handler() 1abncdeopfq
23 async def custom_route_handler(request: Request) -> Response: 1abncdeopfq
24 request = GzipRequest(request.scope, request.receive) 1kgrhlismjt
25 return await original_route_handler(request) 1kgrhlismjt
27 return custom_route_handler 1abncdeopfq
30app = FastAPI() 1abcdef
31app.router.route_class = GzipRoute 1abcdef
34@app.post("/sum") 1abcdef
35async def sum_numbers(numbers: Annotated[list[int], Body()]): 1abcdef
36 return {"sum": sum(numbers)} 1kghlimj