]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
stream -> stream_bytes, raw -> stream_raw (#599)
authorTom Christie <tom@tomchristie.com>
Thu, 5 Dec 2019 11:39:28 +0000 (11:39 +0000)
committerGitHub <noreply@github.com>
Thu, 5 Dec 2019 11:39:28 +0000 (11:39 +0000)
docs/api.md
httpx/models.py
tests/client/test_client.py
tests/models/test_responses.py

index b4e567086740c4b4a6c5cc045d2caad2513a6f0a..2dff987f74e3a696a60e67b563b106983355914a 100644 (file)
 * `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**
 
index d702903546c1d7192f719864042c61ffee026362..0d7c6fc13e4a7ad5a813f909c342b5992e6de178 100644 (file)
@@ -4,6 +4,7 @@ import email.message
 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
@@ -701,7 +702,7 @@ class Request:
 
     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()])
@@ -918,10 +919,26 @@ class Response:
         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.
@@ -929,7 +946,7 @@ class Response:
         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()
 
@@ -940,7 +957,7 @@ class Response:
         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()
 
@@ -952,7 +969,7 @@ class Response:
         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.
         """
index 5514f6d67a1c713ef1c3a4d25c01bf08d6e32be4..668ae307096e8e8086055bcda4858d334b539bec 100644 (file)
@@ -71,7 +71,7 @@ async def test_stream_iterator(server):
         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!"
 
@@ -82,7 +82,7 @@ async def test_raw_iterator(server):
         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()
index 9b60c67391fe0ee3607e84b41df064eeecd9945e..1e1fb89cb333abfe9b00e70818afc62d2b7d4ffa 100644 (file)
@@ -137,7 +137,7 @@ async def test_raw_interface():
     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!"
 
@@ -147,7 +147,7 @@ async def test_stream_interface():
     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!"
 
@@ -183,7 +183,7 @@ async def test_stream_interface_after_read():
     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!"
 
@@ -207,7 +207,7 @@ async def test_cannot_read_after_stream_consumed():
     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):