Skip to content
Myr

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

2 files+16 added−15 removed

modified:httpx/_decoders.py+7 added-7 removed
13 unmodified lines
14except ImportError: # pragma: nocover15 brotli = None161718class Decoder:18class ContentDecoder:19 def decode(self, data: bytes) -> bytes:20 raise NotImplementedError() # pragma: nocover2122 def flush(self) -> bytes:23 raise NotImplementedError() # pragma: nocover242526class IdentityDecoder(Decoder):26class IdentityDecoder(ContentDecoder):27 """28 Handle unencoded data.29 """30
3 unmodified lines
34 def flush(self) -> bytes:35 return b""363738class DeflateDecoder(Decoder):38class DeflateDecoder(ContentDecoder):39 """40 Handle 'deflate' decoding.4142 See: https://stackoverflow.com/questions/1838699
20 unmodified lines
63 except zlib.error as exc: # pragma: nocover64 raise ValueError(str(exc))656667class GZipDecoder(Decoder):67class GZipDecoder(ContentDecoder):68 """69 Handle 'gzip' decoding.7071 See: https://stackoverflow.com/questions/1838699
14 unmodified lines
86 except zlib.error as exc: # pragma: nocover87 raise ValueError(str(exc))888990class BrotliDecoder(Decoder):90class BrotliDecoder(ContentDecoder):91 """92 Handle 'brotli' decoding.9394 Requires `pip install brotlipy`. See: https://brotlipy.readthedocs.io/
36 unmodified lines
131 except brotli.error as exc: # pragma: nocover132 raise ValueError(str(exc))133134135class MultiDecoder(Decoder):135class MultiDecoder(ContentDecoder):136 """137 Handle the case where multiple encodings have been applied.138 """139140 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 lines
modified:httpx/_models.py+9 added-8 removed
15 unmodified lines
1617from ._content_streams import ByteStream, ContentStream, encode18from ._decoders import (19 SUPPORTED_DECODERS,20 Decoder,20 ContentDecoder,21 IdentityDecoder,22 LineDecoder,23 MultiDecoder,24 TextDecoder,
773 unmodified lines
798 Return the encoding, as it appears to autodetection.799 """800 return chardet.detect(self.content)["encoding"]801802 @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 lines
919 """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()928929 def iter_text(self) -> typing.Iterator[str]:930 """931 A str-iterator over the decoded response content
71 unmodified lines
1003 """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()10121013 async def aiter_text(self) -> typing.AsyncIterator[str]:1014 """1015 A str-iterator over the decoded response content
238 unmodified lines