Skip to content

Commit 7a56ccb

Browse files
feat: support gzip_threshold
1 parent ac27ecf commit 7a56ccb

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Features
66

77
1. [#130](https://github.com/InfluxCommunity/influxdb3-python/pull/130): Remove org parameters from the example code because It is not mandatory in Influxdb3
8+
2. [#139](https://github.com/InfluxCommunity/influxdb3-python/pull/139): Supports environment variables with the same name like other clients
89

910
## 0.12.0 [2025-03-26]
1011

influxdb_client_3/write_client/_sync/api_client.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,38 @@ def set_default_header(self, header_name, header_value):
9393
self.default_headers[header_name] = header_value
9494

9595
@staticmethod
96-
def should_gzip(payload: str, enable_gzip: bool = True, gzip_threshold: int = None) -> bool:
96+
def should_gzip(payload: str, enable_gzip: bool = False, gzip_threshold: int = None) -> bool:
9797
"""
98-
Determine if the payload should be compressed with gzip.
99-
100-
Args:
101-
payload: The string content to potentially compress
102-
enable_gzip: Flag indicating if gzip compression is enabled
103-
gzip_threshold: Minimum size in bytes for compression to be applied
104-
105-
Returns:
106-
bool: True if the payload should be compressed, False otherwise
98+
Determines whether gzip compression should be applied to the given payload based
99+
on the specified conditions. This method evaluates the `enable_gzip` flag and
100+
considers the size of the payload in relation to the optional `gzip_threshold`.
101+
If `enable_gzip` is set to True and no threshold is provided, gzip compression
102+
is advised without any size condition. If a threshold is specified, compression
103+
is applied only when the size of the payload meets or exceeds the threshold.
104+
By default, no compression is performed if `enable_gzip` is False.
105+
106+
:param payload: The payload data as a string for which gzip determination is to
107+
be made.
108+
:type payload: str
109+
:param enable_gzip: A flag indicating whether gzip compression is enabled. By
110+
default, this flag is False.
111+
:type enable_gzip: bool, optional
112+
:param gzip_threshold: Optional threshold specifying the minimum size (in bytes)
113+
of the payload to trigger gzip compression. Only considered if
114+
`enable_gzip` is True.
115+
:type gzip_threshold: int, optional
116+
:return: A boolean value indicating True if gzip compression should be applied
117+
based on the payload size, the enable_gzip flag, and the gzip_threshold.
118+
:rtype: bool
107119
"""
108-
if not enable_gzip:
109-
return False
110-
111-
payload_size = len(payload.encode('utf-8'))
112-
return gzip_threshold is not None and payload_size >= gzip_threshold
120+
if enable_gzip is not False:
121+
if gzip_threshold is not None:
122+
payload_size = len(payload.encode('utf-8'))
123+
return payload_size >= gzip_threshold
124+
if enable_gzip is True:
125+
return True
126+
127+
return False
113128

114129
def __call_api(
115130
self, resource_path, method, path_params=None,

tests/test_api_client.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,22 @@ def test_api_error_headers(self):
142142

143143
def test_should_gzip(self):
144144
# Test when gzip is disabled
145-
assert not ApiClient.should_gzip("test", enable_gzip=False, gzip_threshold=1)
145+
self.assertFalse(ApiClient.should_gzip("test", enable_gzip=False, gzip_threshold=1))
146+
self.assertFalse(ApiClient.should_gzip("test", enable_gzip=False, gzip_threshold=10000))
147+
self.assertFalse(ApiClient.should_gzip("test", enable_gzip=False, gzip_threshold=None))
146148

147-
# Test when threshold is None
148-
assert not ApiClient.should_gzip("test", enable_gzip=True, gzip_threshold=None)
149+
# Test when enable_gzip is True
150+
self.assertTrue(ApiClient.should_gzip("test", enable_gzip=True, gzip_threshold=None))
151+
self.assertTrue(ApiClient.should_gzip("test", enable_gzip=True, gzip_threshold=1))
152+
self.assertFalse(ApiClient.should_gzip("test", enable_gzip=True, gzip_threshold=100000))
149153

150154
# Test payload smaller than threshold
151-
assert not ApiClient.should_gzip("test", enable_gzip=True, gzip_threshold=10000)
155+
self.assertFalse(ApiClient.should_gzip("test", enable_gzip=True, gzip_threshold=10000))
152156

153157
# Test payload larger than threshold
154158
large_payload = "x" * 10000
155-
assert ApiClient.should_gzip(large_payload, enable_gzip=True, gzip_threshold=1000)
159+
self.assertTrue(ApiClient.should_gzip(large_payload, enable_gzip=True, gzip_threshold=1000))
156160

157-
# Test exact threshold match
158-
exact_payload = "x" * 1000
159-
assert ApiClient.should_gzip(exact_payload, enable_gzip=True, gzip_threshold=1000)
161+
# Test exact threshold match and less than threshold
162+
payload = "x" * 1000
163+
self.assertTrue(ApiClient.should_gzip(payload, enable_gzip=True, gzip_threshold=1000))

0 commit comments

Comments
 (0)