public
Last active

  • Download Gist
api_runner.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>API Web Runner</title>
 
<link href="bootstrap20/css/bootstrap.min.css" rel="stylesheet" />
<link href="bootstrap20/css/bootstrap-responsive.min.css" rel="stylesheet" />
<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.22/themes/redmond/jquery-ui.css" rel="stylesheet" />
<style type="text/css">
/* Override some defaults */
body {
padding-top: 50px; /* 40px to make the container go all the way to the bottom of the topbar */
}
.container > footer p {
text-align: center; /* center align it with the container */
}
.container {
width: 820px; /* downsize our container to make the content feel a bit tighter and more cohesive. NOTE: this removes two full columns from the grid, meaning you only go to 14 columns and not 16. */
}
.ui-menu-item > a > small {
font-size: 10px;
}
.ui-menu-item {
border-bottom: 1px solid black;
}
</style>
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">API Web Runner</a>
</div>
</div>
</div>
 
<div class="container">
<header>
<h1>API Web Runner</h1>
<p>Simple interface to make requests to interal API.</p>
</header>
<div class="row">
<form class="well">
<div class="row">
<div class="span1">
<label for="request-method">Method</label>
<select id="request-method" class="span1">
<option value="GET">GET</option>
<option value="POST">POST</option>
<option value="PUT">PUT</option>
<option value="DELETE">DELETE</option>
</select>
</div>
<div class="span4">
<label for="request-url">URL</label>
<input id="request-url" type="text" class="span4" placeholder="Type the URL starting with slash">
</div>
</div>
 
<div class="row">
<div class="span1">
<label>Locale</label>
<input id="request-locale" type="text" class="span1" value="en-US">
</div>
<div class="span6">
<label>Requester Username</label>
<input id="request-username" type="text" class="span3">
<span class="help-inline">Leave empty for anonymous request</span>
</div>
</div>
 
<div class="row">
<div class="span7">
<table id="request-params" class="table table-striped">
<thead>
<tr>
<th>Parameter</th>
<th>Value</th>
<th>Action</th>
</tr>
</thead>
<tbody></tbody>
</table>
<button id="request-add-param" class="btn">Add Param</button>
</div>
</div>
 
<br><br>
<button id="request-submit" type="submit" class="btn">Request</button>
</form>
 
<div id="response" class="well" style="display: none">
<h2>Response</h2>
<p>Code: <span id="response-code">0</span></p>
<pre id="response-body"></pre>
</div>
 
<div id="response-log" class="well" style="display: none">
<h2>Log messages</h2>
<table id="response-log-messages" class="table table-striped">
<thead>
<tr>
<th>Message</th>
<th>Type</th>
<th>Data</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<footer>
<p>&copy; Foo Bar Company</p>
</footer>
</div>
 
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.22/jquery-ui.min.js"></script>
<script type="text/javascript" src="bootstrap20/js/bootstrap.min.js"></script>
<script type="text/javascript">
var searchList = {
GET: [],
POST: [],
PUT: [],
DELETE: []
};
 
function buildUriRegex(uri, params) {
// Convert the URI to RegExp string
for (var param in params) {
uri = uri.replace('/' + params[param].toUpperCase(), '/(.+)');
}
 
// Extract the pieces and transform variables in RegExp objects
var pieces = uri.substring(1).split('/');
for (piece in pieces) {
if (pieces[piece] === '(.+)') {
pieces[piece] = new RegExp(pieces[piece]);
}
}
return pieces;
}
 
function buildSearch(data) {
var controller, method;
if (!data.data) {
return;
}
for (var controllerIndex in data.data) {
controller = data.data[controllerIndex];
for (var methodIndex in controller) {
method = controller[methodIndex];
searchList[method.http_method].push({
uri: method.uri,
uriParams: method.params,
uriRegex: buildUriRegex(method.uri, method.params),
description: method.doc_comment.description || '',
params: method.doc_comment.tags.postParam || method.doc_comment.tags.inputParam || {}
});
}
}
enableAutocomplete();
}
 
