using different basePath variants to get API Listing
This commit is contained in:
@@ -1,19 +1,17 @@
|
|||||||
function SwaggerService(baseUrl, _apiKey, statusCallback) {
|
function SwaggerService(discoveryUrl, _apiKey, statusCallback) {
|
||||||
if (!baseUrl)
|
if (!discoveryUrl)
|
||||||
throw new Error("baseUrl must be passed while creating SwaggerService");
|
throw new Error("discoveryUrl must be passed while creating SwaggerService");
|
||||||
|
|
||||||
// constants
|
// constants
|
||||||
baseUrl = jQuery.trim(baseUrl);
|
discoveryUrl = jQuery.trim(discoveryUrl);
|
||||||
if (baseUrl.length == 0)
|
if (discoveryUrl.length == 0)
|
||||||
throw new Error("baseUrl must be passed while creating SwaggerService");
|
throw new Error("discoveryUrl must be passed while creating SwaggerService");
|
||||||
|
|
||||||
if (! (baseUrl.toLowerCase().indexOf("http:") == 0 || baseUrl.toLowerCase().indexOf("https:") == 0)) {
|
if (! (discoveryUrl.toLowerCase().indexOf("http:") == 0 || discoveryUrl.toLowerCase().indexOf("https:") == 0)) {
|
||||||
baseUrl = ("http://" + baseUrl);
|
discoveryUrl = ("http://" + discoveryUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
var apiHost = baseUrl.substr(0, baseUrl.lastIndexOf("/"));
|
var globalBasePath = null;
|
||||||
var discoParts = baseUrl.split("/");
|
|
||||||
var rootResourcesApiName = discoParts[discoParts.length-1];
|
|
||||||
var formatString = ".{format}";
|
var formatString = ".{format}";
|
||||||
var statusListener = statusCallback;
|
var statusListener = statusCallback;
|
||||||
var apiKey = _apiKey;
|
var apiKey = _apiKey;
|
||||||
@@ -37,13 +35,17 @@ function SwaggerService(baseUrl, _apiKey, statusCallback) {
|
|||||||
statusListener(status);
|
statusListener(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function endsWith(str, suffix) {
|
||||||
|
return str.indexOf(suffix, str.length - suffix.length) !== -1;
|
||||||
|
}
|
||||||
|
|
||||||
// make some models public
|
// make some models public
|
||||||
this.ApiResource = function() {
|
this.ApiResource = function() {
|
||||||
return ApiResource;
|
return ApiResource;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.apiHost = function() {
|
this.apiHost = function() {
|
||||||
return apiHost;
|
return globalBasePath;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.formatString = function() {
|
this.formatString = function() {
|
||||||
@@ -60,7 +62,7 @@ function SwaggerService(baseUrl, _apiKey, statusCallback) {
|
|||||||
if (atts) this.load(atts);
|
if (atts) this.load(atts);
|
||||||
this.path_json = this.path.replace("{format}", "json");
|
this.path_json = this.path.replace("{format}", "json");
|
||||||
this.path_xml = this.path.replace("{format}", "xml");
|
this.path_xml = this.path.replace("{format}", "xml");
|
||||||
this.baseUrl = apiHost;
|
this.baseUrl = globalBasePath;
|
||||||
//execluded 9 letters to remove .{format} from name
|
//execluded 9 letters to remove .{format} from name
|
||||||
this.name = this.path.split("/");
|
this.name = this.path.split("/");
|
||||||
this.name = this.name[this.name.length - 1];
|
this.name = this.name[this.name.length - 1];
|
||||||
@@ -90,7 +92,7 @@ function SwaggerService(baseUrl, _apiKey, statusCallback) {
|
|||||||
init: function(atts) {
|
init: function(atts) {
|
||||||
if (atts) this.load(atts);
|
if (atts) this.load(atts);
|
||||||
|
|
||||||
this.baseUrl = apiHost;
|
this.baseUrl = globalBasePath;
|
||||||
|
|
||||||
var secondPathSeperatorIndex = this.path.indexOf("/", 1);
|
var secondPathSeperatorIndex = this.path.indexOf("/", 1);
|
||||||
if (secondPathSeperatorIndex > 0) {
|
if (secondPathSeperatorIndex > 0) {
|
||||||
@@ -157,7 +159,7 @@ function SwaggerService(baseUrl, _apiKey, statusCallback) {
|
|||||||
init: function(atts) {
|
init: function(atts) {
|
||||||
if (atts) this.load(atts);
|
if (atts) this.load(atts);
|
||||||
|
|
||||||
this.baseUrl = apiHost;
|
this.baseUrl = globalBasePath;
|
||||||
this.httpMethodLowercase = this.httpMethod.toLowerCase();
|
this.httpMethodLowercase = this.httpMethod.toLowerCase();
|
||||||
|
|
||||||
var value = this.parameters;
|
var value = this.parameters;
|
||||||
@@ -355,6 +357,8 @@ function SwaggerService(baseUrl, _apiKey, statusCallback) {
|
|||||||
var ModelController = Spine.Controller.create({
|
var ModelController = Spine.Controller.create({
|
||||||
countLoaded: 0,
|
countLoaded: 0,
|
||||||
proxied: ["fetchResources", "loadResources", "apisLoaded", "modelsLoaded"],
|
proxied: ["fetchResources", "loadResources", "apisLoaded", "modelsLoaded"],
|
||||||
|
discoveryUrlList: [],
|
||||||
|
discoveryUrlListCursor: 0,
|
||||||
|
|
||||||
init: function() {
|
init: function() {
|
||||||
// log("ModelController.init");
|
// log("ModelController.init");
|
||||||
@@ -363,26 +367,57 @@ function SwaggerService(baseUrl, _apiKey, statusCallback) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
fetchEndpoints: function() {
|
fetchEndpoints: function() {
|
||||||
var controller = this;
|
|
||||||
|
|
||||||
updateStatus("Fetching API List...");
|
updateStatus("Fetching API List...");
|
||||||
var url = apiHost + "/" + rootResourcesApiName + apiKeySuffix;
|
var baseDiscoveryUrl = endsWith(discoveryUrl, "/") ? discoveryUrl.substr(0, discoveryUrl.length - 1) : discoveryUrl;
|
||||||
$.getJSON(url, function(response) {
|
if(endsWith(baseDiscoveryUrl, "/resources.json"))
|
||||||
|
baseDiscoveryUrl = baseDiscoveryUrl.substr(0, baseDiscoveryUrl.length - "/resources.json".length);
|
||||||
// if (response.apis) {
|
else if(endsWith(baseDiscoveryUrl, "/resources"))
|
||||||
ApiResource.createAll(response.apis);
|
baseDiscoveryUrl = baseDiscoveryUrl.substr(0, baseDiscoveryUrl.length - "/resources".length);
|
||||||
// }
|
|
||||||
|
this.discoveryUrlList.push(discoveryUrl);
|
||||||
// get rid of the root resource list api since we're not going to document that
|
this.discoveryUrlList.push(baseDiscoveryUrl);
|
||||||
var obj = ApiResource.findByAttribute("name", rootResourcesApiName);
|
this.discoveryUrlList.push(baseDiscoveryUrl + "/resources.json");
|
||||||
if (obj)
|
this.discoveryUrlList.push(baseDiscoveryUrl + "/resources");
|
||||||
obj.destroy();
|
|
||||||
controller.fetchResources();
|
log("Will try the following urls to discover api endpoints:")
|
||||||
});
|
for(var i = 0; i < this.discoveryUrlList.length; i++)
|
||||||
|
log(" > " + this.discoveryUrlList[i]);
|
||||||
|
|
||||||
|
this.fetchEndpointsSeq();
|
||||||
},
|
},
|
||||||
|
|
||||||
fetchResources: function() {
|
fetchEndpointsSeq: function() {
|
||||||
//log("fetchResources");
|
var controller = this;
|
||||||
|
|
||||||
|
if(this.discoveryUrlListCursor < this.discoveryUrlList.length) {
|
||||||
|
var url = this.discoveryUrlList[this.discoveryUrlListCursor++]
|
||||||
|
updateStatus("Fetching API List from " + url);
|
||||||
|
log("Trying url " + url);
|
||||||
|
$.getJSON(url + apiKeySuffix, function(response) {
|
||||||
|
})
|
||||||
|
.success(function(response) {
|
||||||
|
globalBasePath = url.substr(0, url.lastIndexOf("/"));
|
||||||
|
log("Setting globalBasePath to " + globalBasePath);
|
||||||
|
ApiResource.createAll(response.apis);
|
||||||
|
controller.fetchResources(response.basePath);
|
||||||
|
})
|
||||||
|
.error(function(response) {
|
||||||
|
controller.fetchEndpointsSeq();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
log ('Error with resource discovery. Exhaused all possible endpoint urls');
|
||||||
|
|
||||||
|
var urlsTried = "";
|
||||||
|
for(var i = 0; i < this.discoveryUrlList.length; i++) {
|
||||||
|
urlsTried = urlsTried + "<br/>" + this.discoveryUrlList[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
updateStatus("Unable to fetch API Listing. Tried the following urls:" + urlsTried);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
fetchResources: function(basePath) {
|
||||||
|
log("fetchResources: basePath = " + basePath);
|
||||||
//ApiResource.logAll();
|
//ApiResource.logAll();
|
||||||
for (var i = 0; i < ApiResource.all().length; i++) {
|
for (var i = 0; i < ApiResource.all().length; i++) {
|
||||||
var apiResource = ApiResource.all()[i];
|
var apiResource = ApiResource.all()[i];
|
||||||
@@ -393,7 +428,7 @@ function SwaggerService(baseUrl, _apiKey, statusCallback) {
|
|||||||
fetchResource: function(apiResource) {
|
fetchResource: function(apiResource) {
|
||||||
var controller = this;
|
var controller = this;
|
||||||
updateStatus("Fetching " + apiResource.name + "...");
|
updateStatus("Fetching " + apiResource.name + "...");
|
||||||
var resourceUrl = apiHost + apiResource.path_json + apiKeySuffix;
|
var resourceUrl = globalBasePath + apiResource.path_json + apiKeySuffix;
|
||||||
// log("resourceUrl: %o", resourceUrl);
|
// log("resourceUrl: %o", resourceUrl);
|
||||||
$.getJSON(resourceUrl,
|
$.getJSON(resourceUrl,
|
||||||
function(response) {
|
function(response) {
|
||||||
|
|||||||
Reference in New Issue
Block a user