Skip to content
Myr

Added support for Google's Brotli library (#156)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Ignore coverage for brotli decompression * Updated assert comment for brotli and brotlipy

authored by halbow · committed by Seth Michael Larson · parent f974ebd · Browse files · 1 file+11 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+11 added−5 removed

  • other:+11 added−5 removed

1 file+11 added−5 removed

modified:httpx/decoders.py+11 added-5 removed
83 unmodified lines
84 """85 Handle 'brotli' decoding.8687 Requires `pip install brotlipy`. See: https://brotlipy.readthedocs.io/88 or `pip install brotli`. See https://github.com/google/brotli89 Supports both 'brotlipy' and 'Brotli' packages since they share an import90 name. The top branches are for 'brotlipy' and bottom branches for 'Brotli'91 """9293 def __init__(self) -> None:94 assert (95 brotli is not None93 ), "The 'brotlipy' library must be installed to use 'BrotliDecoder'"96 ), "The 'brotlipy' or 'brotli' library must be installed to use 'BrotliDecoder'"97 self.decompressor = brotli.Decompressor()9899 def decode(self, data: bytes) -> bytes:100 try:101 if hasattr(self.decompressor, "decompress"):102 return self.decompressor.decompress(data)98 return self.decompressor.decompress(data)99 except brotli.Error as exc:103 return self.decompressor.process(data) # pragma: nocover104 except brotli.error as exc:105 raise DecodingError from exc106107 def flush(self) -> bytes:108 try:109 if hasattr(self.decompressor, "finish"):104 self.decompressor.finish()110 self.decompressor.finish()111 return b""106 except brotli.Error as exc: # pragma: nocover112 except brotli.error as exc: # pragma: nocover113 raise DecodingError from exc114115116class MultiDecoder(Decoder):
39 unmodified lines