Skip to content

raise error indicating gzipped data #100

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

Merged
merged 2 commits into from
Feb 15, 2022
Merged
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
17 changes: 17 additions & 0 deletions adafruit_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,15 @@ def text(self) -> str:
if isinstance(self._cached, str):
return self._cached
raise RuntimeError("Cannot access text after getting content or json")

if (
"content-encoding" in self.headers
and self.headers["content-encoding"] == "gzip"
):
raise ValueError(
"Content-encoding is gzip, data cannot be accessed as json or text. "
"Use content property to access raw bytes."
)
self._cached = str(self.content, self.encoding)
return self._cached

Expand All @@ -450,6 +459,14 @@ def json(self) -> Any:
if not self._raw:
self._raw = _RawResponse(self)

if (
"content-encoding" in self.headers
and self.headers["content-encoding"] == "gzip"
):
raise ValueError(
"Content-encoding is gzip, data cannot be accessed as json or text. "
"Use content property to access raw bytes."
)
try:
obj = json.load(self._raw)
except OSError:
Expand Down