diff options
author | Joffrey F <f.joffrey@gmail.com> | 2017-01-18 15:40:47 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-18 15:40:47 -0800 |
commit | 7db5f7ebcc615db903e3007c05495a82bb319810 (patch) | |
tree | 07aee211b6203b15463fa7fd1225fb63b26ff15e /docker/api/client.py | |
parent | 468bb1c5454c0b76effa89e739e7788999367b4a (diff) | |
parent | fb6c9a82957def973f3029e9d53dd5b4753136f3 (diff) | |
download | docker-py-7db5f7ebcc615db903e3007c05495a82bb319810.tar.gz |
Merge pull request #1389 from docker/fix-stream-helper
Use json_stream function in decoded _stream_helper
Diffstat (limited to 'docker/api/client.py')
-rw-r--r-- | docker/api/client.py | 49 |
1 files changed, 23 insertions, 26 deletions
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 |