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.dev45.tar.gz.

File metadata

  • Download URL: swarmauri_middleware_gzipcompression-0.8.0.dev45.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","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.dev45.tar.gz
Algorithm Hash digest
SHA256 46e15cddf09dc76dafe5a3c629fb5dedf2fc7268cf105d256c0e0486f4c2c820
MD5 e6b85ef554998ebfc9df74f8331229e4
BLAKE2b-256 5177493c5c7e94418b765bd1212c4494a2110ecee2799481b01ceacda7ba758b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swarmauri_middleware_gzipcompression-0.8.0.dev45-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","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.dev45-py3-none-any.whl
Algorithm Hash digest
SHA256 53465d2249ba8bf735a559c07a6bc70358e55c48ff0e9ae382e012cc6d223a61
MD5 4d5bed6e46059b3fb2f8c58d361e2c9c
BLAKE2b-256 761f29167c4f3a56a1bd0103f3965ad0bee7b9d8e9515de726643f4d68e22f09

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