Skip to content

Raise error on HTTP Error and fix json w/ no path #93

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
Nov 6, 2020
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
36 changes: 30 additions & 6 deletions adafruit_pyportal.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@
CONTENT_IMAGE = const(3)


class HttpError(Exception):
"""HTTP Specific Error"""


class Fake_Requests:
"""For faking 'requests' using a local file instead of the network."""

Expand Down Expand Up @@ -758,10 +762,25 @@ def wget(self, url, filename, *, chunk_size=12000):

self.neo_status((100, 100, 0))
r = requests.get(url, stream=True)

headers = {}
for title, content in r.headers.items():
headers[title.lower()] = content

if r.status_code == 200:
print("Reply is OK!")
self.neo_status((0, 0, 100)) # green = got data
else:
if self._debug:
if "content-length" in headers:
print("Content-Length: {}".format(int(headers["content-length"])))
if "date" in headers:
print("Date: {}".format(headers["date"]))
self.neo_status((100, 0, 0)) # red = http error
raise HttpError(
"Code {}: {}".format(r.status_code, r.reason.decode("utf-8"))
)

if self._debug:
print(headers)
if "content-length" in headers:
Expand Down Expand Up @@ -938,9 +957,6 @@ def fetch(self, refresh_url=None, timeout=10):
elif "application/javascript" in headers["content-type"]:
content_type = CONTENT_JSON
else:
print(
"HTTP Error {}: {}".format(r.status_code, r.reason.decode("utf-8"))
)
if self._debug:
if "content-length" in headers:
print(
Expand All @@ -949,7 +965,9 @@ def fetch(self, refresh_url=None, timeout=10):
if "date" in headers:
print("Date: {}".format(headers["date"]))
self.neo_status((100, 0, 0)) # red = http error
return None
raise HttpError(
"Code {}: {}".format(r.status_code, r.reason.decode("utf-8"))
)

if self._debug and content_type == CONTENT_TEXT:
print(r.text)
Expand Down Expand Up @@ -993,11 +1011,17 @@ def fetch(self, refresh_url=None, timeout=10):
except KeyError:
print(json_out)
raise
elif self._regexp_path:
elif content_type == CONTENT_TEXT and self._regexp_path:
for regexp in self._regexp_path:
values.append(re.search(regexp, r.text).group(1))
else:
values = r.text
if content_type == CONTENT_JSON:
# No path given, so return JSON as string for compatibility
import json # pylint: disable=import-outside-toplevel

values = json.dumps(r.json())
else:
values = r.text

if self._image_json_path:
try:
Expand Down