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