Created
June 2, 2015 19:35
-
-
Save inklesspen/a7f08169d8326827dfa4 to your computer and use it in GitHub Desktop.
JSON-RPC Panel for pyramid_debugtoolbar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Needs JS/CSS from https://github.com/yesmeck/jquery-jsonview/ | |
from pyramid_debugtoolbar.panels import DebugPanel | |
class JSONRPCPanel(DebugPanel): | |
name = 'jsonrpc' | |
template = 'fnordapp:templates/jsonrpc_panel.dbtmako' | |
title = 'JSON-RPC calls' | |
nav_title = 'JSON-RPC' | |
def __init__(self, request): | |
self.data['is_rpc'] = False | |
self.data['is_batch'] = False | |
self.data['rpc'] = None | |
# this should properly be done in render_vars, once the static files | |
# exist in the right place | |
self.data.update({ | |
'css_url': request.static_url('fnordapp:static/jquery.jsonview.css'), | |
'script_url': request.static_url('fnordapp:static/jquery.jsonview.js'), | |
}) | |
@property | |
def has_content(self): | |
if self.data['is_rpc']: | |
return True | |
else: | |
return False | |
def collect_info(self, request, response): | |
if hasattr(request, 'batched_rpc_requests'): | |
self.data['is_rpc'] = True | |
self.data['is_batch'] = True | |
self.data['rpc'] = [] | |
self.data['extra_responses'] = [] | |
by_id = {} | |
for inner in request.batched_rpc_requests: | |
reconstructed = { | |
'id': inner.get('id'), | |
'version': inner['jsonrpc'], | |
'method': inner['method'], | |
'args': inner['params'], | |
} | |
wrapper = { | |
'request': reconstructed, | |
'response': None, | |
} | |
self.data['rpc'].append(wrapper) | |
if reconstructed['id']: | |
by_id[reconstructed['id']] = wrapper | |
if response.body != b'': | |
for inner in response.json: | |
if 'id' in inner: | |
by_id[inner['id']]['response'] = inner | |
else: | |
self.data['extra_responses'].append(inner) | |
elif hasattr(request, 'rpc_method'): | |
self.data['is_rpc'] = True | |
self.data['is_batch'] = False | |
self.data['rpc'] = { | |
'request': { | |
'id': request.rpc_id, | |
'method': request.rpc_method, | |
'args': request.rpc_args, | |
'version': request.rpc_version, | |
}, | |
'response': None, | |
} | |
if response.body != b'': | |
self.data['rpc']['response'] = response.json | |
def wrap_handler(self, handler): | |
def wrapped(request): | |
response = handler(request) | |
self.collect_info(request, response) | |
return response | |
return wrapped | |
def includeme(config): | |
config.registry.settings['debugtoolbar.panels'].append(JSONRPCPanel) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%! | |
import json | |
_COUNTER = 0 | |
def make_id(): | |
global _COUNTER | |
_COUNTER += 1 | |
return 'id-{0}'.format(_COUNTER) | |
%> | |
<script src="${script_url}"></script> | |
<script> | |
var css_link = $("<link>", { | |
rel: "stylesheet", | |
type: "text/css", | |
href: "${css_url}" | |
}); | |
css_link.appendTo('head'); | |
</script> | |
<%def name="rpc_block(wrapped)"> | |
<h4>Request</h4> | |
${json_block(wrapped['request'])} | |
% if wrapped['response']: | |
<h4>Response</h4> | |
${json_block(wrapped['response'])} | |
% endif | |
</%def> | |
<%def name="json_block(json_data)"> | |
<% current_id = make_id() %> | |
<div id="${current_id}"></div> | |
<script> | |
$("#${current_id}").JSONView(${json_data | n,json.dumps}); | |
</script> | |
</%def> | |
% if is_batch: | |
<h3>Batched Requests</h3> | |
% for inner in rpc: | |
${rpc_block(inner)} | |
% endfor | |
% if extra_responses: | |
<h3>Extra responses</h3> | |
% for extra in extra_responses: | |
${json_block(extra)} | |
% endfor | |
% endif | |
% else: | |
${rpc_block(rpc)} | |
% endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment