From 47a5819df4621bb486bc368e75e6d26b3d892016 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Thu, 5 Nov 2020 13:27:41 -0800 Subject: [PATCH 1/2] Raise error on HTTP Error and fix json w/ no path --- adafruit_pyportal.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index 75744dc..3d019a2 100755 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -938,9 +938,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( @@ -949,7 +946,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 OSError( + "HTTP {}: {}".format(r.status_code, r.reason.decode("utf-8")) + ) if self._debug and content_type == CONTENT_TEXT: print(r.text) @@ -993,11 +992,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: From 7e44178bd06f1623b44c3402abbfeb265975da7c Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Fri, 6 Nov 2020 11:02:57 -0800 Subject: [PATCH 2/2] Change to HttpError and check status in wget --- adafruit_pyportal.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index 3d019a2..bc02891 100755 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -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.""" @@ -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: @@ -946,8 +965,8 @@ 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 - raise OSError( - "HTTP {}: {}".format(r.status_code, r.reason.decode("utf-8")) + raise HttpError( + "Code {}: {}".format(r.status_code, r.reason.decode("utf-8")) ) if self._debug and content_type == CONTENT_TEXT: