Skip to content

Commit 1ca6690

Browse files
authored
fix: querying data if debug is enabled (#483)
1 parent 7c1b3ac commit 1ca6690

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
:warning: This release drop supports for Python 3.6. As of 2021-12-23, 3.6 has reached the end-of-life phase of its release cycle. 3.6.15 was the final security release. For more info see: https://peps.python.org/pep-0494/#lifespan
44

5+
### Bug Fixes
6+
1. [#483](https://github.com/influxdata/influxdb-client-python/pull/483): Querying data if the `debug` is enabled
7+
58
### Dependencies
69
1. [#472](https://github.com/influxdata/influxdb-client-python/pull/472): Update `RxPY` to `4.0.4`
710

influxdb_client/client/flux_csv_parser.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ def _close(self):
8585

8686
def __enter__(self):
8787
"""Initialize CSV reader."""
88-
self._reader = csv_parser.reader(codecs.iterdecode(self._response, _UTF_8_encoding))
88+
# response can be exhausted by logger, so we have to use data that has already been read
89+
if hasattr(self._response, 'closed') and self._response.closed:
90+
from io import StringIO
91+
self._reader = csv_parser.reader(StringIO(self._response.data.decode(_UTF_8_encoding)))
92+
else:
93+
self._reader = csv_parser.reader(codecs.iterdecode(self._response, _UTF_8_encoding))
8994
return self
9095

9196
def __exit__(self, exc_type, exc_val, exc_tb):

tests/test_InfluxDBClient.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import codecs
12
import http.server
23
import json
34
import logging
@@ -234,6 +235,19 @@ def test_username_password_authorization(self):
234235
self.client = InfluxDBClient(url=self.host, username="my-user", password="my-password", debug=True)
235236
self.client.query_api().query("buckets()", "my-org")
236237

238+
def test_query_and_debug(self):
239+
self.client.close()
240+
self.client = InfluxDBClient(url=self.host, token="my-token", debug=True)
241+
# Query
242+
results = self.client.query_api().query("buckets()", "my-org")
243+
self.assertIn("my-bucket", list(map(lambda record: record["name"], results[0].records)))
244+
# Query RAW
245+
results = self.client.query_api().query_raw("buckets()", "my-org")
246+
self.assertIn("my-bucket", codecs.decode(results.data))
247+
# Bucket API
248+
results = self.client.buckets_api().find_buckets()
249+
self.assertIn("my-bucket", list(map(lambda bucket: bucket.name, results.buckets)))
250+
237251
def _start_proxy_server(self):
238252
import http.server
239253
import urllib.request

tests/test_InfluxDBClientAsync.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pytest
99
from aioresponses import aioresponses
1010

11-
from influxdb_client import Point, WritePrecision
11+
from influxdb_client import Point, WritePrecision, BucketsService
1212
from influxdb_client.client.exceptions import InfluxDBError
1313
from influxdb_client.client.influxdb_client_async import InfluxDBClientAsync
1414
from influxdb_client.client.warnings import MissingPivotFunction
@@ -297,6 +297,21 @@ async def test_redacted_auth_header(self):
297297

298298
self.assertIn("Authorization: ***", log_stream.getvalue())
299299

300+
@async_test
301+
async def test_query_and_debug(self):
302+
await self.client.close()
303+
self.client = InfluxDBClientAsync(url="http://localhost:8086", token="my-token", debug=True)
304+
# Query
305+
results = await self.client.query_api().query("buckets()", "my-org")
306+
self.assertIn("my-bucket", list(map(lambda record: record["name"], results[0].records)))
307+
# Query RAW
308+
results = await self.client.query_api().query_raw("buckets()", "my-org")
309+
self.assertIn("my-bucket", results)
310+
# Bucket API
311+
buckets_service = BucketsService(api_client=self.client.api_client)
312+
results = await buckets_service.get_buckets()
313+
self.assertIn("my-bucket", list(map(lambda bucket: bucket.name, results.buckets)))
314+
300315
async def _prepare_data(self, measurement: str):
301316
_point1 = Point(measurement).tag("location", "Prague").field("temperature", 25.3)
302317
_point2 = Point(measurement).tag("location", "New York").field("temperature", 24.3)

0 commit comments

Comments
 (0)