From 2379e8a111b6f9ae897b84708aff664fd2d3eccc Mon Sep 17 00:00:00 2001 From: Kevin Claytor Date: Mon, 8 Jul 2019 20:38:02 -0400 Subject: [PATCH 01/10] gzip compression working in my influx stack. Needs proper tests. --- influxdb/client.py | 13 +++++++++++ influxdb/tests/client_test.py | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/influxdb/client.py b/influxdb/client.py index 8ac557d3..8a78e10a 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -9,6 +9,7 @@ import time import random +import gzip import json import socket import requests @@ -61,6 +62,8 @@ class InfluxDBClient(object): :type proxies: dict :param path: path of InfluxDB on the server to connect, defaults to '' :type path: str + :parm gzip: use gzip content encoding to compress requests + :type gzip: bool """ def __init__(self, @@ -78,6 +81,7 @@ def __init__(self, proxies=None, pool_size=10, path='', + gzip=False, ): """Construct a new InfluxDBClient object.""" self.__host = host @@ -131,6 +135,8 @@ def __init__(self, 'Accept': 'text/plain' } + self._gzip = gzip + @property def _baseurl(self): return self.__baseurl @@ -250,6 +256,13 @@ def request(self, url, method='GET', params=None, data=None, if isinstance(data, (dict, list)): data = json.dumps(data) + if self._gzip and (data is not None): + # NOTE: zlib only supports method=DEFLATED, we need GZIP + headers.update({ + 'Content-Encoding': 'gzip', + }) + data = gzip.compress(data) + # Try to send the request more than once by default (see #103) retry = True _try = 0 diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index e4cc7e11..b1329968 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -206,6 +206,50 @@ def test_write_points(self): m.last_request.body.decode('utf-8'), ) + def test_write_gzip(self): + """Test write in TestInfluxDBClient object.""" + with requests_mock.Mocker() as m: + m.register_uri( + requests_mock.POST, + "http://localhost:8086/write", + status_code=204 + ) + cli = InfluxDBClient(database='db', gzip=True) + cli.write( + {"database": "mydb", + "retentionPolicy": "mypolicy", + "points": [{"measurement": "cpu_load_short", + "tags": {"host": "server01", + "region": "us-west"}, + "time": "2009-11-10T23:00:00Z", + "fields": {"value": 0.64}}]} + ) + + self.assertEqual( + m.last_request.body, + b"cpu_load_short,host=server01,region=us-west " + b"value=0.64 1257894000000000000\n", + ) + + def test_write_points_gzip(self): + """Test write points for TestInfluxDBClient object.""" + with requests_mock.Mocker() as m: + m.register_uri( + requests_mock.POST, + "http://localhost:8086/write", + status_code=204 + ) + + cli = InfluxDBClient(database='db', gzip=True) + cli.write_points( + self.dummy_points, + ) + self.assertEqual( + 'cpu_load_short,host=server01,region=us-west ' + 'value=0.64 1257894000123456000\n', + m.last_request.body.decode('utf-8'), + ) + def test_write_points_toplevel_attributes(self): """Test write points attrs for TestInfluxDBClient object.""" with requests_mock.Mocker() as m: From b76c255b508a36a6e7d0cd677f7a3c63196dca99 Mon Sep 17 00:00:00 2001 From: Kevin Claytor Date: Thu, 11 Jul 2019 12:03:02 -0400 Subject: [PATCH 02/10] Also gzip data from server, slightly more straightforward data handling. --- influxdb/client.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/influxdb/client.py b/influxdb/client.py index 8a78e10a..d34d8c42 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -256,12 +256,15 @@ def request(self, url, method='GET', params=None, data=None, if isinstance(data, (dict, list)): data = json.dumps(data) - if self._gzip and (data is not None): - # NOTE: zlib only supports method=DEFLATED, we need GZIP + if self._gzip: + # Allow us to receive gzip'd data (requests will decompress) headers.update({ + 'Accept-Encoding': 'gzip', 'Content-Encoding': 'gzip', - }) - data = gzip.compress(data) + }) + if data is not None: + # NOTE: zlib only supports method=DEFLATED, we need method=GZIP + data = gzip.compress(data) # Try to send the request more than once by default (see #103) retry = True From 96924950e1ddbb4bc21ca09730849556a6071479 Mon Sep 17 00:00:00 2001 From: Kevin Claytor Date: Wed, 17 Jul 2019 23:15:03 -0400 Subject: [PATCH 03/10] Adding in test cases. --- influxdb/tests/client_test.py | 15 +++++--- influxdb/tests/server_tests/base.py | 38 +++++++++++++++++++ .../server_tests/client_test_with_server.py | 22 +++++++++++ 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index b1329968..5cd38af8 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -24,6 +24,7 @@ import unittest import warnings +import gzip import json import mock import requests @@ -227,8 +228,10 @@ def test_write_gzip(self): self.assertEqual( m.last_request.body, - b"cpu_load_short,host=server01,region=us-west " - b"value=0.64 1257894000000000000\n", + gzip.compress( + b"cpu_load_short,host=server01,region=us-west " + b"value=0.64 1257894000000000000\n" + ), ) def test_write_points_gzip(self): @@ -245,9 +248,11 @@ def test_write_points_gzip(self): self.dummy_points, ) self.assertEqual( - 'cpu_load_short,host=server01,region=us-west ' - 'value=0.64 1257894000123456000\n', - m.last_request.body.decode('utf-8'), + m.last_request.body, + gzip.compress( + b'cpu_load_short,host=server01,region=us-west ' + b'value=0.64 1257894000123456000\n' + ), ) def test_write_points_toplevel_attributes(self): diff --git a/influxdb/tests/server_tests/base.py b/influxdb/tests/server_tests/base.py index f4bd3ff9..ae6bbc6e 100644 --- a/influxdb/tests/server_tests/base.py +++ b/influxdb/tests/server_tests/base.py @@ -36,6 +36,15 @@ def _setup_influxdb_server(inst): database='db') +def _setup_gzip_client(inst): + inst.cli = InfluxDBClient('localhost', + inst.influxd_inst.http_port, + 'root', + '', + database='db', + gzip=True) + + def _teardown_influxdb_server(inst): remove_tree = sys.exc_info() == (None, None, None) inst.influxd_inst.close(remove_tree=remove_tree) @@ -82,3 +91,32 @@ def tearDownClass(cls): def tearDown(self): """Deconstruct an instance of ManyTestCasesWithServerMixin.""" self.cli.drop_database('db') + + +class SingleTestCaseWithServerGzipMixin(object): + """Define the single testcase with server with gzip client mixin. + + Same as the SingleTestCaseWithServerGzipMixin but the InfluxDBClient has + gzip=True + """ + tearDown = _teardown_influxdb_server + + @classmethod + def setUp(cls): + """Set up an instance of the SingleTestCaseWithServerGzipMixin.""" + _setup_influxdb_server(cls) + _setup_gzip_client(cls) + + +class ManyTestCasesWithServerGzipMixin(object): + """Define the many testcase with server with gzip client mixin. + + Same as the ManyTestCasesWithServerMixin but the InfluxDBClient has + gzip=True. + """ + + @classmethod + def setUpClass(cls): + """Set up an instance of the ManyTestCasesWithServerGzipMixin.""" + _setup_influxdb_server(cls) + _setup_gzip_client(cls) diff --git a/influxdb/tests/server_tests/client_test_with_server.py b/influxdb/tests/server_tests/client_test_with_server.py index fda3f720..0d87e49a 100644 --- a/influxdb/tests/server_tests/client_test_with_server.py +++ b/influxdb/tests/server_tests/client_test_with_server.py @@ -26,6 +26,8 @@ from influxdb.tests import skip_if_pypy, using_pypy, skip_server_tests from influxdb.tests.server_tests.base import ManyTestCasesWithServerMixin from influxdb.tests.server_tests.base import SingleTestCaseWithServerMixin +from influxdb.tests.server_tests.base import ManyTestCasesWithServerGzipMixin +from influxdb.tests.server_tests.base import SingleTestCaseWithServerGzipMixin # By default, raise exceptions on warnings warnings.simplefilter('error', FutureWarning) @@ -855,3 +857,23 @@ def test_write_points_udp(self): ], list(rsp['cpu_load_short']) ) + + +# Run the tests again, but with gzip enabled this time +@skip_server_tests +class GzipSimpleTests(SimpleTests, SingleTestCaseWithServerGzipMixin): + """Repeat the simple tests with InfluxDBClient where gzip=True.""" + pass + + +@skip_server_tests +class GzipCommonTests(CommonTests, ManyTestCasesWithServerGzipMixin): + """Repeat the common tests with InfluxDBClient where gzip=True.""" + pass + + +@skip_server_tests +class GzipUdpTests(UdpTests, ManyTestCasesWithServerGzipMixin): + """Repeat the UDP tests with InfluxDBClient where gzip=True.""" + """Define a class to test UDP series.""" + pass From 05f81b3cc7354d956acf9d3b258bcaedec714a48 Mon Sep 17 00:00:00 2001 From: Kevin Claytor Date: Thu, 18 Jul 2019 09:00:03 -0400 Subject: [PATCH 04/10] Switching back to zlib with gzip headers. --- influxdb/client.py | 9 ++++++--- influxdb/tests/client_test.py | 10 +++++++--- influxdb/tests/server_tests/base.py | 1 + influxdb/tests/server_tests/client_test_with_server.py | 3 +++ 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/influxdb/client.py b/influxdb/client.py index d34d8c42..a5c65f9d 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -9,7 +9,7 @@ import time import random -import gzip +import zlib import json import socket import requests @@ -136,6 +136,10 @@ def __init__(self, } self._gzip = gzip + if self._gzip: + # Create the zlib compression object to do the compression later. + # We need the gzip headers which is done with "zlib.MAX_WBITS | 16" + self._gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16) @property def _baseurl(self): @@ -263,8 +267,7 @@ def request(self, url, method='GET', params=None, data=None, 'Content-Encoding': 'gzip', }) if data is not None: - # NOTE: zlib only supports method=DEFLATED, we need method=GZIP - data = gzip.compress(data) + data = self._gzip_compressor.compress(data) # Try to send the request more than once by default (see #103) retry = True diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index 5cd38af8..0ee01f51 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -24,7 +24,7 @@ import unittest import warnings -import gzip +import zlib import json import mock import requests @@ -215,6 +215,7 @@ def test_write_gzip(self): "http://localhost:8086/write", status_code=204 ) + cli = InfluxDBClient(database='db', gzip=True) cli.write( {"database": "mydb", @@ -226,9 +227,10 @@ def test_write_gzip(self): "fields": {"value": 0.64}}]} ) + gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16) self.assertEqual( m.last_request.body, - gzip.compress( + gzip_compressor.compress( b"cpu_load_short,host=server01,region=us-west " b"value=0.64 1257894000000000000\n" ), @@ -247,9 +249,11 @@ def test_write_points_gzip(self): cli.write_points( self.dummy_points, ) + + gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16) self.assertEqual( m.last_request.body, - gzip.compress( + gzip_compressor.compress( b'cpu_load_short,host=server01,region=us-west ' b'value=0.64 1257894000123456000\n' ), diff --git a/influxdb/tests/server_tests/base.py b/influxdb/tests/server_tests/base.py index ae6bbc6e..d3556257 100644 --- a/influxdb/tests/server_tests/base.py +++ b/influxdb/tests/server_tests/base.py @@ -99,6 +99,7 @@ class SingleTestCaseWithServerGzipMixin(object): Same as the SingleTestCaseWithServerGzipMixin but the InfluxDBClient has gzip=True """ + tearDown = _teardown_influxdb_server @classmethod diff --git a/influxdb/tests/server_tests/client_test_with_server.py b/influxdb/tests/server_tests/client_test_with_server.py index 0d87e49a..b168f350 100644 --- a/influxdb/tests/server_tests/client_test_with_server.py +++ b/influxdb/tests/server_tests/client_test_with_server.py @@ -863,12 +863,14 @@ def test_write_points_udp(self): @skip_server_tests class GzipSimpleTests(SimpleTests, SingleTestCaseWithServerGzipMixin): """Repeat the simple tests with InfluxDBClient where gzip=True.""" + pass @skip_server_tests class GzipCommonTests(CommonTests, ManyTestCasesWithServerGzipMixin): """Repeat the common tests with InfluxDBClient where gzip=True.""" + pass @@ -876,4 +878,5 @@ class GzipCommonTests(CommonTests, ManyTestCasesWithServerGzipMixin): class GzipUdpTests(UdpTests, ManyTestCasesWithServerGzipMixin): """Repeat the UDP tests with InfluxDBClient where gzip=True.""" """Define a class to test UDP series.""" + pass From 248a3ac6f6193814434a9924112a32abcaac49b0 Mon Sep 17 00:00:00 2001 From: Kevin Claytor Date: Thu, 18 Jul 2019 09:48:25 -0400 Subject: [PATCH 05/10] flake8 compatibility --- influxdb/client.py | 6 +++++- influxdb/tests/client_test.py | 12 ++++++++++-- .../tests/server_tests/client_test_with_server.py | 3 +-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/influxdb/client.py b/influxdb/client.py index a5c65f9d..72b6fe95 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -139,7 +139,11 @@ def __init__(self, if self._gzip: # Create the zlib compression object to do the compression later. # We need the gzip headers which is done with "zlib.MAX_WBITS | 16" - self._gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16) + self._gzip_compressor = zlib.compressobj( + 9, + zlib.DEFLATED, + zlib.MAX_WBITS | 16 + ) @property def _baseurl(self): diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index 0ee01f51..136c9504 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -227,7 +227,11 @@ def test_write_gzip(self): "fields": {"value": 0.64}}]} ) - gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16) + gzip_compressor = zlib.compressobj( + 9, + zlib.DEFLATED, + zlib.MAX_WBITS | 16 + ) self.assertEqual( m.last_request.body, gzip_compressor.compress( @@ -250,7 +254,11 @@ def test_write_points_gzip(self): self.dummy_points, ) - gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16) + gzip_compressor = zlib.compressobj( + 9, + zlib.DEFLATED, + zlib.MAX_WBITS | 16 + ) self.assertEqual( m.last_request.body, gzip_compressor.compress( diff --git a/influxdb/tests/server_tests/client_test_with_server.py b/influxdb/tests/server_tests/client_test_with_server.py index b168f350..148e430f 100644 --- a/influxdb/tests/server_tests/client_test_with_server.py +++ b/influxdb/tests/server_tests/client_test_with_server.py @@ -877,6 +877,5 @@ class GzipCommonTests(CommonTests, ManyTestCasesWithServerGzipMixin): @skip_server_tests class GzipUdpTests(UdpTests, ManyTestCasesWithServerGzipMixin): """Repeat the UDP tests with InfluxDBClient where gzip=True.""" - """Define a class to test UDP series.""" - + pass From f98e48dd716ebf153b05d3c34274507936011bf0 Mon Sep 17 00:00:00 2001 From: Kevin Claytor Date: Tue, 17 Sep 2019 23:25:42 -0400 Subject: [PATCH 06/10] Move parameter into correct position. per review --- influxdb/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/influxdb/client.py b/influxdb/client.py index 467dbda4..808aca08 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -67,9 +67,9 @@ class InfluxDBClient(object): as a single file containing the private key and the certificate, or as a tuple of both files’ paths, defaults to None :type cert: str - :raises ValueError: if cert is provided but ssl is disabled (set to False) - :parm gzip: use gzip content encoding to compress requests + :param gzip: use gzip content encoding to compress requests :type gzip: bool + :raises ValueError: if cert is provided but ssl is disabled (set to False) """ def __init__(self, From d8ad79e9806c43ec724fb43845e1b8316605f951 Mon Sep 17 00:00:00 2001 From: Kevin Claytor Date: Mon, 23 Sep 2019 23:31:17 -0400 Subject: [PATCH 07/10] Switching back to gzip for the headers. --- influxdb/client.py | 15 +++++---------- influxdb/tests/client_test.py | 16 +++------------- influxdb/tests/server_tests/base.py | 12 ++++++++++-- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/influxdb/client.py b/influxdb/client.py index 808aca08..82f21c23 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -9,7 +9,7 @@ import time import random -import zlib +import gzip import json import socket import requests @@ -151,14 +151,6 @@ def __init__(self, } self._gzip = gzip - if self._gzip: - # Create the zlib compression object to do the compression later. - # We need the gzip headers which is done with "zlib.MAX_WBITS | 16" - self._gzip_compressor = zlib.compressobj( - 9, - zlib.DEFLATED, - zlib.MAX_WBITS | 16 - ) @property def _baseurl(self): @@ -281,12 +273,15 @@ def request(self, url, method='GET', params=None, data=None, if self._gzip: # Allow us to receive gzip'd data (requests will decompress) + # as well as write it out headers.update({ 'Accept-Encoding': 'gzip', 'Content-Encoding': 'gzip', }) + # Note you may get better performance with zlib (speed, ratio) + # but the headers are such that Influx rejects them. if data is not None: - data = self._gzip_compressor.compress(data) + data = gzip.compress(data, compresslevel=9) # Try to send the request more than once by default (see #103) retry = True diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index c87b191f..474e28f6 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -24,7 +24,7 @@ import unittest import warnings -import zlib +import gzip import json import mock import requests @@ -235,14 +235,9 @@ def test_write_gzip(self): "fields": {"value": 0.64}}]} ) - gzip_compressor = zlib.compressobj( - 9, - zlib.DEFLATED, - zlib.MAX_WBITS | 16 - ) self.assertEqual( m.last_request.body, - gzip_compressor.compress( + gzip.compress( b"cpu_load_short,host=server01,region=us-west " b"value=0.64 1257894000000000000\n" ), @@ -262,14 +257,9 @@ def test_write_points_gzip(self): self.dummy_points, ) - gzip_compressor = zlib.compressobj( - 9, - zlib.DEFLATED, - zlib.MAX_WBITS | 16 - ) self.assertEqual( m.last_request.body, - gzip_compressor.compress( + gzip.compress( b'cpu_load_short,host=server01,region=us-west ' b'value=0.64 1257894000123456000\n' ), diff --git a/influxdb/tests/server_tests/base.py b/influxdb/tests/server_tests/base.py index e03874c9..45a9ec80 100644 --- a/influxdb/tests/server_tests/base.py +++ b/influxdb/tests/server_tests/base.py @@ -107,14 +107,17 @@ class SingleTestCaseWithServerGzipMixin(object): gzip=True """ - tearDown = _teardown_influxdb_server - @classmethod def setUp(cls): """Set up an instance of the SingleTestCaseWithServerGzipMixin.""" _setup_influxdb_server(cls) _setup_gzip_client(cls) + @classmethod + def tearDown(cls): + """Tear down an instance of the SingleTestCaseWithServerMixin.""" + _teardown_influxdb_server(cls) + class ManyTestCasesWithServerGzipMixin(object): """Define the many testcase with server with gzip client mixin. @@ -128,3 +131,8 @@ def setUpClass(cls): """Set up an instance of the ManyTestCasesWithServerGzipMixin.""" _setup_influxdb_server(cls) _setup_gzip_client(cls) + + @classmethod + def tearDown(cls): + """Tear down an instance of the SingleTestCaseWithServerMixin.""" + _teardown_influxdb_server(cls) From 3345b44fcc8d6eb885ee98ef479575eb32ca2505 Mon Sep 17 00:00:00 2001 From: Kevin Claytor Date: Tue, 24 Sep 2019 10:07:15 -0400 Subject: [PATCH 08/10] Fixing python 2.7 compatability with gzip. --- influxdb/client.py | 9 ++++++++- influxdb/tests/client_test.py | 24 ++++++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/influxdb/client.py b/influxdb/client.py index 82f21c23..1702feb4 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -9,6 +9,7 @@ import time import random +import io import gzip import json import socket @@ -281,7 +282,13 @@ def request(self, url, method='GET', params=None, data=None, # Note you may get better performance with zlib (speed, ratio) # but the headers are such that Influx rejects them. if data is not None: - data = gzip.compress(data, compresslevel=9) + # Python 3+ + # data = gzip.compress(data, compresslevel=9) + # But for Py 2.7 compatability; + compressed = io.BytesIO() + with gzip.GzipFile(compresslevel=9, fileobj=compressed, mode='w') as f: + f.write(data) + data = compressed.getvalue() # Try to send the request more than once by default (see #103) retry = True diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index 474e28f6..c321307f 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -24,6 +24,7 @@ import unittest import warnings +import io import gzip import json import mock @@ -235,12 +236,16 @@ def test_write_gzip(self): "fields": {"value": 0.64}}]} ) - self.assertEqual( - m.last_request.body, - gzip.compress( + compressed = io.BytesIO() + with gzip.GzipFile(compresslevel=9, fileobj=compressed, mode='w') as f: + f.write( b"cpu_load_short,host=server01,region=us-west " b"value=0.64 1257894000000000000\n" - ), + ) + + self.assertEqual( + m.last_request.body, + compressed.getvalue(), ) def test_write_points_gzip(self): @@ -257,12 +262,15 @@ def test_write_points_gzip(self): self.dummy_points, ) - self.assertEqual( - m.last_request.body, - gzip.compress( + compressed = io.BytesIO() + with gzip.GzipFile(compresslevel=9, fileobj=compressed, mode='w') as f: + f.write( b'cpu_load_short,host=server01,region=us-west ' b'value=0.64 1257894000123456000\n' - ), + ) + self.assertEqual( + m.last_request.body, + compressed.getvalue(), ) def test_write_points_toplevel_attributes(self): From 15f07be354528c039a45e1af0e6ebfb07f3d3549 Mon Sep 17 00:00:00 2001 From: Kevin Claytor Date: Tue, 24 Sep 2019 15:13:33 -0400 Subject: [PATCH 09/10] flake8 compatibility. --- influxdb/client.py | 12 ++++++------ influxdb/tests/client_test.py | 12 ++++++++++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/influxdb/client.py b/influxdb/client.py index 1702feb4..f0d9d3e7 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -279,14 +279,14 @@ def request(self, url, method='GET', params=None, data=None, 'Accept-Encoding': 'gzip', 'Content-Encoding': 'gzip', }) - # Note you may get better performance with zlib (speed, ratio) - # but the headers are such that Influx rejects them. if data is not None: - # Python 3+ - # data = gzip.compress(data, compresslevel=9) - # But for Py 2.7 compatability; + # For Py 2.7 compatability use Gzipfile compressed = io.BytesIO() - with gzip.GzipFile(compresslevel=9, fileobj=compressed, mode='w') as f: + with gzip.GzipFile( + compresslevel=9, + fileobj=compressed, + mode='w' + ) as f: f.write(data) data = compressed.getvalue() diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index c321307f..af719af6 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -237,7 +237,11 @@ def test_write_gzip(self): ) compressed = io.BytesIO() - with gzip.GzipFile(compresslevel=9, fileobj=compressed, mode='w') as f: + with gzip.GzipFile( + compresslevel=9, + fileobj=compressed, + mode='w' + ) as f: f.write( b"cpu_load_short,host=server01,region=us-west " b"value=0.64 1257894000000000000\n" @@ -263,7 +267,11 @@ def test_write_points_gzip(self): ) compressed = io.BytesIO() - with gzip.GzipFile(compresslevel=9, fileobj=compressed, mode='w') as f: + with gzip.GzipFile( + compresslevel=9, + fileobj=compressed, + mode='w' + ) as f: f.write( b'cpu_load_short,host=server01,region=us-west ' b'value=0.64 1257894000123456000\n' From 95c1d60de9a84b998acd3b640880eb1bc113c3d6 Mon Sep 17 00:00:00 2001 From: Kevin Claytor Date: Thu, 26 Sep 2019 18:37:51 -0400 Subject: [PATCH 10/10] flake8 testing --- influxdb/client.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/influxdb/client.py b/influxdb/client.py index f0d9d3e7..6653bbca 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -273,8 +273,7 @@ def request(self, url, method='GET', params=None, data=None, data = json.dumps(data) if self._gzip: - # Allow us to receive gzip'd data (requests will decompress) - # as well as write it out + # Receive and send compressed data headers.update({ 'Accept-Encoding': 'gzip', 'Content-Encoding': 'gzip',