* `def .raise_for_status()` - **None**
* `def .json()` - **Any**
* `def .read()` - **bytes**
-* `def .stream()` - **bytes iterator**
-* `def .raw()` - **bytes iterator**
+* `def .stream_raw()` - **async bytes iterator**
+* `def .stream_bytes()` - **async bytes iterator**
+* `def .stream_text()` - **async text iterator**
+* `def .stream_lines()` - **async text iterator**
* `def .close()` - **None**
* `def .next()` - **Response**
import json as jsonlib
import typing
import urllib.request
+import warnings
from collections.abc import MutableMapping
from http.cookiejar import Cookie, CookieJar
from urllib.parse import parse_qsl, urlencode
async def read(self) -> bytes:
"""
- Read and return the response content.
+ Read and return the request content.
"""
if not hasattr(self, "content"):
self.content = b"".join([part async for part in self.stream()])
Read and return the response content.
"""
if not hasattr(self, "_content"):
- self._content = b"".join([part async for part in self.stream()])
+ self._content = b"".join([part async for part in self.stream_bytes()])
return self._content
- async def stream(self) -> typing.AsyncIterator[bytes]:
+ @property
+ def stream(self): # type: ignore
+ warnings.warn(
+ "Response.stream() is due to be deprecated. "
+ "Use Response.stream_bytes() instead."
+ )
+ return self.stream_bytes
+
+ @property
+ def raw(self): # type: ignore
+ warnings.warn(
+ "Response.raw() is due to be deprecated. "
+ "Use Response.stream_raw() instead."
+ )
+ return self.stream_raw
+
+ async def stream_bytes(self) -> typing.AsyncIterator[bytes]:
"""
A byte-iterator over the decoded response content.
This allows us to handle gzip, deflate, and brotli encoded responses.
if hasattr(self, "_content"):
yield self._content
else:
- async for chunk in self.raw():
+ async for chunk in self.stream_raw():
yield self.decoder.decode(chunk)
yield self.decoder.flush()
string encoding.
"""
decoder = TextDecoder(encoding=self.charset_encoding)
- async for chunk in self.stream():
+ async for chunk in self.stream_bytes():
yield decoder.decode(chunk)
yield decoder.flush()
for line in decoder.flush():
yield line
- async def raw(self) -> typing.AsyncIterator[bytes]:
+ async def stream_raw(self) -> typing.AsyncIterator[bytes]:
"""
A byte-iterator over the raw response content.
"""
response = await client.get(server.url, stream=True)
assert response.status_code == 200
body = b""
- async for chunk in response.stream():
+ async for chunk in response.stream_bytes():
body += chunk
assert body == b"Hello, world!"
response = await client.get(server.url, stream=True)
assert response.status_code == 200
body = b""
- async for chunk in response.raw():
+ async for chunk in response.stream_raw():
body += chunk
assert body == b"Hello, world!"
await response.close()
response = httpx.Response(200, content=b"Hello, world!")
raw = b""
- async for part in response.raw():
+ async for part in response.stream_raw():
raw += part
assert raw == b"Hello, world!"
response = httpx.Response(200, content=b"Hello, world!")
content = b""
- async for part in response.stream():
+ async for part in response.stream_bytes():
content += part
assert content == b"Hello, world!"
await response.read()
content = b""
- async for part in response.stream():
+ async for part in response.stream_bytes():
content += part
assert content == b"Hello, world!"
response = httpx.Response(200, content=async_streaming_body())
content = b""
- async for part in response.stream():
+ async for part in response.stream_bytes():
content += part
with pytest.raises(httpx.StreamConsumed):