function enableAutocomplete() {
var $url = $('#request-url');
 
$url.autocomplete({
source: function(criteria, response) {
var SUGGESTION_LIMIT = 8,
method = $('#request-method').val(),
userUrl = criteria.term,
userUrlPieces = userUrl.substring(1).split('/'),
uris = [],
searchMethod;
 
// Ignore empty or just /
if (userUrl.length < 2) {
return;
}
 
// Remove the last element if it is empty
if (userUrlPieces[userUrlPieces.length - 1] == "") {
userUrlPieces.pop();
}
 
loopMethods:
for (var i in searchList[method]) {
var userPiecesLength = userUrlPieces.length;
searchMethod = searchList[method][i];
 
// Ignore if the number of pieces in method is lesser than user input
if (searchMethod.uriRegex.length < userPiecesLength) {
continue;
}
 
loopUserPieces:
for (var userPieceIndex in userUrlPieces) {
var methodPiece = searchMethod.uriRegex[userPieceIndex],
userPiece = userUrlPieces[userPieceIndex];
 
// Just need to validate the strings, the objects will always match
if (typeof(methodPiece) === 'string') {
if (userPieceIndex == userPiecesLength - 1) {
// Last piece can be partial
if (methodPiece.substring(0, userPiece.length) != userPiece) {
continue loopMethods;
}
} else if (methodPiece != userPiece) {
continue loopMethods;
}
}
}
 
// Nothing else against add the uri in the list
uris.push({
label: searchMethod.uri,
value: i,
method: searchMethod
});
if (uris.length >= SUGGESTION_LIMIT) {
break;
}
}
response(uris);
},
focus: function(event, ui) {
// Do not change the input field while selecting
return false;
},
select: function(event, ui) {
// Let's make a mess with the user url
var $url = $('#request-url'),
userPieces = $url.val().substring(1).split('/'),
userPiecesLength = userPieces.length,
method = ui.item.method,
newUrl = '';
 
if (method.uriParams.length == 0) {
newUrl = method.uri;
} else {
var paramPos = 0;
for (var i in method.uriRegex) {
if (typeof(method.uriRegex[i]) === 'string') {
newUrl += '/' + method.uriRegex[i];
} else {
newUrl += '/' + (userPieces[i] || method.uriParams[paramPos].toUpperCase());
paramPos++;
}
}
}
$url.val(newUrl);
 
// Set the params
cleanParams();
for (var key in ui.item.method.params) {
addParam(key, '', ui.item.method.params[key].description, ui.item.method.params[key].optional);
}
 
return false;
}
}).data('autocomplete')._renderItem = function (ul, item) {
return $('<li></li>')
.data('item.autocomplete', item)
.append('<a><strong>' + item.method.uri + '</strong><br><small>' + item.method.description + '</small></a>')
.appendTo(ul);
};
}
 
function loadDefaultParams() {
var urlParams = {};
(function () {
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);
 
while (match = search.exec(query)) {
urlParams[decode(match[1])] = decode(match[2]);
}
})();
 
if (urlParams['method']) {
$('#request-method').val(urlParams['method']);
}
if (urlParams['uri']) {
$('#request-url').val(urlParams['uri']);
}
}
 
function getSubmitParams() {
var params = {};
$('#request-params > tbody > tr').each(function(index) {
var name = $(this).find('.request-param-name').val(),
value = $(this).find('.request-param-value').val();
 
if (!name) {
return;
}
params[name] = value;
});
return params;
}
 
function cleanParams() {
$('#request-params > tbody').html('');
}
 
function addParam(key, value, help, optional) {
var $paramRows = $('#request-params > tbody'),
tr;
 
if (optional === false) {
help = '<i class="icon-exclamation-sign"></i> ' + help;
}
 
tr = '<tr>';
tr += '<td><input class="request-param-name span2" value="' + key + '"></td>';
tr += '<td><input class="request-param-value span4" value="' + value + '"><span class="help-block">' + help + '</span></td>';
tr += '<td><a href="#" class="request-param-remove">Remove</td>';
tr += '</tr>';
$paramRows.append(tr);
}
 
function serviceRequest(url) {
var serviceRequest, headers = {};
 
// Set the headers
if ($('#request-locale').val()) {
headers['Accept-Language'] = $('#request-locale').val();
}
if ($('#request-username').val()) {
headers['X-Requested-Username'] = $('#request-username').val();
}
headers['Accept'] = 'application/vnd.custom.api-v1.0';
 
serviceRequest = $.ajax({
type: $('#request-method').val(),
url: url,
cache: false,
headers: headers,
data: getSubmitParams()
}).always(function(data, status) {
parseServiceResponse(serviceRequest, status, data);
parseServiceResponseLog(serviceRequest);
});
}
 
function parseServiceResponse(xhr, status, data) {
$('#response').show();
$('#response-code').text(xhr.status + " (" + xhr.statusText + ")");
if (status !== "success") {
data = JSON.parse(data.responseText);
}
$('#response-body').text(JSON.stringify(data, null, 4));
}
 
function parseServiceResponseLog(xhr) {
var log = xhr.getResponseHeader('X-ChromePhp-Data');
if (!log) {
$('#response-log').hide();
return;
}
 
var tableRows = $('#response-log-messages > tbody');
$('#response-log').show();
tableRows.html('');
log = JSON.parse(atob(log));
for (var i in log.rows) {
var row = log.rows[i],
tr;
tr = '<tr><td>' + row[0] + '</td><td>' + row[3] + '</td>';
tr = tr + '<td><pre>' + JSON.stringify(row[1], null, 4) + '</pre></td></tr>';
tableRows.append(tr);
}
}
 
$(document).ready(function() {
var jqxhr = $.ajax({
url: '/discover',
dataType: 'json'
}).done(buildSearch);
 
loadDefaultParams();
 
$('#request-add-param').on('click', function() {
addParam('', '', '', null);
return false;
});
 
$(document).on('click', '.request-param-remove', function(event) {
$(event.currentTarget).parents('tr').remove();
return false;
});
 
$('form').on('submit', function() {
var url = $('#request-url').val();
 
if (!url) {
alert('Where is the URL???');
return false;
}
 
serviceRequest(url);
return false;
});
});
</script>
</body>
</html>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.