Skip to content

Commit 38a1385

Browse files
authored
remove iter_text and iter_lines (#20460)
1 parent 0320ace commit 38a1385

File tree

7 files changed

+29
-117
lines changed

7 files changed

+29
-117
lines changed

sdk/core/azure-core/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Release History
22

3-
## 1.17.1 (Unreleased)
3+
## 1.18.0 (Unreleased)
44

55
### Features Added
66

@@ -10,6 +10,7 @@
1010

1111
- The `text` property on `azure.core.rest.HttpResponse` and `azure.core.rest.AsyncHttpResponse` has changed to a method, which also takes
1212
an `encoding` parameter.
13+
- Removed `iter_text` and `iter_lines` from `azure.core.rest.HttpResponse` and `azure.core.rest.AsyncHttpResponse`
1314

1415
### Bugs Fixed
1516

sdk/core/azure-core/azure/core/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
# regenerated.
1010
# --------------------------------------------------------------------------
1111

12-
VERSION = "1.17.1"
12+
VERSION = "1.18.0"

sdk/core/azure-core/azure/core/rest/_helpers.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -227,40 +227,6 @@ def lookup_encoding(encoding):
227227
except LookupError:
228228
return False
229229

230-
def parse_lines_from_text(text):
231-
# largely taken from httpx's LineDecoder code
232-
lines = []
233-
last_chunk_of_text = ""
234-
while text:
235-
text_length = len(text)
236-
for idx in range(text_length):
237-
curr_char = text[idx]
238-
next_char = None if idx == len(text) - 1 else text[idx + 1]
239-
if curr_char == "\n":
240-
lines.append(text[: idx + 1])
241-
text = text[idx + 1: ]
242-
break
243-
if curr_char == "\r" and next_char == "\n":
244-
# if it ends with \r\n, we only do \n
245-
lines.append(text[:idx] + "\n")
246-
text = text[idx + 2:]
247-
break
248-
if curr_char == "\r" and next_char is not None:
249-
# if it's \r then a normal character, we switch \r to \n
250-
lines.append(text[:idx] + "\n")
251-
text = text[idx + 1:]
252-
break
253-
if next_char is None:
254-
last_chunk_of_text += text
255-
text = ""
256-
break
257-
if last_chunk_of_text.endswith("\r"):
258-
# if ends with \r, we switch \r to \n
259-
lines.append(last_chunk_of_text[:-1] + "\n")
260-
elif last_chunk_of_text:
261-
lines.append(last_chunk_of_text)
262-
return lines
263-
264230
def to_pipeline_transport_request_helper(rest_request):
265231
from ..pipeline.transport import HttpRequest as PipelineTransportHttpRequest
266232
return PipelineTransportHttpRequest(

sdk/core/azure-core/azure/core/rest/_rest.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
from ..utils._utils import _case_insensitive_dict
3434
from ._helpers import (
3535
FilesType,
36-
parse_lines_from_text,
3736
set_content_body,
3837
set_json_body,
3938
set_multipart_body,
@@ -357,21 +356,6 @@ def iter_bytes(self):
357356
"""
358357
raise NotImplementedError()
359358

360-
def iter_text(self):
361-
# type: () -> Iterator[str]
362-
"""Iterate over the response text
363-
"""
364-
for byte in self.iter_bytes():
365-
text = byte.decode(self.encoding or "utf-8")
366-
yield text
367-
368-
def iter_lines(self):
369-
# type: () -> Iterator[str]
370-
for text in self.iter_text():
371-
lines = parse_lines_from_text(text)
372-
for line in lines:
373-
yield line
374-
375359
def _close_stream(self):
376360
# type: (...) -> None
377361
self.is_stream_consumed = True

sdk/core/azure-core/azure/core/rest/_rest_py3.py

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
FilesType,
4949
HeadersType,
5050
cast,
51-
parse_lines_from_text,
5251
set_json_body,
5352
set_multipart_body,
5453
set_urlencoded_body,
@@ -377,27 +376,6 @@ def iter_bytes(self) -> Iterator[bytes]:
377376
"""
378377
raise NotImplementedError()
379378

380-
def iter_text(self) -> Iterator[str]:
381-
"""Iterates over the text in the response.
382-
383-
:return: An iterator of string. Each string chunk will be a text from the response
384-
:rtype: Iterator[str]
385-
"""
386-
for byte in self.iter_bytes():
387-
text = byte.decode(self.encoding or "utf-8")
388-
yield text
389-
390-
def iter_lines(self) -> Iterator[str]:
391-
"""Iterates over the lines in the response.
392-
393-
:return: An iterator of string. Each string chunk will be a line from the response
394-
:rtype: Iterator[str]
395-
"""
396-
for text in self.iter_text():
397-
lines = parse_lines_from_text(text)
398-
for line in lines:
399-
yield line
400-
401379
def __repr__(self) -> str:
402380
content_type_str = (
403381
", Content-Type: {}".format(self.content_type) if self.content_type else ""
@@ -471,27 +449,6 @@ async def iter_bytes(self) -> AsyncIterator[bytes]:
471449
# getting around mypy behavior, see https://github.com/python/mypy/issues/10732
472450
yield # pylint: disable=unreachable
473451

474-
async def iter_text(self) -> AsyncIterator[str]:
475-
"""Asynchronously iterates over the text in the response.
476-
477-
:return: An async iterator of string. Each string chunk will be a text from the response
478-
:rtype: AsyncIterator[str]
479-
"""
480-
async for byte in self.iter_bytes(): # type: ignore
481-
text = byte.decode(self.encoding or "utf-8")
482-
yield text
483-
484-
async def iter_lines(self) -> AsyncIterator[str]:
485-
"""Asynchronously iterates over the lines in the response.
486-
487-
:return: An async iterator of string. Each string chunk will be a line from the response
488-
:rtype: AsyncIterator[str]
489-
"""
490-
async for text in self.iter_text():
491-
lines = parse_lines_from_text(text)
492-
for line in lines:
493-
yield line
494-
495452
async def close(self) -> None:
496453
"""Close the response.
497454

sdk/core/azure-core/tests/async_tests/test_rest_stream_responses_async.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ async def test_iter_bytes(client):
6666
assert response.is_closed
6767
assert raw == b"Hello, world!"
6868

69+
@pytest.mark.skip(reason="We've gotten rid of iter_text for now")
6970
@pytest.mark.asyncio
7071
async def test_iter_text(client):
7172
request = HttpRequest("GET", "/basic/string")
@@ -76,14 +77,15 @@ async def test_iter_text(client):
7677
content += part
7778
assert content == "Hello, world!"
7879

80+
@pytest.mark.skip(reason="We've gotten rid of iter_lines for now")
7981
@pytest.mark.asyncio
8082
async def test_iter_lines(client):
8183
request = HttpRequest("GET", "/basic/lines")
8284

8385
async with client.send_request(request, stream=True) as response:
8486
content = []
85-
async for line in response.iter_lines():
86-
content.append(line)
87+
async for part in response.iter_lines():
88+
content.append(part)
8789
assert content == ["Hello,\n", "world!"]
8890

8991

@@ -161,11 +163,11 @@ async def test_iter_read_back_and_forth(client):
161163
# the reason why the code flow is like this, is because the 'iter_x' functions don't
162164
# actually read the contents into the response, the output them. Once they're yielded,
163165
# the stream is closed, so you have to catch the output when you iterate through it
164-
request = HttpRequest("GET", "/basic/lines")
166+
request = HttpRequest("GET", "/basic/string")
165167

166168
async with client.send_request(request, stream=True) as response:
167-
async for line in response.iter_lines():
168-
assert line
169+
async for part in response.iter_bytes():
170+
assert part
169171
with pytest.raises(ResponseNotReadError):
170172
response.text()
171173
with pytest.raises(StreamConsumedError):
@@ -175,16 +177,16 @@ async def test_iter_read_back_and_forth(client):
175177

176178
@pytest.mark.asyncio
177179
async def test_stream_with_return_pipeline_response(client):
178-
request = HttpRequest("GET", "/basic/lines")
180+
request = HttpRequest("GET", "/basic/string")
179181
pipeline_response = await client.send_request(request, stream=True, _return_pipeline_response=True)
180182
assert hasattr(pipeline_response, "http_request")
181183
assert hasattr(pipeline_response.http_request, "content")
182184
assert hasattr(pipeline_response, "http_response")
183185
assert hasattr(pipeline_response, "context")
184186
parts = []
185-
async for line in pipeline_response.http_response.iter_lines():
186-
parts.append(line)
187-
assert parts == ['Hello,\n', 'world!']
187+
async for part in pipeline_response.http_response.iter_bytes():
188+
parts.append(part)
189+
assert parts == [b'Hello, world!']
188190
await client.close()
189191

190192
@pytest.mark.asyncio

sdk/core/azure-core/tests/test_rest_stream_responses.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def test_iter_bytes(client):
7979
assert response.is_stream_consumed
8080
assert raw == b"Hello, world!"
8181

82+
@pytest.mark.skip(reason="We've gotten rid of iter_text for now")
8283
def test_iter_text(client):
8384
request = HttpRequest("GET", "/basic/string")
8485

@@ -88,6 +89,7 @@ def test_iter_text(client):
8889
content += part
8990
assert content == "Hello, world!"
9091

92+
@pytest.mark.skip(reason="We've gotten rid of iter_lines for now")
9193
def test_iter_lines(client):
9294
request = HttpRequest("GET", "/basic/lines")
9395

@@ -175,18 +177,18 @@ def test_decompress_compressed_header(client):
175177
url = "https://{}.blob.core.windows.net/tests/test_with_header.tar.gz".format(account_name)
176178
request = HttpRequest("GET", url)
177179
response = client.send_request(request, stream=True)
178-
iter = response.iter_text()
179-
data = "".join(list(iter))
180-
assert data == "test"
180+
iter = response.iter_bytes()
181+
data = b"".join(list(iter))
182+
assert data == b"test"
181183

182184
def test_iter_read(client):
183185
# thanks to McCoy Patiño for this test!
184-
request = HttpRequest("GET", "/basic/lines")
186+
request = HttpRequest("GET", "/basic/string")
185187
response = client.send_request(request, stream=True)
186188
response.read()
187-
iterator = response.iter_lines()
188-
for line in iterator:
189-
assert line
189+
iterator = response.iter_bytes()
190+
for part in iterator:
191+
assert part
190192
assert response.text()
191193

192194
def test_iter_read_back_and_forth(client):
@@ -196,11 +198,11 @@ def test_iter_read_back_and_forth(client):
196198
# the reason why the code flow is like this, is because the 'iter_x' functions don't
197199
# actually read the contents into the response, the output them. Once they're yielded,
198200
# the stream is closed, so you have to catch the output when you iterate through it
199-
request = HttpRequest("GET", "/basic/lines")
201+
request = HttpRequest("GET", "/basic/string")
200202
response = client.send_request(request, stream=True)
201-
iterator = response.iter_lines()
202-
for line in iterator:
203-
assert line
203+
iterator = response.iter_bytes()
204+
for part in iterator:
205+
assert part
204206
with pytest.raises(ResponseNotReadError):
205207
response.text()
206208
with pytest.raises(StreamConsumedError):
@@ -209,12 +211,12 @@ def test_iter_read_back_and_forth(client):
209211
response.text()
210212

211213
def test_stream_with_return_pipeline_response(client):
212-
request = HttpRequest("GET", "/basic/lines")
214+
request = HttpRequest("GET", "/basic/string")
213215
pipeline_response = client.send_request(request, stream=True, _return_pipeline_response=True)
214216
assert hasattr(pipeline_response, "http_request")
215217
assert hasattr(pipeline_response, "http_response")
216218
assert hasattr(pipeline_response, "context")
217-
assert list(pipeline_response.http_response.iter_lines()) == ['Hello,\n', 'world!']
219+
assert list(pipeline_response.http_response.iter_bytes()) == [b'Hello, world!']
218220

219221
def test_error_reading(client):
220222
request = HttpRequest("GET", "/errors/403")

0 commit comments

Comments
 (0)