7 changed files with 201 additions and 9 deletions
@ -0,0 +1,36 @@ |
|||||
|
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)} |
||||
@ -0,0 +1,36 @@ |
|||||
|
import gzip |
||||
|
from collections.abc import Callable |
||||
|
from typing import Annotated |
||||
|
|
||||
|
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: Annotated[list[int], Body()]): |
||||
|
return {"sum": sum(numbers)} |
||||
@ -0,0 +1,35 @@ |
|||||
|
import gzip |
||||
|
from typing import Annotated, Callable |
||||
|
|
||||
|
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: Annotated[list[int], Body()]): |
||||
|
return {"sum": sum(numbers)} |
||||
@ -0,0 +1,35 @@ |
|||||
|
import gzip |
||||
|
from collections.abc import Callable |
||||
|
|
||||
|
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)} |
||||
@ -0,0 +1,35 @@ |
|||||
|
import gzip |
||||
|
from typing import Callable |
||||
|
|
||||
|
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)} |
||||
Loading…
Reference in new issue