-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #783 from roboflow/enable_gzip
Add gzip support
- Loading branch information
Showing
3 changed files
with
39 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import gzip | ||
from typing import TypeVar, Union | ||
|
||
from fastapi import Request, Response | ||
from pydantic import BaseModel | ||
|
||
T = TypeVar("T", bound=BaseModel) | ||
|
||
|
||
def gzip_response_if_requested( | ||
request: Request, | ||
response: T, | ||
) -> Union[Response, T]: | ||
if "gzip" not in request.headers.get("Accept-Encoding", ""): | ||
return response | ||
response = Response( | ||
content=response.json(), | ||
) | ||
response.body = gzip.compress(response.body) | ||
response.headers["Content-Encoding"] = "gzip" | ||
response.headers["Content-Length"] = str(len(response.body)) | ||
return response |