Minor decoder refactoring. (#1276)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Switch auth/redirect methods to follow flow of execution better * Drop response.decoder property * Decoder -> ContentDecoder * Decoder -> ContentDecoder
authored by Tom Christie · committed by GitHub · parent 59074c7 · Browse files · 2 files·+16 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+16 added−15 removed
- other:+16 added−15 removed
Provenance
- Commit
6006721- Tree
5c8b20b- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
59074c7
2 files changed+16 added−15 removed
modified:httpx/_decoders.py+7 added-7 removed
13 unmodified lines14 except ImportError: # pragma: nocover15 brotli = None16 17 18-class Decoder:18+class ContentDecoder:19 def decode(self, data: bytes) -> bytes:20 raise NotImplementedError() # pragma: nocover21 22 def flush(self) -> bytes:23 raise NotImplementedError() # pragma: nocover24 25 26-class IdentityDecoder(Decoder):26+class IdentityDecoder(ContentDecoder):27 """28 Handle unencoded data.29 """30 3 unmodified lines34 def flush(self) -> bytes:35 return b""36 37 38-class DeflateDecoder(Decoder):38+class DeflateDecoder(ContentDecoder):39 """40 Handle 'deflate' decoding.41 42 See: https://stackoverflow.com/questions/183869920 unmodified lines63 except zlib.error as exc: # pragma: nocover64 raise ValueError(str(exc))65 66 67-class GZipDecoder(Decoder):67+class GZipDecoder(ContentDecoder):68 """69 Handle 'gzip' decoding.70 71 See: https://stackoverflow.com/questions/183869914 unmodified lines86 except zlib.error as exc: # pragma: nocover87 raise ValueError(str(exc))88 89 90-class BrotliDecoder(Decoder):90+class BrotliDecoder(ContentDecoder):91 """92 Handle 'brotli' decoding.93 94 Requires `pip install brotlipy`. See: https://brotlipy.readthedocs.io/36 unmodified lines131 except brotli.error as exc: # pragma: nocover132 raise ValueError(str(exc))133 134 135-class MultiDecoder(Decoder):135+class MultiDecoder(ContentDecoder):136 """137 Handle the case where multiple encodings have been applied.138 """139 140- def __init__(self, children: typing.Sequence[Decoder]) -> None:140+ def __init__(self, children: typing.Sequence[ContentDecoder]) -> None:141 """142 'children' should be a sequence of decoders in the order in which143 each was applied.144 """156 unmodified linesmodified:httpx/_models.py+9 added-8 removed
15 unmodified lines16 17 from ._content_streams import ByteStream, ContentStream, encode18 from ._decoders import (19 SUPPORTED_DECODERS,20- Decoder,20+ ContentDecoder,21 IdentityDecoder,22 LineDecoder,23 MultiDecoder,24 TextDecoder,773 unmodified lines798 Return the encoding, as it appears to autodetection.799 """800 return chardet.detect(self.content)["encoding"]801 802- @property803- def decoder(self) -> Decoder:802+ def _get_content_decoder(self) -> ContentDecoder:803 """804 Returns a decoder instance which can be used to decode the raw byte805 content, depending on the Content-Encoding used in the response.806 """807 if not hasattr(self, "_decoder"):809- decoders: typing.List[Decoder] = []808+ decoders: typing.List[ContentDecoder] = []809 values = self.headers.get_list("content-encoding", split_commas=True)810 for value in values:811 value = value.strip().lower()812 try:106 unmodified lines919 """920 if hasattr(self, "_content"):921 yield self._content922 else:923+ decoder = self._get_content_decoder()924 with self._wrap_decoder_errors():925 for chunk in self.iter_raw():926- yield self.decoder.decode(chunk)927- yield self.decoder.flush()926+ yield decoder.decode(chunk)927+ yield decoder.flush()928 929 def iter_text(self) -> typing.Iterator[str]:930 """931 A str-iterator over the decoded response content71 unmodified lines1003 """1004 if hasattr(self, "_content"):1005 yield self._content1006 else:1007+ decoder = self._get_content_decoder()1008 with self._wrap_decoder_errors():1009 async for chunk in self.aiter_raw():1009- yield self.decoder.decode(chunk)1010- yield self.decoder.flush()1010+ yield decoder.decode(chunk)1011+ yield decoder.flush()1012 1013 async def aiter_text(self) -> typing.AsyncIterator[str]:1014 """1015 A str-iterator over the decoded response content238 unmodified lines