From fb6c9a82957def973f3029e9d53dd5b4753136f3 Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Wed, 11 Jan 2017 17:24:50 -0800 Subject: Use json_stream function in decoded _stream_helper Signed-off-by: Joffrey F --- docker/api/client.py | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) (limited to 'docker/api/client.py') diff --git a/docker/api/client.py b/docker/api/client.py index a9fe7d0..22c32b4 100644 --- a/docker/api/client.py +++ b/docker/api/client.py @@ -18,16 +18,20 @@ from .service import ServiceApiMixin from .swarm import SwarmApiMixin from .volume import VolumeApiMixin from .. import auth -from ..constants import (DEFAULT_TIMEOUT_SECONDS, DEFAULT_USER_AGENT, - IS_WINDOWS_PLATFORM, DEFAULT_DOCKER_API_VERSION, - STREAM_HEADER_SIZE_BYTES, DEFAULT_NUM_POOLS, - MINIMUM_DOCKER_API_VERSION) -from ..errors import (DockerException, TLSParameterError, - create_api_error_from_http_exception) +from ..constants import ( + DEFAULT_TIMEOUT_SECONDS, DEFAULT_USER_AGENT, IS_WINDOWS_PLATFORM, + DEFAULT_DOCKER_API_VERSION, STREAM_HEADER_SIZE_BYTES, DEFAULT_NUM_POOLS, + MINIMUM_DOCKER_API_VERSION +) +from ..errors import ( + DockerException, TLSParameterError, + create_api_error_from_http_exception +) from ..tls import TLSConfig from ..transport import SSLAdapter, UnixAdapter from ..utils import utils, check_resource, update_headers from ..utils.socket import frames_iter +from ..utils.json_stream import json_stream try: from ..transport import NpipeAdapter except ImportError: @@ -274,27 +278,20 @@ class APIClient( def _stream_helper(self, response, decode=False): """Generator for data coming from a chunked-encoded HTTP response.""" + if response.raw._fp.chunked: - reader = response.raw - while not reader.closed: - # this read call will block until we get a chunk - data = reader.read(1) - if not data: - break - if reader._fp.chunk_left: - data += reader.read(reader._fp.chunk_left) - if decode: - if six.PY3: - data = data.decode('utf-8') - # remove the trailing newline - data = data.strip() - # split the data at any newlines - data_list = data.split("\r\n") - # load and yield each line seperately - for data in data_list: - data = json.loads(data) - yield data - else: + if decode: + for chunk in json_stream(self._stream_helper(response, False)): + yield chunk + else: + reader = response.raw + while not reader.closed: + # this read call will block until we get a chunk + data = reader.read(1) + if not data: + break + if reader._fp.chunk_left: + data += reader.read(reader._fp.chunk_left) yield data else: # Response isn't chunked, meaning we probably -- cgit v1.2.1