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,21 +16,24 @@ ...@@ -16,21 +16,24 @@
<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({
...@@ -47,17 +50,30 @@ ...@@ -47,17 +50,30 @@
onFailure: function(data) { onFailure: function(data) {
log("Unable to Load SwaggerUI"); log("Unable to Load SwaggerUI");
}, },
docExpansion: "none" docExpansion: "none",
sorter : "alpha"
}); });
$('#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"));
} }
}) }
$('#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(); window.swaggerUi.load();
}); });
</script> </script>
...@@ -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,9 +37,6 @@ function handleLogin() { ...@@ -18,9 +37,6 @@ function handleLogin() {
appName = window.swaggerUi.api.info.title; appName = window.swaggerUi.api.info.title;
} }
if(popupDialog.length > 0)
popupDialog = popupDialog.last();
else {
popupDialog = $( popupDialog = $(
[ [
'<div class="api-popup-dialog">', '<div class="api-popup-dialog">',
...@@ -49,7 +65,6 @@ function handleLogin() { ...@@ -49,7 +65,6 @@ function handleLogin() {
popup.append(str); popup.append(str);
} }
var $win = $(window), var $win = $(window),
dw = $win.width(), dw = $win.width(),
dh = $win.height(), dh = $win.height(),
...@@ -67,53 +82,58 @@ function handleLogin() { ...@@ -67,53 +82,58 @@ function handleLogin() {
popupDialog.find('button.api-popup-cancel').click(function() { popupDialog.find('button.api-popup-cancel').click(function() {
popupMask.hide(); popupMask.hide();
popupDialog.hide(); popupDialog.hide();
popupDialog.empty();
popupDialog = [];
}); });
popupDialog.find('button.api-popup-authbtn').click(function() { popupDialog.find('button.api-popup-authbtn').click(function() {
popupMask.hide(); popupMask.hide();
popupDialog.hide(); popupDialog.hide();
var authSchemes = window.swaggerUi.api.authSchemes; var authSchemes = window.swaggerUi.api.authSchemes;
var location = window.location; var host = window.location;
var locationUrl = location.protocol + '//' + location.host + location.pathname; var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/"));
var redirectUrl = locationUrl.replace("index.html","").concat("/o2c.html").replace("//o2c.html","/o2c.html"); var redirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
var url = null; var url = null;
var p = window.swaggerUi.api.authSchemes; for (var key in authSchemes) {
for (var key in p) { if (authSchemes.hasOwnProperty(key)) {
if (p.hasOwnProperty(key)) { if(authSchemes[key].type === 'oauth2' && authSchemes[key].flow === 'implicit') {
var o = p[key].grantTypes; 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++) { for(k =0; k < o.length; k++) {
scopes.push($(o[k]).attr("scope")); scopes.push($(o[k]).attr('scope'));
if(k > 0){
scopeForUrl+=' ';
}
scopeForUrl+=$(o[k]).attr("scope");
} }
window.enabledScopes=scopes; window.enabledScopes=scopes;
url += '&redirect_uri=' + encodeURIComponent(redirectUrl); url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
url += '&realm=' + encodeURIComponent(realm); url += '&realm=' + encodeURIComponent(realm);
url += '&client_id=' + encodeURIComponent(clientId); url += '&client_id=' + encodeURIComponent(clientId);
url += '&scope=' + encodeURIComponent(scopeForUrl); 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!