]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Minor decoder refactoring. (#1276)
authorTom Christie <tom@tomchristie.com>
Thu, 10 Sep 2020 14:10:31 +0000 (15:10 +0100)
committerGitHub <noreply@github.com>
Thu, 10 Sep 2020 14:10:31 +0000 (15:10 +0100)
* Switch auth/redirect methods to follow flow of execution better

* Drop response.decoder property

* Decoder -> ContentDecoder

* Decoder -> ContentDecoder

httpx/_decoders.py
httpx/_models.py

index 6c0c4492f97fed5f3464773bad5d1f7a4a2ff60e..40c6da9fc0f8782b3bc3dfcc1f6961b5a9e534f8 100644 (file)
@@ -15,7 +15,7 @@ except ImportError:  # pragma: nocover
     brotli = None
 
 
-class Decoder:
+class ContentDecoder:
     def decode(self, data: bytes) -> bytes:
         raise NotImplementedError()  # pragma: nocover
 
@@ -23,7 +23,7 @@ class Decoder:
         raise NotImplementedError()  # pragma: nocover
 
 
-class IdentityDecoder(Decoder):
+class IdentityDecoder(ContentDecoder):
     """
     Handle unencoded data.
     """
@@ -35,7 +35,7 @@ class IdentityDecoder(Decoder):
         return b""
 
 
-class DeflateDecoder(Decoder):
+class DeflateDecoder(ContentDecoder):
     """
     Handle 'deflate' decoding.
 
@@ -64,7 +64,7 @@ class DeflateDecoder(Decoder):
             raise ValueError(str(exc))
 
 
-class GZipDecoder(Decoder):
+class GZipDecoder(ContentDecoder):
     """
     Handle 'gzip' decoding.
 
@@ -87,7 +87,7 @@ class GZipDecoder(Decoder):
             raise ValueError(str(exc))
 
 
-class BrotliDecoder(Decoder):
+class BrotliDecoder(ContentDecoder):
     """
     Handle 'brotli' decoding.
 
@@ -132,12 +132,12 @@ class BrotliDecoder(Decoder):
             raise ValueError(str(exc))
 
 
-class MultiDecoder(Decoder):
+class MultiDecoder(ContentDecoder):
     """
     Handle the case where multiple encodings have been applied.
     """
 
-    def __init__(self, children: typing.Sequence[Decoder]) -> None:
+    def __init__(self, children: typing.Sequence[ContentDecoder]) -> None:
         """
         'children' should be a sequence of decoders in the order in which
         each was applied.
index 65db9ae8b8629d270e3f8d455b8ec7654c47773b..6d90f90b4efb044bdf5556885378cb4da7e4158c 100644 (file)
@@ -17,7 +17,7 @@ import rfc3986.exceptions
 from ._content_streams import ByteStream, ContentStream, encode
 from ._decoders import (
     SUPPORTED_DECODERS,
-    Decoder,
+    ContentDecoder,
     IdentityDecoder,
     LineDecoder,
     MultiDecoder,
@@ -799,14 +799,13 @@ class Response:
         """
         return chardet.detect(self.content)["encoding"]
 
-    @property
-    def decoder(self) -> Decoder:
+    def _get_content_decoder(self) -> ContentDecoder:
         """
         Returns a decoder instance which can be used to decode the raw byte
         content, depending on the Content-Encoding used in the response.
         """
         if not hasattr(self, "_decoder"):
-            decoders: typing.List[Decoder] = []
+            decoders: typing.List[ContentDecoder] = []
             values = self.headers.get_list("content-encoding", split_commas=True)
             for value in values:
                 value = value.strip().lower()
@@ -921,10 +920,11 @@ class Response:
         if hasattr(self, "_content"):
             yield self._content
         else:
+            decoder = self._get_content_decoder()
             with self._wrap_decoder_errors():
                 for chunk in self.iter_raw():
-                    yield self.decoder.decode(chunk)
-                yield self.decoder.flush()
+                    yield decoder.decode(chunk)
+                yield decoder.flush()
 
     def iter_text(self) -> typing.Iterator[str]:
         """
@@ -1004,10 +1004,11 @@ class Response:
         if hasattr(self, "_content"):
             yield self._content
         else:
+            decoder = self._get_content_decoder()
             with self._wrap_decoder_errors():
                 async for chunk in self.aiter_raw():
-                    yield self.decoder.decode(chunk)
-                yield self.decoder.flush()
+                    yield decoder.decode(chunk)
+                yield decoder.flush()
 
     async def aiter_text(self) -> typing.AsyncIterator[str]:
         """