Skip to content

Commit

Permalink
Minor decoder refactoring. (#1276)
Browse files Browse the repository at this point in the history
* Switch auth/redirect methods to follow flow of execution better

* Drop response.decoder property

* Decoder -> ContentDecoder

* Decoder -> ContentDecoder
  • Loading branch information
tomchristie authored Sep 10, 2020
1 parent 59074c7 commit 6006721
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
14 changes: 7 additions & 7 deletions httpx/_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
brotli = None


class Decoder:
class ContentDecoder:
def decode(self, data: bytes) -> bytes:
raise NotImplementedError() # pragma: nocover

def flush(self) -> bytes:
raise NotImplementedError() # pragma: nocover


class IdentityDecoder(Decoder):
class IdentityDecoder(ContentDecoder):
"""
Handle unencoded data.
"""
Expand All @@ -35,7 +35,7 @@ def flush(self) -> bytes:
return b""


class DeflateDecoder(Decoder):
class DeflateDecoder(ContentDecoder):
"""
Handle 'deflate' decoding.
Expand Down Expand Up @@ -64,7 +64,7 @@ def flush(self) -> bytes:
raise ValueError(str(exc))


class GZipDecoder(Decoder):
class GZipDecoder(ContentDecoder):
"""
Handle 'gzip' decoding.
Expand All @@ -87,7 +87,7 @@ def flush(self) -> bytes:
raise ValueError(str(exc))


class BrotliDecoder(Decoder):
class BrotliDecoder(ContentDecoder):
"""
Handle 'brotli' decoding.
Expand Down Expand Up @@ -132,12 +132,12 @@ def flush(self) -> bytes:
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.
Expand Down
17 changes: 9 additions & 8 deletions httpx/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ._content_streams import ByteStream, ContentStream, encode
from ._decoders import (
SUPPORTED_DECODERS,
Decoder,
ContentDecoder,
IdentityDecoder,
LineDecoder,
MultiDecoder,
Expand Down Expand Up @@ -799,14 +799,13 @@ def apparent_encoding(self) -> typing.Optional[str]:
"""
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()
Expand Down Expand Up @@ -921,10 +920,11 @@ def iter_bytes(self) -> typing.Iterator[bytes]:
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]:
"""
Expand Down Expand Up @@ -1004,10 +1004,11 @@ async def aiter_bytes(self) -> typing.AsyncIterator[bytes]:
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]:
"""
Expand Down

0 comments on commit 6006721

Please sign in to comment.