From 511f0c7bc1c9ff7f54d3278edd1193d81119ede9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Gr=C3=BCb?= Date: Thu, 8 Oct 2020 16:21:39 +0200 Subject: [PATCH 1/5] Added headers to individual queries Query headers overwrite overwrite client headers --- examples/tutorial_authorization.py | 12 +++++++++ influxdb/client.py | 6 +++-- influxdb/tests/client_test.py | 42 ++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/examples/tutorial_authorization.py b/examples/tutorial_authorization.py index 9d9a800f..a72f479f 100644 --- a/examples/tutorial_authorization.py +++ b/examples/tutorial_authorization.py @@ -16,6 +16,17 @@ def main(token='my-token'): print("Successfully connected to InfluxDB: " + version) pass +def auth_per_query(token="my-token"): + client = InfluxDBClient(username=None, password=None, headers=None) + + print(f"Use authorization token {token}") + + print(f"""Response for query with token: + {client.query( + f'SHOW DATABASES', + headers={"Authorization": token})}""") + pass + def parse_args(): """Parse the args from main.""" @@ -30,3 +41,4 @@ def parse_args(): if __name__ == '__main__': args = parse_args() main(token=args.token) + auth_per_query(token=args.token) diff --git a/influxdb/client.py b/influxdb/client.py index 51a64ac3..02bd7d41 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -444,7 +444,8 @@ def query(self, raise_errors=True, chunked=False, chunk_size=0, - method="GET"): + method="GET", + headers=None): """Send a query to InfluxDB. .. danger:: @@ -524,7 +525,8 @@ def query(self, params=params, data=None, stream=chunked, - expected_response_code=expected_response_code + expected_response_code=expected_response_code, + headers=headers ) data = response._msgpack diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index e511ca9b..4f37809e 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -1497,7 +1497,49 @@ def test_auth_token(self): cli.ping() self.assertEqual(m.last_request.headers["Authorization"], "my-token") + + def test_query_with_auth_token(self): + """ Test query with custom authorization header """ + with requests_mock.Mocker() as m: + m.register_uri( + requests_mock.GET, + "http://localhost:8086/query", + status_code=200, + text="{}", + headers={"X-Influxdb-Version": "1.2.3"} + ) + cli = InfluxDBClient(username=None, password=None, headers=None) + cli.query("SELECT * FROM foo") + self.assertEqual(m.last_request.headers.get("Authorization"), + None) + + cli.query("SELET * FROM foo", headers={"Authorization": "my-token"}) + self.assertEqual(m.last_request.headers.get("Authorization"), + "my-token") + def test_query_header_overwrites_client_header(self): + """ Test query with custom authorization header + to overwrite defaults specified on client init""" + + with requests_mock.Mocker() as m: + m.register_uri( + requests_mock.GET, + "http://localhost:8086/query", + status_code=200, + text="{}", + headers={"X-Influxdb-Version": "1.2.3"} + ) + cli = InfluxDBClient(username=None, password=None, headers={ + "Authorization": "client-token", + "header-to-drop": "there-is-no-merge-for-headers-from-client-and-query"}) + cli.query("SELECT * FROM foo") + self.assertEqual(m.last_request.headers.get("Authorization"), + "client-token") + + cli.query("SELET * FROM foo", headers={"Authorization": "query-token"}) + self.assertEqual(m.last_request.headers.get("Authorization"), + "query-token") + self.assertFalse("header-to-drop" in m.last_request.headers) class FakeClient(InfluxDBClient): """Set up a fake client instance of InfluxDBClient.""" From f38f6b5ccbf62596aed157af7577fbe3af45ac7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Gr=C3=BCb?= Date: Fri, 9 Oct 2020 09:27:18 +0200 Subject: [PATCH 2/5] Fixed flake8 and pydocstrings flake8 for ./influxdb pydocstrings for ./influxdb and ./examples --- examples/tutorial_authorization.py | 4 +++- influxdb/tests/client_test.py | 30 +++++++++++++++++------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/examples/tutorial_authorization.py b/examples/tutorial_authorization.py index a72f479f..07aa84a5 100644 --- a/examples/tutorial_authorization.py +++ b/examples/tutorial_authorization.py @@ -16,9 +16,11 @@ def main(token='my-token'): print("Successfully connected to InfluxDB: " + version) pass + def auth_per_query(token="my-token"): + """Set headers per query, most prevelantly for authorization headers.""" client = InfluxDBClient(username=None, password=None, headers=None) - + print(f"Use authorization token {token}") print(f"""Response for query with token: diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index 4f37809e..73d0d05c 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -1497,9 +1497,9 @@ def test_auth_token(self): cli.ping() self.assertEqual(m.last_request.headers["Authorization"], "my-token") - + def test_query_with_auth_token(self): - """ Test query with custom authorization header """ + """Test query with custom authorization header.""" with requests_mock.Mocker() as m: m.register_uri( requests_mock.GET, @@ -1510,17 +1510,16 @@ def test_query_with_auth_token(self): ) cli = InfluxDBClient(username=None, password=None, headers=None) cli.query("SELECT * FROM foo") - self.assertEqual(m.last_request.headers.get("Authorization"), + self.assertEqual(m.last_request.headers.get("Authorization"), None) - - cli.query("SELET * FROM foo", headers={"Authorization": "my-token"}) - self.assertEqual(m.last_request.headers.get("Authorization"), + + cli.query("SELET * FROM foo", + headers={"Authorization": "my-token"}) + self.assertEqual(m.last_request.headers.get("Authorization"), "my-token") def test_query_header_overwrites_client_header(self): - """ Test query with custom authorization header - to overwrite defaults specified on client init""" - + """Custom query authorization header must overwrite init headers.""" with requests_mock.Mocker() as m: m.register_uri( requests_mock.GET, @@ -1531,15 +1530,20 @@ def test_query_header_overwrites_client_header(self): ) cli = InfluxDBClient(username=None, password=None, headers={ "Authorization": "client-token", - "header-to-drop": "there-is-no-merge-for-headers-from-client-and-query"}) + "header-to-drop": "not-only-is-Authorization-overwritten", + "header-to-drop2": "headers-of-client-are-all-overwritten"}) cli.query("SELECT * FROM foo") - self.assertEqual(m.last_request.headers.get("Authorization"), + self.assertEqual(m.last_request.headers.get("Authorization"), "client-token") - cli.query("SELET * FROM foo", headers={"Authorization": "query-token"}) - self.assertEqual(m.last_request.headers.get("Authorization"), + cli.query("SELET * FROM foo", + headers={"Authorization": "query-token"}) + + self.assertEqual(m.last_request.headers.get("Authorization"), "query-token") self.assertFalse("header-to-drop" in m.last_request.headers) + self.assertFalse("header-to-drop2" in m.last_request.headers) + class FakeClient(InfluxDBClient): """Set up a fake client instance of InfluxDBClient.""" From b4d16bd35d0111a049d678b196c04dd409db356c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Gr=C3=BCb?= Date: Fri, 9 Oct 2020 10:24:48 +0200 Subject: [PATCH 3/5] Added docstring indicator for gzip --- influxdb/tests/client_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index 73d0d05c..bca6d539 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -253,7 +253,7 @@ def test_write_gzip(self): ) def test_write_points_gzip(self): - """Test write points for TestInfluxDBClient object.""" + """Test write points for TestInfluxDBClient object with gzip.""" with requests_mock.Mocker() as m: m.register_uri( requests_mock.POST, From 628b61701bd3573bca7b08c920415cdaad9ac828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Gr=C3=BCb?= Date: Fri, 9 Oct 2020 15:11:29 +0200 Subject: [PATCH 4/5] Fixed gzip tests by comparing decompressed values --- influxdb/tests/client_test.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index e511ca9b..0c017f3f 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -248,8 +248,8 @@ def test_write_gzip(self): ) self.assertEqual( - m.last_request.body, - compressed.getvalue(), + gzip.decompress(m.last_request.body), + gzip.decompress(compressed.getvalue()), ) def test_write_points_gzip(self): @@ -276,9 +276,10 @@ def test_write_points_gzip(self): b'cpu_load_short,host=server01,region=us-west ' b'value=0.64 1257894000123456000\n' ) + self.assertEqual( - m.last_request.body, - compressed.getvalue(), + gzip.decompress(m.last_request.body), + gzip.decompress(compressed.getvalue()), ) def test_write_points_toplevel_attributes(self): From ed548b408ebce9fd655aa5ff52625d439b7cdf6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Gr=C3=BCb?= Date: Fri, 9 Oct 2020 16:05:32 +0200 Subject: [PATCH 5/5] Python 2.7 compliant test version --- influxdb/tests/client_test.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index 0c017f3f..7f1091cb 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -246,10 +246,11 @@ def test_write_gzip(self): b"cpu_load_short,host=server01,region=us-west " b"value=0.64 1257894000000000000\n" ) + compressed.seek(0) self.assertEqual( - gzip.decompress(m.last_request.body), - gzip.decompress(compressed.getvalue()), + gzip.GzipFile(fileobj=io.BytesIO(m.last_request.body)).read(), + gzip.GzipFile(fileobj=compressed).read() ) def test_write_points_gzip(self): @@ -276,10 +277,11 @@ def test_write_points_gzip(self): b'cpu_load_short,host=server01,region=us-west ' b'value=0.64 1257894000123456000\n' ) + compressed.seek(0) self.assertEqual( - gzip.decompress(m.last_request.body), - gzip.decompress(compressed.getvalue()), + gzip.GzipFile(fileobj=io.BytesIO(m.last_request.body)).read(), + gzip.GzipFile(fileobj=compressed).read() ) def test_write_points_toplevel_attributes(self):