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

File metadata

  • Download URL: swarmauri_middleware_gzipcompression-0.8.0.dev33.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","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.dev33.tar.gz
Algorithm Hash digest
SHA256 8cc1db3e7711ba04c345e817ae83cee4eec49a0530dc8d44f565f603b58f0616
MD5 4479bf21e0f6bd632beae58c37526990
BLAKE2b-256 555bacdedb6c8fe57bcf6e906a657e48e065ba770d5f090ef51aeced07ede889

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swarmauri_middleware_gzipcompression-0.8.0.dev33-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","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.dev33-py3-none-any.whl
Algorithm Hash digest
SHA256 cf3c4b8f4057f2caf3281ba01f4a3c3b88454a036a4bd8a03e97e1f29a6ecf71
MD5 02ae718093a59b8f9d9caf79839b6281
BLAKE2b-256 49643c0ad8f213a72ae83bf5ffbe03f0c055be1e278540f7794dea4fad68792b

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