stream -> stream_bytes, raw -> stream_raw (#599)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by GitHub · parent dad3797 · Browse files · 4 files·+33 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+33 added−14 removed
- tests:+6 added−6 removed
- other:+27 added−8 removed
Provenance
- Commit
fc95c7e- Tree
171001c- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
dad3797
4 files changed+33 added−14 removed
modified:docs/api.md+4 added-2 removed
61 unmodified lines626364656667666768697071727387 unmodified lines61 unmodified lines the total elapsed seconds.* `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**
## `Request`87 unmodified linesmodified:httpx/models.py+23 added-6 removed
2 unmodified lines3 import email.message4 import json as jsonlib5 import typing6 import urllib.request7+import warnings8 from collections.abc import MutableMapping9 from http.cookiejar import Cookie, CookieJar10 from urllib.parse import parse_qsl, urlencode11 689 unmodified lines701 return f"<{class_name}({self.method!r}, {url!r})>"702 703 async def read(self) -> bytes:704 """704- Read and return the response content.705+ Read and return the request content.706 """707 if not hasattr(self, "content"):708 self.content = b"".join([part async for part in self.stream()])709 return self.content208 unmodified lines918 """919 Read and return the response content.920 """921 if not hasattr(self, "_content"):921- self._content = b"".join([part async for part in self.stream()])922+ self._content = b"".join([part async for part in self.stream_bytes()])923 return self._content924 925+ @property926+ def stream(self): # type: ignore927+ warnings.warn(928+ "Response.stream() is due to be deprecated. "929+ "Use Response.stream_bytes() instead."930+ )931+ return self.stream_bytes932+933+ @property934+ def raw(self): # type: ignore935+ warnings.warn(936+ "Response.raw() is due to be deprecated. "937+ "Use Response.stream_raw() instead."938+ )939+ return self.stream_raw940+924- async def stream(self) -> typing.AsyncIterator[bytes]:941+ async def stream_bytes(self) -> typing.AsyncIterator[bytes]:942 """943 A byte-iterator over the decoded response content.944 This allows us to handle gzip, deflate, and brotli encoded responses.945 """946 if hasattr(self, "_content"):947 yield self._content948 else:932- async for chunk in self.raw():949+ async for chunk in self.stream_raw():950 yield self.decoder.decode(chunk)951 yield self.decoder.flush()952 953 async def stream_text(self) -> typing.AsyncIterator[str]:2 unmodified lines956 that handles both gzip, deflate, etc but also detects the content's957 string encoding.958 """959 decoder = TextDecoder(encoding=self.charset_encoding)943- async for chunk in self.stream():960+ async for chunk in self.stream_bytes():961 yield decoder.decode(chunk)962 yield decoder.flush()963 964 async def stream_lines(self) -> typing.AsyncIterator[str]:3 unmodified lines968 yield line969 for line in decoder.flush():970 yield line971 955- async def raw(self) -> typing.AsyncIterator[bytes]:972+ async def stream_raw(self) -> typing.AsyncIterator[bytes]:973 """974 A byte-iterator over the raw response content.975 """976 if hasattr(self, "_raw_content"):205 unmodified linesmodified:tests/client/test_client.py+2 added-2 removed
69 unmodified lines70 async with httpx.Client() as client:71 response = await client.get(server.url, stream=True)72 assert response.status_code == 20073 body = b""74- async for chunk in response.stream():74+ async for chunk in response.stream_bytes():75 body += chunk76 assert body == b"Hello, world!"77 78 2 unmodified lines81 async with httpx.Client() as client:82 response = await client.get(server.url, stream=True)83 assert response.status_code == 20084 body = b""85- async for chunk in response.raw():85+ async for chunk in response.stream_raw():86 body += chunk87 assert body == b"Hello, world!"88 await response.close()89 101 unmodified linesmodified:tests/models/test_responses.py+4 added-4 removed
135 unmodified lines136 async def test_raw_interface():137 response = httpx.Response(200, content=b"Hello, world!")138 139 raw = b""140- async for part in response.raw():140+ async for part in response.stream_raw():141 raw += part142 assert raw == b"Hello, world!"143 144 1 unmodified line146 async def test_stream_interface():147 response = httpx.Response(200, content=b"Hello, world!")148 149 content = b""150- async for part in response.stream():150+ async for part in response.stream_bytes():151 content += part152 assert content == b"Hello, world!"153 154 27 unmodified lines182 183 await response.read()184 185 content = b""186- async for part in response.stream():186+ async for part in response.stream_bytes():187 content += part188 assert content == b"Hello, world!"189 190 15 unmodified lines206 async def test_cannot_read_after_stream_consumed():207 response = httpx.Response(200, content=async_streaming_body())208 209 content = b""210- async for part in response.stream():210+ async for part in response.stream_bytes():211 content += part212 213 with pytest.raises(httpx.StreamConsumed):214 await response.read()73 unmodified lines