1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-14 00:07:14 +03:00

always compress eddn data, comment rationale

This commit is contained in:
A_D 2022-01-24 17:21:31 +02:00
parent 2b8fe57bc7
commit 73f45e37cd
No known key found for this signature in database
GPG Key ID: 4BE9EB7DF45076C4
2 changed files with 13 additions and 1 deletions

View File

@ -219,7 +219,16 @@ class EDDN:
('message', msg['message']),
])
encoded, compressed = http.gzip(json.dumps(to_send))
# About the smallest request is going to be (newlines added for brevity):
# {"$schemaRef":"https://eddn.edcd.io/schemas/shipyard/2","header":{"softwareName":"E:D Market
# Connector Windows","softwareVersion":"5.3.0-beta4extra","uploaderID":"abcdefghijklm"},"messa
# ge":{"systemName":"delphi","stationName":"The Oracle","marketID":128782803,"timestamp":"xxxx
# -xx-xxTxx:xx:xxZ","ships":[]}}
#
# Which comes to about around 307 bytes. Lets play it safe and make our minimum 0 bytes.
# Which compresses everything
encoded, compressed = http.gzip(json.dumps(to_send, separators=(',', ':')), max_size=0)
headers: None | dict[str, str] = None
if compressed:
headers = {'Content-Encoding': 'gzip'}

View File

@ -10,6 +10,9 @@ def gzip(data: str | bytes, max_size: int = 512, encoding='utf-8') -> tuple[byte
"""
Compress the given data if the max size is greater than specified.
The default was chosen somewhat arbitrarily, see eddn.py for some more careful
work towards keeping the data almost always compressed
:param data: The data to compress
:param max_size: The max size of data, in bytes, defaults to 512
:param encoding: The encoding to use if data is a str, defaults to 'utf-8'