Skip to main content

Gzip compression middleware for Swarmauri

Project description

Swarmauri Logo

PyPI - Downloads GitHub Hits PyPI - Python Version PyPI - License
PyPI - swarmauri-middleware-gzipcompression


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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file swarmauri_middleware_gzipcompression-0.8.0.dev40.tar.gz.

File metadata

  • Download URL: swarmauri_middleware_gzipcompression-0.8.0.dev40.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","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}

File hashes

Hashes for swarmauri_middleware_gzipcompression-0.8.0.dev40.tar.gz
Algorithm Hash digest
SHA256 aae62737081e0233dc56fb0a7924a3d931739966b8d2d251c4980467bc52009a
MD5 46ea1ed87e327d09c401ff685643f6da
BLAKE2b-256 ce5109d90ee8c470fa0b56a060da71d98ede8cc7255d6141b7e295a2636fdaf9

See more details on using hashes here.

File details

Details for the file swarmauri_middleware_gzipcompression-0.8.0.dev40-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_middleware_gzipcompression-0.8.0.dev40-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","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}

File hashes

Hashes for swarmauri_middleware_gzipcompression-0.8.0.dev40-py3-none-any.whl
Algorithm Hash digest
SHA256 46b1803ef288bd99c13378a3b271a8e308a9ffe94c5a8ecb62c74f77a62266b0
MD5 f7c3c43bcb1bad2f161dbf3a15b6938a
BLAKE2b-256 fbbac483674f6a6670b3749e76268614717f08cedaed190b5ddda9c27afc7236

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page