Prettier debug json (#260)

This commit is contained in:
Michael Manfre 2022-12-24 13:05:07 -05:00 committed by GitHub
parent 4339b09dd4
commit 5536397bdb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 140 additions and 14 deletions

View file

@ -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)

View file

@ -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;
}

View file

@ -16,10 +16,76 @@
</form>
{% if result %}
<p>Canonacalized Response: (view source for raw)</p>
<div id="raw-json-result" style="display: none;">
{{ raw_result|escape }}
<div class="debug">
<h2>Summary</h2>
<div class="debug-section">
<div class="field">
<span class="name">Status Code:</span>
<span class="value">{{ status_code }}</span>
</div>
<div class="field">
<span class="name">Content-Type:</span>
<span class="value">{{ content_type }}</span>
</div>
<div class="field">
<span class="name">Bytes Downloaded:</span>
<span class="value">{{ num_bytes_downloaded }}</span>
</div>
</div>
<pre>{{ result }}</pre>
<h2>Payload</h2>
<div class="debug-section">
<div class="field payload">
<span class="name">Raw Response:
<a title="Copy Content"
class="copy"
_="on click
writeText(#raw_response.innerText) into the navigator's clipboard
then add .copied
wait 2s
then remove .copied">
<i class="fa-solid fa-copy"></i>
</a>
</span>
<span class="value">
<a _="on click
toggle .hidden on #raw_response
then
if my.innerText is 'Hide' set my.innerText to 'Show'
else set my.innerText to 'Hide'
">Show</a></span>
</div>
<div id="raw_response" class="hidden">
{{ raw_result|escape }}
</div>
<div class="field payload">
<span class="name">Canonical:
<a title="Copy Content"
class="copy"
_="on click
writeText(#canonical_response.innerText) into the navigator's clipboard
then add .copied
wait 2s
then remove .copied">
<i class="fa-solid fa-copy"></i>
</a>
</span>
<span class="value">
<a _="on click
toggle .hidden on #canonical_response
then
if my.innerText is 'Hide' set my.innerText to 'Show'
else set my.innerText to 'Hide'
">Show</a></span>
</div>
<pre id="canonical_response" class="hidden">{{ result }}</pre>
</div>
</div>
{% endif %}
{% endblock %}

View file

@ -642,6 +642,10 @@ class Identity(StatorModel):
)
except httpx.RequestError:
return False
content_type = response.headers.get("content-type")
if content_type and "html" in content_type:
# Some servers don't properly handle "application/activity+json"
return False
if response.status_code == 410:
# Their account got deleted, so let's do the same.
if self.pk: