Skip to content

Commit 1a523a4

Browse files
author
aviau
committed
1 parent 6a4d2dd commit 1a523a4

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

influxdb/chunked_json.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
3+
#
4+
# Author: Adrian Sampson <adrian@radbox.org>
5+
# Source: https://gist.github.com/sampsyo/920215
6+
#
7+
8+
import json
9+
10+
_decoder = json.JSONDecoder()
11+
12+
13+
def loads(s):
14+
"""A generator reading a sequence of JSON values from a string."""
15+
while s:
16+
s = s.strip()
17+
obj, pos = _decoder.raw_decode(s)
18+
if not pos:
19+
raise ValueError('no JSON object found at %i' % pos)
20+
yield obj
21+
s = s[pos:]

influxdb/client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import socket
77
import requests
88

9+
from influxdb import chunked_json
10+
911
try:
1012
xrange
1113
except NameError:
@@ -322,7 +324,11 @@ def query(self, query, time_precision='s', chunked=False):
322324
expected_response_code=200
323325
)
324326

325-
return response.json()
327+
if chunked:
328+
return list(chunked_json.loads(response.content))
329+
else:
330+
return response.json()
331+
326332

327333
# Creating and Dropping Databases
328334
#

tests/influxdb/client_test.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def test_query(self):
242242

243243
def test_query_chunked(self):
244244
cli = InfluxDBClient(database='db')
245-
example_response = {
245+
example_object = {
246246
'points': [
247247
[1415206250119, 40001, 667],
248248
[1415206244555, 30001, 7],
@@ -257,17 +257,18 @@ def test_query_chunked(self):
257257
'val'
258258
]
259259
}
260+
example_response = json.dumps(example_object) + json.dumps(example_object)
260261

261262
with requests_mock.Mocker() as m:
262263
m.register_uri(
263264
requests_mock.GET,
264265
"http://localhost:8086/db/db/series",
265-
text=json.dumps(example_response)
266+
text=example_response
266267
)
267268

268-
self.assertDictEqual(
269+
self.assertListEqual(
269270
cli.query('select * from foo', chunked=True),
270-
example_response
271+
[example_object, example_object]
271272
)
272273

273274
@raises(Exception)

0 commit comments

Comments
 (0)