Skip to content
Snippets Groups Projects
Verified Commit 837dccbd authored by Timm Fitschen's avatar Timm Fitschen
Browse files

FIX: implementation of WrappedResponse

parent e50affa0
No related branches found
No related tags found
2 merge requests!79Release 0.10.0,!75F http proxy
Pipeline #30210 passed
......@@ -65,6 +65,9 @@ class _WrappedHTTPResponse(CaosDBHTTPResponse):
def __init__(self, response):
self.response = response
self._generator = None
self._buffer = b''
self._stream_consumed = False
@property
def reason(self):
......@@ -75,12 +78,44 @@ class _WrappedHTTPResponse(CaosDBHTTPResponse):
return self.response.status_code
def read(self, size=None):
if size is None or size == 0:
result = b''
for chunk in self.response.iter_content(chunk_size=1024):
result = result + chunk
if self._stream_consumed is True:
raise RuntimeError("Stream is consumed")
if self._buffer is None:
# the buffer has been drained in the previous call.
self._stream_consumed = True
return b''
if self._generator is None and (size is None or size == 0):
# return full content at once
self._stream_consumed = True
return self.response.content
if len(self._buffer) >= size:
# still enough bytes in the buffer
result = chunk[:size]
self._buffer = chunk[size:]
return result
if self._generator is None:
# first call to this method
if size is None or size == 0:
size = 512
self._generator = self.response.iter_content(size)
try:
# read new data into the buffer
chunk = self._buffer + next(self._generator)
result = chunk[:size]
if len(result) == 0:
self._stream_consumed = True
self._buffer = chunk[size:]
return result
except StopIteration:
# drain buffer
result = self._buffer
self._buffer = None
return result
return self.response.iter_content(size)
def getheader(self, name, default=None):
return self.response.headers[name] if name in self.response.headers else default
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment