This release is a pre-release and may not be stable for production use.
swarmauri_middleware_gzipcompression
Middleware for adding gzip compression to FastAPI responses.
Purpose
This package provides a middleware that automatically compresses outgoing responses using gzip encoding. It ensures that responses are only compressed when supported by the client and when the content type is appropriate for compression.
Installation
pip
pip install swarmauri_middleware_gzipcompression
Poetry
poetry add swarmauri_middleware_gzipcompression
uv
uv pip install swarmauri_middleware_gzipcompression
How it works
The middleware mirrors FastAPI's BaseHTTPMiddleware contract and can be plugged into an application using the standard @app.middleware("http") hook. Compression is only applied when all of the following are true:
- The downstream component returns a
fastapi.Response(non-Responseresults pass through unchanged). - The response has a compressible media type such as
application/jsonor anytext/*content type. - The client opts into compression by sending an
Accept-Encodingheader that includesgzip. - The response is not already marked as gzip encoded.
Example
The snippet below simulates FastAPI's middleware pipeline and demonstrates how gzip compression is applied when the client opts in to receive gzip-encoded responses.
import asyncio
import gzip
from fastapi import Request, Response
from swarmauri_middleware_gzipcompression import GzipCompressionMiddleware
middleware = GzipCompressionMiddleware()
async def _handle_request() -> None:
scope = {
"type": "http",
"method": "GET",
"headers": [(b"accept-encoding", b"gzip")],
}
async def receive() -> dict:
return {"type": "http.request", "body": b""}
request = Request(scope, receive=receive)
async def call_next(_: Request) -> Response:
return Response(
content='{"message":"Hello, gzip!"}',
media_type="application/json",
)
response = await middleware.dispatch(request, call_next)
assert response.headers.get("Content-Encoding") == "gzip"
body = gzip.decompress(response.body).decode("utf-8")
assert body == '{"message":"Hello, gzip!"}'
def run_example() -> None:
asyncio.run(_handle_request())
if __name__ == "__main__":
run_example()
Want to help?
If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.
Metadata
Release files for swarmauri_middleware_gzipcompression 0.11.0.dev1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| swarmauri_middleware_gzipcompression-0.11.0.dev1.tar.gz | 7.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| swarmauri_middleware_gzipcompression-0.11.0.dev1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.8 kB
Release files / swarmauri_middleware_gzipcompression-0.11.0.dev1.tar.gz
| Download URL | swarmauri_middleware_gzipcompression-0.11.0.dev1.tar.gz |
|---|---|
| Size | 7.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
411d72d8246c36ecf3175799168877f5942c5a54cb703f2b45b67c618f082d0d
|
|
BLAKE2b-256 checksum How to use checksums |
0ab9de4f682a978e7b796329acc2bd874209f7966cf4180fec765ccf4d1873eb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / swarmauri_middleware_gzipcompression-0.11.0.dev1-py3-none-any.whl
| Download URL | swarmauri_middleware_gzipcompression-0.11.0.dev1-py3-none-any.whl |
|---|---|
| Size | 9.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
184be79d2e56da8598770a0ac2b4a6c7edb20764ccd7d537778755d84df3bba4
|
|
BLAKE2b-256 checksum How to use checksums |
725d8d36a6c234b66c75a355a78bdec7045a955d8608e1a0f4637c6591a89e7c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|