From 2cc8beabfc905da8209d43f8e8ceb0b2e173a893 Mon Sep 17 00:00:00 2001 From: halbow <39669025+halbow@users.noreply.github.com> Date: Sat, 27 Jul 2019 18:32:01 +0200 Subject: [PATCH] Added support for Google's Brotli library (#156) * Ignore coverage for brotli decompression * Updated assert comment for brotli and brotlipy --- httpx/decoders.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/httpx/decoders.py b/httpx/decoders.py index 417f3177..a2d43fa9 100644 --- a/httpx/decoders.py +++ b/httpx/decoders.py @@ -85,25 +85,31 @@ class BrotliDecoder(Decoder): Handle 'brotli' decoding. Requires `pip install brotlipy`. See: https://brotlipy.readthedocs.io/ + or `pip install brotli`. See https://github.com/google/brotli + Supports both 'brotlipy' and 'Brotli' packages since they share an import + name. The top branches are for 'brotlipy' and bottom branches for 'Brotli' """ def __init__(self) -> None: assert ( brotli is not None - ), "The 'brotlipy' library must be installed to use 'BrotliDecoder'" + ), "The 'brotlipy' or 'brotli' library must be installed to use 'BrotliDecoder'" self.decompressor = brotli.Decompressor() def decode(self, data: bytes) -> bytes: try: - return self.decompressor.decompress(data) - except brotli.Error as exc: + if hasattr(self.decompressor, "decompress"): + return self.decompressor.decompress(data) + return self.decompressor.process(data) # pragma: nocover + except brotli.error as exc: raise DecodingError from exc def flush(self) -> bytes: try: - self.decompressor.finish() + if hasattr(self.decompressor, "finish"): + self.decompressor.finish() return b"" - except brotli.Error as exc: # pragma: nocover + except brotli.error as exc: # pragma: nocover raise DecodingError from exc -- 2.47.3