diff --git a/activities/views/debug.py b/activities/views/debug.py index b77a96c..d93190e 100644 --- a/activities/views/debug.py +++ b/activities/views/debug.py @@ -1,4 +1,4 @@ -import pprint +import json import httpx from asgiref.sync import async_to_sync @@ -23,28 +23,42 @@ class JsonViewer(FormView): ) def form_valid(self, form): - raw_result = "" uri = form.cleaned_data["uri"] if "://" not in uri: uri = "https://" + uri + + # Render results + context = self.get_context_data(form=form) + try: response = async_to_sync(SystemActor().signed_request)( method="get", uri=uri, ) - except httpx.RequestError: - result = "Request Error" + except httpx.RequestError as ex: + result = f"Request Error: {str(ex)}" else: - raw_result = response.text + context.update( + { + "status_code": response.status_code, + "content_type": response.headers["content-type"], + "num_bytes_downloaded": response.num_bytes_downloaded, + "charset_encoding": response.charset_encoding, + "raw_result": response.text, + } + ) + if response.status_code >= 400: result = f"Error response: {response.status_code}\n{response.content}" else: - document = canonicalise(response.json(), include_security=True) - result = pprint.pformat(document) - # Render results - context = self.get_context_data(form=form) + try: + document = canonicalise(response.json(), include_security=True) + except json.JSONDecodeError as ex: + result = str(ex) + else: + result = json.dumps(document, indent=4, sort_keys=True) + # result = pprint.pformat(document) context["result"] = result - context["raw_result"] = raw_result return self.render_to_response(context) diff --git a/static/css/style.css b/static/css/style.css index 0de3680..23379b3 100644 --- a/static/css/style.css +++ b/static/css/style.css @@ -1516,3 +1516,45 @@ form .post { position: static; margin: auto; } + + +/* Debug */ +.debug { + width: 100%; +} +.debug h2 { + text-align: center; + padding-bottom: 5px; +} +.debug-section { + text-align: center; +} +.debug-section .field.payload, +#canonical_response, +#raw_response { + margin-bottom: 10px; +} +#canonical_response, #raw_response { + background-color: var(--color-bg-box); + overflow: scroll; + text-align: left; +} +.debug-section .field .name { + display: inline-block; + min-width: 49%; + text-align: right; + padding-right: 10px; +} +.debug-section .field .value { + display: inline-block; + min-width: 49%; + text-align: left; +} + +.debug-section { + margin-bottom: 20px; +} + +.debug-section .hidden { + display: none; +} diff --git a/templates/activities/debug_json.html b/templates/activities/debug_json.html index 5cf3e5e..0811c16 100644 --- a/templates/activities/debug_json.html +++ b/templates/activities/debug_json.html @@ -16,10 +16,76 @@ {% if result %} -
Canonacalized Response: (view source for raw)
-