Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor decoder refactoring. #1276

Merged
merged 5 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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