This commit is contained in:
Константин Калинин
2015-02-13 16:06:27 +03:00
12 changed files with 364 additions and 314 deletions

View File

@@ -1,6 +1,6 @@
/**
* swagger-client - swagger.js is a javascript client for use with swaggering APIs.
* @version v2.1.2-M1
* @version v2.1.3-M1
* @link http://swagger.io
* @license apache 2.0
*/
@@ -82,10 +82,10 @@ SwaggerAuthorizations.prototype.remove = function(name) {
SwaggerAuthorizations.prototype.apply = function (obj, authorizations) {
var status = null;
var key, value, result;
var key, name, value, result;
// if the "authorizations" key is undefined, or has an empty array, add all keys
if (typeof authorizations === 'undefined' || Object.keys(authorizations).length == 0) {
if (typeof authorizations === 'undefined' || Object.keys(authorizations).length === 0) {
for (key in this.authz) {
value = this.authz[key];
result = value.apply(obj, authorizations);
@@ -280,6 +280,10 @@ PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) {
}
return returnVal;
};
var addModel = function(name, model) {
models[name] = model;
};
var SwaggerClient = function(url, options) {
this.isBuilt = false;
this.url = null;
@@ -291,6 +295,7 @@ var SwaggerClient = function(url, options) {
this.isValid = false;
this.info = null;
this.useJQuery = false;
this.resourceCount = 0;
if(typeof url !== 'undefined')
return this.initialize(url, options);
@@ -386,7 +391,6 @@ SwaggerClient.prototype.build = function(mock) {
return obj;
new SwaggerHttp().execute(obj);
}
return this;
};
@@ -502,8 +506,11 @@ SwaggerClient.prototype.buildFromSpec = function(response) {
}
}
this.isBuilt = true;
if (this.success)
if (this.success) {
this.isValid = true;
this.isBuilt = true;
this.success();
}
return this;
};
@@ -592,18 +599,18 @@ var Operation = function(parent, scheme, operationId, httpMethod, path, args, de
}
}
var i, model;
if(definitions) {
// add to global models
var key;
for(key in this.definitions) {
var model = new Model(key, definitions[key]);
model = new Model(key, definitions[key]);
if(model) {
models[key] = model;
}
}
}
var i;
for(i = 0; i < this.parameters.length; i++) {
var param = this.parameters[i];
if(param.type === 'array') {
@@ -641,7 +648,7 @@ var Operation = function(parent, scheme, operationId, httpMethod, path, args, de
param.responseClassSignature = param.signature;
}
var defaultResponseCode, response, model, responses = this.responses;
var defaultResponseCode, response, responses = this.responses;
if(responses['200']) {
response = responses['200'];
@@ -995,9 +1002,9 @@ Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
var allHeaders = this.getHeaderParams(args);
var contentTypeHeaders = this.setContentTypes(args, opts);
var headers = {};
for (var attrname in allHeaders) { headers[attrname] = allHeaders[attrname]; }
for (var attrname in contentTypeHeaders) { headers[attrname] = contentTypeHeaders[attrname]; }
var headers = {}, attrname;
for (attrname in allHeaders) { headers[attrname] = allHeaders[attrname]; }
for (attrname in contentTypeHeaders) { headers[attrname] = contentTypeHeaders[attrname]; }
var body = this.getBody(headers, args);
var url = this.urlify(args);
@@ -1439,7 +1446,7 @@ Property.prototype.toString = function() {
type = '';
}
else {
this.schema.type;
type = this.schema.type;
}
if (this.default)
@@ -1500,7 +1507,7 @@ Property.prototype.toString = function() {
optionHtml = function(label, value) {
return '<tr><td class="optionName">' + label + ':</td><td>' + value + '</td></tr>';
}
};
typeFromJsonSchema = function(type, format) {
var str;
@@ -1533,7 +1540,7 @@ var cookies = {};
var models = {};
SwaggerClient.prototype.buildFrom1_2Spec = function (response) {
if (response.apiVersion != null) {
if (response.apiVersion !== null) {
this.apiVersion = response.apiVersion;
}
this.apis = {};
@@ -1569,6 +1576,7 @@ SwaggerClient.prototype.buildFrom1_2Spec = function (response) {
this.apisArray.push(res);
} else {
var k;
this.expectedResourceCount = response.apis.length;
for (k = 0; k < response.apis.length; k++) {
var resource = response.apis[k];
res = new SwaggerResource(resource, this);
@@ -1577,15 +1585,22 @@ SwaggerClient.prototype.buildFrom1_2Spec = function (response) {
}
}
this.isValid = true;
return this;
};
SwaggerClient.prototype.finish = function() {
if (typeof this.success === 'function') {
this.isValid = true;
this.isBuilt = true;
this.selfReflect();
this.success();
} return this;
}
};
SwaggerClient.prototype.buildFrom1_1Spec = function (response) {
log('This API is using a deprecated version of Swagger! Please see http://github.com/wordnik/swagger-core/wiki for more info');
if (response.apiVersion != null)
if (response.apiVersion !== null)
this.apiVersion = response.apiVersion;
this.apis = {};
this.apisArray = [];
@@ -1631,7 +1646,7 @@ SwaggerClient.prototype.buildFrom1_1Spec = function (response) {
SwaggerClient.prototype.convertInfo = function (resp) {
if(typeof resp == 'object') {
var info = {}
var info = {};
info.title = resp.title;
info.description = resp.description;
@@ -1660,9 +1675,6 @@ SwaggerClient.prototype.selfReflect = function () {
}
this.setConsolidatedModels();
this.ready = true;
if (typeof this.success === 'function') {
return this.success();
}
};
SwaggerClient.prototype.setConsolidatedModels = function () {
@@ -1728,9 +1740,11 @@ var SwaggerResource = function (resourceObj, api) {
on: {
response: function (resp) {
var responseObj = resp.obj || JSON.parse(resp.data);
_this.api.resourceCount += 1;
return _this.addApiDeclaration(responseObj);
},
error: function (response) {
_this.api.resourceCount += 1;
return _this.api.fail('<span data-swTarnslate="1">Unable to read api</span> \'' +
_this.name + '\' <span data-swTarnslate="1">from path</span> ' + _this.url + ' (<span data-swTarnslate="1">server returned</span> ' + response.statusText + ')');
}
@@ -1784,7 +1798,9 @@ SwaggerResource.prototype.addApiDeclaration = function (response) {
}
this.api[this.name] = this;
this.ready = true;
return this.api.selfReflect();
if(this.api.resourceCount === this.api.expectedResourceCount)
this.api.finish();
return this;
};
SwaggerResource.prototype.addModels = function (models) {
@@ -2077,7 +2093,7 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no
this.path = this.path.replace('{format}', 'json');
this.method = this.method.toLowerCase();
this.isGetMethod = this.method === 'GET';
this.isGetMethod = this.method === 'get';
var i, j, v;
this.resourceName = this.resource.name;
@@ -2128,17 +2144,17 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no
}
}
}
else if (param.allowableValues != null) {
else if (param.allowableValues) {
if (param.allowableValues.valueType === 'RANGE')
param.isRange = true;
else
param.isList = true;
if (param.allowableValues != null) {
if (param.allowableValues) {
param.allowableValues.descriptiveValues = [];
if (param.allowableValues.values) {
for (j = 0; j < param.allowableValues.values.length; j++) {
v = param.allowableValues.values[j];
if (param.defaultValue != null) {
if (param.defaultValue !== null) {
param.allowableValues.descriptiveValues.push({
value: String(v),
isDefault: (v === param.defaultValue)
@@ -2208,7 +2224,7 @@ SwaggerOperation.prototype.getSampleJSON = function (type, models) {
var isPrimitive, listType, val;
listType = this.isListType(type);
isPrimitive = ((typeof listType !== 'undefined') && models[listType]) || (typeof models[type] !== 'undefined') ? false : true;
val = isPrimitive ? void 0 : (listType != null ? models[listType].createJSONSample() : models[type].createJSONSample());
val = isPrimitive ? void 0 : (listType ? models[listType].createJSONSample() : models[type].createJSONSample());
if (val) {
val = listType ? [val] : val;
if (typeof val == 'string')
@@ -2243,7 +2259,7 @@ SwaggerOperation.prototype['do'] = function (args, opts, callback, error) {
callback = function (response) {
var content;
content = null;
if (response != null) {
if (response !== null) {
content = response.data;
} else {
content = 'no data';
@@ -2254,7 +2270,7 @@ SwaggerOperation.prototype['do'] = function (args, opts, callback, error) {
params = {};
params.headers = [];
if (args.headers != null) {
if (args.headers) {
params.headers = args.headers;
delete args.headers;
}
@@ -2375,7 +2391,7 @@ SwaggerOperation.prototype.urlify = function (args) {
}
}
}
if ((queryParams != null) && queryParams.length > 0)
if ((queryParams) && queryParams.length > 0)
url += '?' + queryParams;
return url;
};
@@ -2584,7 +2600,7 @@ var SwaggerRequest = function (type, url, params, opts, successCallback, errorCa
}
var obj;
if (!((this.headers != null) && (this.headers.mock != null))) {
if (!((this.headers) && (this.headers.mock))) {
obj = {
url: this.url,
method: this.type,
@@ -2807,7 +2823,7 @@ JQueryHttpClient.prototype.execute = function(obj) {
if(contentType) {
if(contentType.indexOf("application/json") === 0 || contentType.indexOf("+json") > 0) {
try {
out.obj = response.responseJSON || {};
out.obj = response.responseJSON || JSON.parse(out.data) || {};
} catch (ex) {
// do not set out.obj
log("unable to parse JSON content");
@@ -2953,7 +2969,9 @@ e.ApiKeyAuthorization = ApiKeyAuthorization;
e.PasswordAuthorization = PasswordAuthorization;
e.CookieAuthorization = CookieAuthorization;
e.SwaggerClient = SwaggerClient;
e.SwaggerApi = SwaggerClient;
e.Operation = Operation;
e.Model = Model;
e.models = models;
e.addModel = addModel;
})();