Commit b9290efb77c480eaff5809fa87dd3a2272e75e7c
Committed by
Rodrigo Souto
1 parent
1bf49c90
Exists in
api_tasks
and in
1 other branch
Adds API Playground
Showing
4 changed files
with
121 additions
and
0 deletions
Show diff stats
app/controllers/public/api_controller.rb
app/views/api/index.html.erb
| 1 | <h1>EndPoints</h1> | 1 | <h1>EndPoints</h1> |
| 2 | 2 | ||
| 3 | +<div style="float: right"> | ||
| 4 | +<%= s_('api-playground|Try the %s') % link_to('API Playground', '/api/playground') %> | ||
| 5 | +</div> | ||
| 6 | + | ||
| 3 | <%= @api.endpoints.map do |endpoint| | 7 | <%= @api.endpoints.map do |endpoint| |
| 4 | app = endpoint.options[:app].to_s | 8 | app = endpoint.options[:app].to_s |
| 5 | unless app.blank? | 9 | unless app.blank? |
| @@ -0,0 +1,26 @@ | @@ -0,0 +1,26 @@ | ||
| 1 | +<h1>API Playground</h1> | ||
| 2 | + | ||
| 3 | +<script> | ||
| 4 | +var endpoints = <%= | ||
| 5 | +@api.endpoints.map do |endpoint| | ||
| 6 | + app = endpoint.options[:app].to_s | ||
| 7 | + unless app.blank? | ||
| 8 | + endpoint.routes.map do |route| | ||
| 9 | + { | ||
| 10 | + method: route.route_method, | ||
| 11 | + path: route.route_path.gsub(':version', route.route_version).split('(').first | ||
| 12 | + } | ||
| 13 | + end | ||
| 14 | + end | ||
| 15 | +end.flatten.compact.sort{|a,b| a[:path]=='/api/v1/login' ? -1:1}.to_json %>; | ||
| 16 | +</script> | ||
| 17 | + | ||
| 18 | +<form id="api-form"> | ||
| 19 | + <label>Endpoint: <select name="endpoint" onchange="playground.selEndpoint()"></select></label> | ||
| 20 | + <label>Token: <input id="api-token" size="32" style="font-family:monospace"></label> | ||
| 21 | +</form> | ||
| 22 | +<button onclick="playground.addFormParam()"><%=_('Add parameter')%></button> | ||
| 23 | +<button onclick="playground.run()" style="font-weight:bold"> <%=_('Run')%> </button> | ||
| 24 | +<pre id="api-response"></pre> | ||
| 25 | + | ||
| 26 | +<script src="/javascripts/api-playground.js"></script> |
| @@ -0,0 +1,87 @@ | @@ -0,0 +1,87 @@ | ||
| 1 | +// Create endpoint options | ||
| 2 | +for (var endpoint,i=0; endpoint=endpoints[i]; i++) { | ||
| 3 | + jQuery('<option>'+endpoint.method+' '+endpoint.path+'</option>').appendTo('#api-form select'); | ||
| 4 | +} | ||
| 5 | + | ||
| 6 | +var playground = { | ||
| 7 | + | ||
| 8 | + getToken: function() { | ||
| 9 | + return jQuery('#api-token').val(); | ||
| 10 | + }, | ||
| 11 | + | ||
| 12 | + setToken: function(token) { | ||
| 13 | + jQuery('#api-token').val(token); | ||
| 14 | + }, | ||
| 15 | + | ||
| 16 | + getEndpoint: function() { | ||
| 17 | + var endpoint = jQuery('#api-form select').val().split(' '); | ||
| 18 | + return { | ||
| 19 | + method: endpoint[0], | ||
| 20 | + path: endpoint[1] | ||
| 21 | + }; | ||
| 22 | + }, | ||
| 23 | + | ||
| 24 | + selEndpoint: function() { | ||
| 25 | + var endpoint = this.getEndpoint(); | ||
| 26 | + jQuery('#api-form .api-param').remove(); | ||
| 27 | + if ( endpoint.path == '/api/v1/login' ) { | ||
| 28 | + this.addFormParam('login'); | ||
| 29 | + this.addFormParam('password'); | ||
| 30 | + } | ||
| 31 | + var pathParameters = endpoint.path.match(/:[^/]+/g); | ||
| 32 | + if ( pathParameters ) { | ||
| 33 | + for (var pathParam,i=0; pathParam=pathParameters[i]; i++) { | ||
| 34 | + this.addFormParam(pathParam.substr(1)); | ||
| 35 | + } | ||
| 36 | + } | ||
| 37 | + }, | ||
| 38 | + | ||
| 39 | + addFormParam: function(name) { | ||
| 40 | + if (!name) name = ''; | ||
| 41 | + jQuery('<div class="api-param">'+ | ||
| 42 | + '<label>name: <input name="name[]" value="'+name+'"></label> '+ | ||
| 43 | + '<label>value: <input name="value[]"></label>'+ | ||
| 44 | + '</div>').appendTo('#api-form'); | ||
| 45 | + }, | ||
| 46 | + | ||
| 47 | + run: function() { | ||
| 48 | + var endpoint = this.getEndpoint(); | ||
| 49 | + var rawData = jQuery('#api-form').serializeArray(); | ||
| 50 | + var data = {}; | ||
| 51 | + for (var i=1; i<rawData.length; i+=2) { | ||
| 52 | + data[ rawData[i].value ] = rawData[i+1].value; | ||
| 53 | + } | ||
| 54 | + if ( endpoint.path != '/api/v1/login' ) { | ||
| 55 | + data.private_token = this.getToken(); | ||
| 56 | + } | ||
| 57 | + jQuery('#api-response').empty(); | ||
| 58 | + var url = endpoint.path; | ||
| 59 | + var pathParameters = endpoint.path.match(/:[^/]+/g); | ||
| 60 | + if ( pathParameters ) { | ||
| 61 | + for (var pathParam,i=0; pathParam=pathParameters[i]; i++) { | ||
| 62 | + url = url.replace(pathParam, data[pathParam.substr(1)]); | ||
| 63 | + } | ||
| 64 | + } | ||
| 65 | + console.log('API Request', url, data); | ||
| 66 | + jQuery.ajax(url, { | ||
| 67 | + dataType: 'json', | ||
| 68 | + method: endpoint.method, | ||
| 69 | + data: data, | ||
| 70 | + success: function(data, textStatus, jqXHR) { | ||
| 71 | + jQuery('#api-response').text( JSON.stringify(data, null, ' ') ); | ||
| 72 | + if ( endpoint.path == '/api/v1/login' ) { | ||
| 73 | + playground.setToken(data.private_token); | ||
| 74 | + } | ||
| 75 | + }, | ||
| 76 | + error: function(jqXHR, textStatus, errorThrown) { | ||
| 77 | + jQuery('#api-response').html( | ||
| 78 | + '<h2>'+textStatus+'</h2>' + | ||
| 79 | + 'Request to '+url+' fail.\n\n' + errorThrown | ||
| 80 | + ); | ||
| 81 | + } | ||
| 82 | + }); | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | +}; | ||
| 86 | + | ||
| 87 | +playground.selEndpoint(jQuery('#api-form select')[0]); |