]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Added support for Google's Brotli library (#156)
authorhalbow <39669025+halbow@users.noreply.github.com>
Sat, 27 Jul 2019 16:32:01 +0000 (18:32 +0200)
committerSeth Michael Larson <sethmichaellarson@gmail.com>
Sat, 27 Jul 2019 16:32:01 +0000 (11:32 -0500)
* Ignore coverage for brotli decompression

* Updated assert comment for brotli and brotlipy

httpx/decoders.py

index 417f3177323f0845ecb114873f45413b37801edd..a2d43fa9acfdb0e5ae11443cec1c182bc3c86c2d 100644 (file)
@@ -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