Skip to content

Commit

Permalink
client: _compress function
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaume committed Sep 13, 2023
1 parent 31c2c94 commit 298eea9
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions mpcontribs-client/mpcontribs/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ def grouper(n, iterable):
yield chunk


def _compress(data):
data_json = ujson.dumps(data, indent=4).encode("utf-8")
content = gzip.compress(data_json)
return len(content), content


def get_session(session=None):
adapter_kwargs = dict(max_retries=Retry(
total=RETRIES,
Expand Down Expand Up @@ -454,17 +460,15 @@ def name(self) -> str:
return self["name"]

@classmethod
def from_data(cls, name: str, data: Union[list, dict]):
def from_data(cls, data: Union[list, dict], name: str = "attachment"):
"""Construct attachment from data dict or list
Args:
name (str): name for the attachment
data (list,dict): JSON-serializable data to go into the attachment
name (str): name for the attachment
"""
filename = name + ".json.gz"
data_json = ujson.dumps(data, indent=4).encode("utf-8")
content = gzip.compress(data_json)
size = len(content)
size, content = _compress(data)

if size > MAX_BYTES:
raise MPContribsClientError(f"{name} too large ({size} > {MAX_BYTES})!")
Expand Down

0 comments on commit 298eea9

Please sign in to comment.