|
18 | 18 | from .swarm import SwarmApiMixin |
19 | 19 | from .volume import VolumeApiMixin |
20 | 20 | 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 | +) |
27 | 30 | from ..tls import TLSConfig |
28 | 31 | from ..transport import SSLAdapter, UnixAdapter |
29 | 32 | from ..utils import utils, check_resource, update_headers |
30 | 33 | from ..utils.socket import frames_iter |
| 34 | +from ..utils.json_stream import json_stream |
31 | 35 | try: |
32 | 36 | from ..transport import NpipeAdapter |
33 | 37 | except ImportError: |
@@ -274,27 +278,20 @@ def _get_raw_response_socket(self, response): |
274 | 278 |
|
275 | 279 | def _stream_helper(self, response, decode=False): |
276 | 280 | """Generator for data coming from a chunked-encoded HTTP response.""" |
| 281 | + |
277 | 282 | 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) |
298 | 295 | yield data |
299 | 296 | else: |
300 | 297 | # Response isn't chunked, meaning we probably |
|
0 commit comments