Skip to content

Commit 47a5819

Browse files
committed
Raise error on HTTP Error and fix json w/ no path
1 parent 1dcd993 commit 47a5819

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

adafruit_pyportal.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -938,9 +938,6 @@ def fetch(self, refresh_url=None, timeout=10):
938938
elif "application/javascript" in headers["content-type"]:
939939
content_type = CONTENT_JSON
940940
else:
941-
print(
942-
"HTTP Error {}: {}".format(r.status_code, r.reason.decode("utf-8"))
943-
)
944941
if self._debug:
945942
if "content-length" in headers:
946943
print(
@@ -949,7 +946,9 @@ def fetch(self, refresh_url=None, timeout=10):
949946
if "date" in headers:
950947
print("Date: {}".format(headers["date"]))
951948
self.neo_status((100, 0, 0)) # red = http error
952-
return None
949+
raise OSError(
950+
"HTTP {}: {}".format(r.status_code, r.reason.decode("utf-8"))
951+
)
953952

954953
if self._debug and content_type == CONTENT_TEXT:
955954
print(r.text)
@@ -993,11 +992,17 @@ def fetch(self, refresh_url=None, timeout=10):
993992
except KeyError:
994993
print(json_out)
995994
raise
996-
elif self._regexp_path:
995+
elif content_type == CONTENT_TEXT and self._regexp_path:
997996
for regexp in self._regexp_path:
998997
values.append(re.search(regexp, r.text).group(1))
999998
else:
1000-
values = r.text
999+
if content_type == CONTENT_JSON:
1000+
# No path given, so return JSON as string for compatibility
1001+
import json # pylint: disable=import-outside-toplevel
1002+
1003+
values = json.dumps(r.json())
1004+
else:
1005+
values = r.text
10011006

10021007
if self._image_json_path:
10031008
try:

0 commit comments

Comments
 (0)