Skip to content

Commit

Permalink
chore: Compress (gzip) request if server support it
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrykGala committed Sep 29, 2023
1 parent 8f8b6aa commit 5c8bed3
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/neptune/internal/backends/hosted_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"create_artifacts_client",
]

import gzip
import os
import platform
import zlib
from typing import (
Dict,
Tuple,
Expand Down Expand Up @@ -69,10 +69,13 @@


class GzipAdapter(HTTPAdapter):
_COMPRESSION_LEVEL = int((zlib.Z_BEST_SPEED + zlib.Z_BEST_COMPRESSION) / 2)

def send(self, request, stream=False, **kw):
if request.body is not None and not stream:
request_body = request.body if isinstance(request.body, bytes) else bytes(request.body, "utf-8")
compressed = gzip.compress(request_body)
gzip_compress = zlib.compressobj(GzipAdapter._COMPRESSION_LEVEL, zlib.DEFLATED, zlib.MAX_WBITS | 16)
compressed = gzip_compress.compress(request_body) + gzip_compress.flush()
request.prepare_body(compressed, None)
request.headers["Content-Encoding"] = "gzip"

Expand Down

0 comments on commit 5c8bed3

Please sign in to comment.