Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft committed Apr 13, 2021
1 parent 98f6179 commit cd12344
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 27 deletions.
54 changes: 36 additions & 18 deletions sdk/core/azure-core/azure/core/rest/_rest_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,12 @@ def stream_download(self, pipeline=None):
:rtype: iterator[bytes]
"""

def _validate_streaming_access(self) -> None:
if self.is_closed:
raise TypeError("Can not iterate over stream, it is closed.")
if self.is_stream_consumed:
raise TypeError("Can not iterate over stream, it has been fully consumed")

class HttpResponse(_HttpResponseBase):

@property
Expand All @@ -567,9 +573,16 @@ def read(self) -> bytes:
Read the response's bytes.
"""
if not hasattr(self, "_content"):
self._content = self._internal_response.internal_response.read()
return self._content
try:
return self._content
except AttributeError:
self._validate_streaming_access()
self._content = (
self._internal_response.body() or
b"".join(self.iter_raw())
)
self._close_stream()
return self._content

def iter_bytes(self, chunk_size: int = None) -> Iterator[bytes]:
"""Iterate over the bytes in the response stream
Expand All @@ -596,20 +609,20 @@ def iter_lines(self, chunk_size: int = None) -> Iterator[str]:
for line in lines:
yield line

def _close_stream(self) -> None:
self.is_stream_consumed = True
self.close()

def iter_raw(self, chunk_size: int = None) -> Iterator[bytes]:
"""Iterate over the raw response bytes
"""
if self.is_closed:
raise TypeError("Can not iterate over stream, it is closed.")
if self.is_stream_consumed:
raise TypeError("Can not iterate over stream, it has been fully consumed")
self._validate_streaming_access()
stream_download = self._internal_response.stream_download(None, chunk_size=chunk_size)
for raw_bytes in stream_download:
self._num_bytes_downloaded += len(raw_bytes)
yield raw_bytes

self.is_stream_consumed = True
self.close() # close after iterating through everything
self._close_stream()


class AsyncHttpResponse(_HttpResponseBase):
Expand All @@ -621,14 +634,23 @@ def content(self) -> bytes:
except AttributeError:
raise TypeError("You have not read in the response's bytes yet. Call response.read() first.")

async def _close_stream(self) -> None:
self.is_stream_consumed = True
await self.close()

async def read(self) -> bytes:
"""
Read the response's bytes.
"""
if not hasattr(self, "_content"):
self._content = await self._internal_response.internal_response.read()
return self._content
try:
return self._content
except AttributeError:
self._validate_streaming_access()
await self._internal_response.load_body()
self._content = self._internal_response._body
await self._close_stream()
return self._content

async def iter_bytes(self, chunk_size: int = None) -> Iterator[bytes]:
"""Iterate over the bytes in the response stream
Expand Down Expand Up @@ -658,17 +680,13 @@ async def iter_lines(self, chunk_size: int = None) -> Iterator[str]:
async def iter_raw(self, chunk_size: int = None) -> Iterator[bytes]:
"""Iterate over the raw response bytes
"""
if self.is_closed:
raise TypeError("Can not iterate over stream, it is closed.")
if self.is_stream_consumed:
raise TypeError("Can not iterate over stream, it has been fully consumed")
self._validate_streaming_access()
stream_download = self._internal_response.stream_download(None, chunk_size=chunk_size)
async for raw_bytes in stream_download:
self._num_bytes_downloaded += len(raw_bytes)
yield raw_bytes

self.is_stream_consumed = True
await self.close() # close after iterating through everything
await self._close_stream()

async def close(self) -> None:
self.is_closed = True
Expand Down
24 changes: 16 additions & 8 deletions sdk/core/azure-core/tests/test_rest/test_async_http_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ async def test_rest_response():

assert response.status_code == 200
assert response.reason == "OK"
await response.read()
content = await response.read()
assert content == b"Hello, world!"
assert response.text == "Hello, world!"
assert response.request.method == "GET"
assert response.request.url == "https://example.org"
Expand All @@ -61,7 +62,8 @@ async def test_rest_response_content():

assert response.status_code == 200
assert response.reason == "OK"
await response.read()
content = await response.read()
assert content == b"Hello, world!"
assert response.text == "Hello, world!"
response.raise_for_status()

Expand All @@ -75,7 +77,8 @@ async def test_rest_response_text():

assert response.status_code == 200
assert response.reason == "OK"
await response.read()
content = await response.read()
assert content == b"Hello, world!"
assert response.text == "Hello, world!"
assert response.headers == {
"Content-Length": "13",
Expand All @@ -90,7 +93,8 @@ async def test_rest_response_html():

assert response.status_code == 200
assert response.reason == "OK"
await response.read()
content = await response.read()
assert content == b"<html><body>Hello, world!</html></body>"
assert response.text == "<html><body>Hello, world!</html></body>"
response.raise_for_status()

Expand Down Expand Up @@ -131,7 +135,8 @@ async def test_rest_response_content_type_encoding():
content=content,
headers=headers,
)
await response.read()
content == await response.read()
assert content == b'Latin 1: \xff'
assert response.text == "Latin 1: ÿ"
assert response.encoding == "latin-1"

Expand Down Expand Up @@ -183,7 +188,8 @@ async def test_rest_response_no_charset_with_ascii_content():
)
assert response.status_code == 200
assert response.encoding is None
await response.read()
content = await response.read()
assert content == b"Hello, world!"
assert response.text == "Hello, world!"


Expand All @@ -200,7 +206,8 @@ async def test_rest_response_no_charset_with_iso_8859_1_content():
content=content,
headers=headers,
)
await response.read()
content = await response.read()
assert content == b'Accented: \xd6sterreich'
assert response.text == "Accented: Österreich"
assert response.encoding is None

Expand All @@ -215,7 +222,8 @@ async def test_rest_response_set_explicit_encoding():
headers=headers,
)
response.encoding = "latin-1"
await response.read()
content = await response.read()
assert content == b'Latin 1: \xff'
assert response.text == "Latin 1: ÿ"
assert response.encoding == "latin-1"

Expand Down
1 change: 1 addition & 0 deletions sdk/core/azure-core/tests/test_rest/test_http_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def test_rest_response_repr():
content=b"Hello, world!",
headers=headers
)
response.read()
assert repr(response) == "<HttpResponse: 200 OK, Content-Type: text-plain>"

def test_rest_response_content_type_encoding():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ def test_rest_sync_streaming_response():
assert response.content == file_bytes
assert response.is_closed


def test_rest_cannot_read_after_stream_consumed():
response = _create_http_response(url="https://httpbin.org/image/jpeg")

Expand Down

0 comments on commit cd12344

Please sign in to comment.