Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_middleware_gzipcompression Discord

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-Response results pass through unchanged).
  • The response has a compressible media type such as application/json or any text/* content type.
  • The client opts into compression by sending an Accept-Encoding header that includes gzip.
  • 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)

Source distribution for swarmauri_middleware_gzipcompression 0.11.0.dev1
File Size Uploaded
swarmauri_middleware_gzipcompression-0.11.0.dev1.tar.gz 7.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for swarmauri_middleware_gzipcompression 0.11.0.dev1
File Interpreter ABI Platform
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}
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page