# MRS gzip + MD5 helper (Python)
# Computes hex MD5 of the gzip payload, as required by MRS presign + complete.

import gzip
import hashlib


def gzip_json(value):
    raw = (str(value) if isinstance(value, str) else __import__('json').dumps(value)).encode('utf-8')
    gzipped = gzip.compress(raw)
    content_md5_hex = hashlib.md5(gzipped).hexdigest()
    return {
        'gzipped_bytes': gzipped,
        'content_md5_hex': content_md5_hex,
        'size_bytes': len(raw),
        'size_gzip_bytes': len(gzipped),
    }


# Example:
# helper = gzip_json({ 'hello': 'world' })
# Use helper['content_md5_hex'] in /mrs/record presign and /mrs/record/complete.
