Skip to content

Commit

Permalink
Workaround to support both brotli and brotlipy (#11)
Browse files Browse the repository at this point in the history
* process -> compress

* Workaround to support both brotli and brotlipy

* Simplify dependencies
  • Loading branch information
kylebarron authored Sep 29, 2020
1 parent ab30b4f commit dba50e0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
extras-install: [test_brotli, test_brotlipy]
python-version: [3.6, 3.7, 3.8]

steps:
Expand All @@ -28,7 +29,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
pip install '.[${{ matrix.extras-install }}]'
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
Expand Down
18 changes: 15 additions & 3 deletions brotli_asgi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async def send_with_brotli(self, message: Message) -> None:
await self.send(message)
elif not more_body:
# Standard Brotli response.
body = self.br_file.process(body) + self.br_file.finish()
body = self._process(body) + self.br_file.finish()
headers = MutableHeaders(raw=self.initial_message["headers"])
headers["Content-Encoding"] = "br"
headers["Content-Length"] = str(len(body))
Expand All @@ -133,7 +133,7 @@ async def send_with_brotli(self, message: Message) -> None:
headers["Content-Encoding"] = "br"
headers.add_vary_header("Accept-Encoding")
del headers["Content-Length"]
self.br_buffer.write(self.br_file.process(body) + self.br_file.flush())
self.br_buffer.write(self._process(body) + self.br_file.flush())

message["body"] = self.br_buffer.getvalue()
self.br_buffer.seek(0)
Expand All @@ -145,7 +145,7 @@ async def send_with_brotli(self, message: Message) -> None:
# Remaining body in streaming Brotli response.
body = message.get("body", b"")
more_body = message.get("more_body", False)
self.br_buffer.write(self.br_file.process(body) + self.br_file.flush())
self.br_buffer.write(self._process(body) + self.br_file.flush())
if not more_body:
self.br_buffer.write(self.br_file.finish())
message["body"] = self.br_buffer.getvalue()
Expand All @@ -157,6 +157,18 @@ async def send_with_brotli(self, message: Message) -> None:
self.br_buffer.truncate()
await self.send(message)

def _process(self, body):
"""Workaround to support both brotli and brotlipy
Before the official Google brotli repository offered a Python version,
there was a separate package to connect to brotli. These APIs are nearly
identical except that the official Google API has Compressor.process
while the brotlipy API has Compress.compress
"""
if hasattr(self.br_file, 'process'):
return self.br_file.process(body)

return self.br_file.compress(body)

async def unattached_send(message: Message) -> None:
raise RuntimeError("send awaitable not set") # pragma: no cover
5 changes: 0 additions & 5 deletions requirements.txt

This file was deleted.

6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

from setuptools import setup # type: ignore

extras = {
'test_brotli': ['requests==2.23.0', 'mypy==0.770'],
'test_brotlipy': ['requests==2.23.0', 'mypy==0.770', 'brotlipy==0.7.0']
}

setup(
name="brotli-asgi",
version="0.4",
Expand All @@ -19,6 +24,7 @@
python_requires=">=3.6",
include_package_data=True,
install_requires=["starlette>=0.13.4", "brotli>=1.0.7"],
extras_require=extras,
platforms="any",
zip_safe=False,
classifiers=[
Expand Down

0 comments on commit dba50e0

Please sign in to comment.