Commit 98c1144c by Juho Juopperi

new swagger and url logic

1 parent e73a21db
...@@ -743,17 +743,20 @@ ...@@ -743,17 +743,20 @@
display: inline-block; display: inline-block;
font-size: 0.9em; font-size: 0.9em;
} }
.swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.content div.sandbox_header img {
display: block;
clear: none;
float: right;
}
.swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.content div.sandbox_header input.submit { .swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.content div.sandbox_header input.submit {
display: block; display: block;
clear: none; clear: none;
float: left; float: left;
padding: 6px 8px; padding: 6px 8px;
} }
.swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.content div.sandbox_header span.response_throbber {
background-image: url('../images/throbber.gif');
width: 128px;
height: 16px;
display: block;
clear: none;
float: right;
}
.swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.content form input[type='text'].error { .swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.content form input[type='text'].error {
outline: 2px solid black; outline: 2px solid black;
outline-color: #cc0000; outline-color: #cc0000;
......
...@@ -16,49 +16,65 @@ ...@@ -16,49 +16,65 @@
<script src='lib/underscore-min.js' type='text/javascript'></script> <script src='lib/underscore-min.js' type='text/javascript'></script>
<script src='lib/backbone-min.js' type='text/javascript'></script> <script src='lib/backbone-min.js' type='text/javascript'></script>
<script src='lib/swagger.js' type='text/javascript'></script> <script src='lib/swagger.js' type='text/javascript'></script>
<script src='lib/swagger-client.js' type='text/javascript'></script>
<script src='swagger-ui.js' type='text/javascript'></script> <script src='swagger-ui.js' type='text/javascript'></script>
<script src='lib/highlight.7.3.pack.js' type='text/javascript'></script> <script src='lib/highlight.7.3.pack.js' type='text/javascript'></script>
<!-- enabling this will enable oauth2 implicit scope support --> <!-- enabling this will enable oauth2 implicit scope support -->
<script src='lib/swagger-oauth.js' type='text/javascript'></script> <script src='lib/swagger-oauth.js' type='text/javascript'></script>
<script type="text/javascript"> <script type="text/javascript">
$(function () { $(function () {
var url = window.location.protocol + '//' + window.location.host + '/MoyaWeb/rest/api-docs';
if (!url || url.length < 1) {
url = "http://localhost:8080/MoyaWeb/rest/api-docs";
}
window.swaggerUi = new SwaggerUi({ window.swaggerUi = new SwaggerUi({
url: "http://localhost:8080/MoyaWeb/rest/api-docs", url: url,
dom_id: "swagger-ui-container", dom_id: "swagger-ui-container",
supportedSubmitMethods: ['get', 'post', 'put', 'delete'], supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
onComplete: function(swaggerApi, swaggerUi){ onComplete: function(swaggerApi, swaggerUi){
log("Loaded SwaggerUI"); log("Loaded SwaggerUI");
if(typeof initOAuth == "function") {
if(typeof initOAuth == "function") { /*
/* initOAuth({
initOAuth({ clientId: "your-client-id",
clientId: "your-client-id", realm: "your-realms",
realm: "your-realms", appName: "your-app-name"
appName: "your-app-name" });
*/
}
$('pre code').each(function(i, e) {
hljs.highlightBlock(e)
}); });
*/ },
} onFailure: function(data) {
$('pre code').each(function(i, e) { log("Unable to Load SwaggerUI");
hljs.highlightBlock(e) },
}); docExpansion: "none",
}, sorter : "alpha"
onFailure: function(data) { });
log("Unable to Load SwaggerUI");
},
docExpansion: "none"
});
$('#input_apiKey').change(function() { function addApiKeyAuthorization() {
var key = $('#input_apiKey')[0].value; var key = $('#input_apiKey')[0].value;
log("key: " + key); log("key: " + key);
if(key && key.trim() != "") { if(key && key.trim() != "") {
log("added key " + key); log("added key " + key);
window.authorizations.add("key", new ApiKeyAuthorization("api_key", key, "query")); window.authorizations.add("api_key", new ApiKeyAuthorization("api_key", key, "query"));
}
} }
})
window.swaggerUi.load(); $('#input_apiKey').change(function() {
addApiKeyAuthorization();
});
// if you have an apiKey you would like to pre-populate on the page for demonstration purposes...
/*
var apiKey = "myApiKeyXXXX123456789";
$('#input_apiKey').val(apiKey);
addApiKeyAuthorization();
*/
window.swaggerUi.load();
}); });
</script> </script>
</head> </head>
...@@ -66,14 +82,8 @@ ...@@ -66,14 +82,8 @@
<body class="swagger-section"> <body class="swagger-section">
<div id='header'> <div id='header'>
<div class="swagger-ui-wrap"> <div class="swagger-ui-wrap">
<a id="logo" href="http://swagger.wordnik.com">swagger</a> <a id="logo" href="http://swagger.io">swagger</a>
<form id='api_selector'> <form id='api_selector'>
<div class='input icon-btn'>
<img id="show-pet-store-icon" src="images/pet_store_api.png" title="Show Swagger Petstore Example Apis">
</div>
<div class='input icon-btn'>
<img id="show-wordnik-dev-icon" src="images/wordnik_api.png" title="Show Wordnik Developer Apis">
</div>
<div class='input'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text"/></div> <div class='input'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text"/></div>
<div class='input'><input placeholder="api_key" id="input_apiKey" name="apiKey" type="text"/></div> <div class='input'><input placeholder="api_key" id="input_apiKey" name="apiKey" type="text"/></div>
<div class='input'><a id="explore" href="#">Explore</a></div> <div class='input'><a id="explore" href="#">Explore</a></div>
......
...@@ -2131,10 +2131,10 @@ require.define("/shred/mixins/headers.js", function (require, module, exports, _ ...@@ -2131,10 +2131,10 @@ require.define("/shred/mixins/headers.js", function (require, module, exports, _
// to `Content-Type`. // to `Content-Type`.
var corsetCase = function(string) { var corsetCase = function(string) {
return string.toLowerCase() return string;//.toLowerCase()
//.replace("_","-") //.replace("_","-")
.replace(/(^|-)(\w)/g, // .replace(/(^|-)(\w)/g,
function(s) { return s.toUpperCase(); }); // function(s) { return s.toUpperCase(); });
}; };
// We suspect that `initializeHeaders` was once more complicated ... // We suspect that `initializeHeaders` was once more complicated ...
......
...@@ -7,10 +7,29 @@ var realm; ...@@ -7,10 +7,29 @@ var realm;
function handleLogin() { function handleLogin() {
var scopes = []; var scopes = [];
if(window.swaggerUi.api.authSchemes var auths = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions;
&& window.swaggerUi.api.authSchemes.oauth2 if(auths) {
&& window.swaggerUi.api.authSchemes.oauth2.scopes) { var key;
scopes = window.swaggerUi.api.authSchemes.oauth2.scopes; var defs = auths;
for(key in defs) {
var auth = defs[key];
if(auth.type === 'oauth2' && auth.scopes) {
var scope;
if(Array.isArray(auth.scopes)) {
// 1.2 support
var i;
for(i = 0; i < auth.scopes.length; i++) {
scopes.push(auth.scopes[i]);
}
}
else {
// 2.0 support
for(scope in auth.scopes) {
scopes.push({scope: scope, description: auth.scopes[scope]});
}
}
}
}
} }
if(window.swaggerUi.api if(window.swaggerUi.api
...@@ -18,102 +37,103 @@ function handleLogin() { ...@@ -18,102 +37,103 @@ function handleLogin() {
appName = window.swaggerUi.api.info.title; appName = window.swaggerUi.api.info.title;
} }
if(popupDialog.length > 0) popupDialog = $(
popupDialog = popupDialog.last(); [
else { '<div class="api-popup-dialog">',
popupDialog = $( '<div class="api-popup-title">Select OAuth2.0 Scopes</div>',
[ '<div class="api-popup-content">',
'<div class="api-popup-dialog">', '<p>Scopes are used to grant an application different levels of access to data on behalf of the end user. Each API may declare one or more scopes.',
'<div class="api-popup-title">Select OAuth2.0 Scopes</div>', '<a href="#">Learn how to use</a>',
'<div class="api-popup-content">', '</p>',
'<p>Scopes are used to grant an application different levels of access to data on behalf of the end user. Each API may declare one or more scopes.', '<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>',
'<a href="#">Learn how to use</a>', '<ul class="api-popup-scopes">',
'</p>', '</ul>',
'<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>', '<p class="error-msg"></p>',
'<ul class="api-popup-scopes">', '<div class="api-popup-actions"><button class="api-popup-authbtn api-button green" type="button">Authorize</button><button class="api-popup-cancel api-button gray" type="button">Cancel</button></div>',
'</ul>', '</div>',
'<p class="error-msg"></p>', '</div>'].join(''));
'<div class="api-popup-actions"><button class="api-popup-authbtn api-button green" type="button">Authorize</button><button class="api-popup-cancel api-button gray" type="button">Cancel</button></div>', $(document.body).append(popupDialog);
'</div>',
'</div>'].join('')); popup = popupDialog.find('ul.api-popup-scopes').empty();
$(document.body).append(popupDialog); for (i = 0; i < scopes.length; i ++) {
scope = scopes[i];
popup = popupDialog.find('ul.api-popup-scopes').empty(); str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope;
for (i = 0; i < scopes.length; i ++) { if (scope.description) {
scope = scopes[i]; str += '<br/><span class="api-scope-desc">' + scope.description + '</span>';
str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope;
if (scope.description) {
str += '<br/><span class="api-scope-desc">' + scope.description + '</span>';
}
str += '</label></li>';
popup.append(str);
} }
str += '</label></li>';
popup.append(str);
var $win = $(window), }
dw = $win.width(),
dh = $win.height(), var $win = $(window),
st = $win.scrollTop(), dw = $win.width(),
dlgWd = popupDialog.outerWidth(), dh = $win.height(),
dlgHt = popupDialog.outerHeight(), st = $win.scrollTop(),
top = (dh -dlgHt)/2 + st, dlgWd = popupDialog.outerWidth(),
left = (dw - dlgWd)/2; dlgHt = popupDialog.outerHeight(),
top = (dh -dlgHt)/2 + st,
popupDialog.css({ left = (dw - dlgWd)/2;
top: (top < 0? 0 : top) + 'px',
left: (left < 0? 0 : left) + 'px' popupDialog.css({
}); top: (top < 0? 0 : top) + 'px',
left: (left < 0? 0 : left) + 'px'
popupDialog.find('button.api-popup-cancel').click(function() { });
popupMask.hide();
popupDialog.hide(); popupDialog.find('button.api-popup-cancel').click(function() {
}); popupMask.hide();
popupDialog.find('button.api-popup-authbtn').click(function() { popupDialog.hide();
popupMask.hide(); popupDialog.empty();
popupDialog.hide(); popupDialog = [];
});
var authSchemes = window.swaggerUi.api.authSchemes;
var location = window.location; popupDialog.find('button.api-popup-authbtn').click(function() {
var locationUrl = location.protocol + '//' + location.host + location.pathname; popupMask.hide();
var redirectUrl = locationUrl.replace("index.html","").concat("/o2c.html").replace("//o2c.html","/o2c.html"); popupDialog.hide();
var url = null;
var authSchemes = window.swaggerUi.api.authSchemes;
var p = window.swaggerUi.api.authSchemes; var host = window.location;
for (var key in p) { var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/"));
if (p.hasOwnProperty(key)) { var redirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
var o = p[key].grantTypes; var url = null;
for (var key in authSchemes) {
if (authSchemes.hasOwnProperty(key)) {
if(authSchemes[key].type === 'oauth2' && authSchemes[key].flow === 'implicit') {
var dets = authSchemes[key];
url = dets.authorizationUrl + '?response_type=token';
window.swaggerUi.tokenName = dets.tokenUrl || 'access_token';
}
else if(authSchemes[key].grantTypes) {
// 1.2 support
var o = authSchemes[key].grantTypes;
for(var t in o) { for(var t in o) {
if(o.hasOwnProperty(t) && t === 'implicit') { if(o.hasOwnProperty(t) && t === 'implicit') {
var dets = o[t]; var dets = o[t];
url = dets.loginEndpoint.url + "?response_type=token"; var ep = dets.loginEndpoint.url;
url = dets.loginEndpoint.url + '?response_type=token';
window.swaggerUi.tokenName = dets.tokenName; window.swaggerUi.tokenName = dets.tokenName;
} }
} }
} }
} }
var scopes = []; }
var scopeForUrl=''; var scopes = []
var o = $('.api-popup-scopes').find('input:checked'); var o = $('.api-popup-scopes').find('input:checked');
for(var k =0; k < o.length; k++) {
scopes.push($(o[k]).attr("scope"));
if(k > 0){
scopeForUrl+=' ';
}
scopeForUrl+=$(o[k]).attr("scope");
}
window.enabledScopes=scopes; for(k =0; k < o.length; k++) {
scopes.push($(o[k]).attr('scope'));
}
url += '&redirect_uri=' + encodeURIComponent(redirectUrl); window.enabledScopes=scopes;
url += '&realm=' + encodeURIComponent(realm);
url += '&client_id=' + encodeURIComponent(clientId); url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
url += '&scope=' + encodeURIComponent(scopeForUrl); url += '&realm=' + encodeURIComponent(realm);
url += '&client_id=' + encodeURIComponent(clientId);
url += '&scope=' + encodeURIComponent(scopes);
window.open(url);
});
window.open(url);
});
}
popupMask.show(); popupMask.show();
popupDialog.show(); popupDialog.show();
return; return;
...@@ -137,14 +157,14 @@ function initOAuth(opts) { ...@@ -137,14 +157,14 @@ function initOAuth(opts) {
var o = (opts||{}); var o = (opts||{});
var errors = []; var errors = [];
appName = (o.appName||errors.push("missing appName")); appName = (o.appName||errors.push('missing appName'));
popupMask = (o.popupMask||$('#api-common-mask')); popupMask = (o.popupMask||$('#api-common-mask'));
popupDialog = (o.popupDialog||$('.api-popup-dialog')); popupDialog = (o.popupDialog||$('.api-popup-dialog'));
clientId = (o.clientId||errors.push("missing client id")); clientId = (o.clientId||errors.push('missing client id'));
realm = (o.realm||errors.push("missing realm")); realm = (o.realm||errors.push('missing realm'));
if(errors.length > 0){ if(errors.length > 0){
log("auth unable initialize oauth: " + errors); log('auth unable initialize oauth: ' + errors);
return; return;
} }
...@@ -210,8 +230,7 @@ function onOAuthComplete(token) { ...@@ -210,8 +230,7 @@ function onOAuthComplete(token) {
} }
} }
}); });
window.authorizations.add('oauth2', new ApiKeyAuthorization('Authorization', 'Bearer ' + b, 'header'));
window.authorizations.add("key", new ApiKeyAuthorization("Authorization", "Bearer " + b, "header"));
} }
} }
} }
......
This diff could not be displayed because it is too large.
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!