Skip to content
Myr

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

4 files+33 added−14 removed

modified:docs/api.md+4 added-2 removed
modified:httpx/models.py+23 added-6 removed
2 unmodified lines
3import email.message4import json as jsonlib5import typing6import urllib.request7import warnings8from collections.abc import MutableMapping9from http.cookiejar import Cookie, CookieJar10from urllib.parse import parse_qsl, urlencode11
689 unmodified lines
701 return f"<{class_name}({self.method!r}, {url!r})>"702703 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.content
208 unmodified lines
918 """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._content924925 @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_bytes932933 @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_raw940924 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()952953 async def stream_text(self) -> typing.AsyncIterator[str]:
2 unmodified lines
956 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()963964 async def stream_lines(self) -> typing.AsyncIterator[str]:
3 unmodified lines
968 yield line969 for line in decoder.flush():970 yield line971955 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 lines
modified:tests/client/test_client.py+2 added-2 removed
69 unmodified lines
70 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!"7778
2 unmodified lines
81 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 lines
modified:tests/models/test_responses.py+4 added-4 removed
135 unmodified lines
136async def test_raw_interface():137 response = httpx.Response(200, content=b"Hello, world!")138139 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!"143144
1 unmodified line
146async def test_stream_interface():147 response = httpx.Response(200, content=b"Hello, world!")148149 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!"153154
27 unmodified lines
182183 await response.read()184185 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!"189190
15 unmodified lines
206async def test_cannot_read_after_stream_consumed():207 response = httpx.Response(200, content=async_streaming_body())208209 content = b""210 async for part in response.stream():210 async for part in response.stream_bytes():211 content += part212213 with pytest.raises(httpx.StreamConsumed):214 await response.read()
73 unmodified lines