Skip to content

Commit 8e724fc

Browse files
committed
Use json_stream function in decoded _stream_helper
Signed-off-by: Joffrey F <[email protected]>
1 parent 1adc9e3 commit 8e724fc

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

docker/api/client.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@
1818
from .swarm import SwarmApiMixin
1919
from .volume import VolumeApiMixin
2020
from .. import auth
21-
from ..constants import (DEFAULT_TIMEOUT_SECONDS, DEFAULT_USER_AGENT,
22-
IS_WINDOWS_PLATFORM, DEFAULT_DOCKER_API_VERSION,
23-
STREAM_HEADER_SIZE_BYTES, DEFAULT_NUM_POOLS,
24-
MINIMUM_DOCKER_API_VERSION)
25-
from ..errors import (DockerException, TLSParameterError,
26-
create_api_error_from_http_exception)
21+
from ..constants import (
22+
DEFAULT_TIMEOUT_SECONDS, DEFAULT_USER_AGENT, IS_WINDOWS_PLATFORM,
23+
DEFAULT_DOCKER_API_VERSION, STREAM_HEADER_SIZE_BYTES, DEFAULT_NUM_POOLS,
24+
MINIMUM_DOCKER_API_VERSION
25+
)
26+
from ..errors import (
27+
DockerException, TLSParameterError,
28+
create_api_error_from_http_exception
29+
)
2730
from ..tls import TLSConfig
2831
from ..transport import SSLAdapter, UnixAdapter
2932
from ..utils import utils, check_resource, update_headers
3033
from ..utils.socket import frames_iter
34+
from ..utils.json_stream import json_stream
3135
try:
3236
from ..transport import NpipeAdapter
3337
except ImportError:
@@ -274,27 +278,20 @@ def _get_raw_response_socket(self, response):
274278

275279
def _stream_helper(self, response, decode=False):
276280
"""Generator for data coming from a chunked-encoded HTTP response."""
281+
277282
if response.raw._fp.chunked:
278-
reader = response.raw
279-
while not reader.closed:
280-
# this read call will block until we get a chunk
281-
data = reader.read(1)
282-
if not data:
283-
break
284-
if reader._fp.chunk_left:
285-
data += reader.read(reader._fp.chunk_left)
286-
if decode:
287-
if six.PY3:
288-
data = data.decode('utf-8')
289-
# remove the trailing newline
290-
data = data.strip()
291-
# split the data at any newlines
292-
data_list = data.split("\r\n")
293-
# load and yield each line seperately
294-
for data in data_list:
295-
data = json.loads(data)
296-
yield data
297-
else:
283+
if decode:
284+
for chunk in json_stream(self._stream_helper(response, False)):
285+
yield chunk
286+
else:
287+
reader = response.raw
288+
while not reader.closed:
289+
# this read call will block until we get a chunk
290+
data = reader.read(1)
291+
if not data:
292+
break
293+
if reader._fp.chunk_left:
294+
data += reader.read(reader._fp.chunk_left)
298295
yield data
299296
else:
300297
# Response isn't chunked, meaning we probably

0 commit comments

Comments
 (0)