updated client

This commit is contained in:
Tony Tam
2015-02-21 00:42:03 -08:00
3 changed files with 58 additions and 24 deletions

View File

@@ -1,6 +1,6 @@
/** /**
* swagger-client - swagger.js is a javascript client for use with swaggering APIs. * swagger-client - swagger.js is a javascript client for use with swaggering APIs.
* @version v2.1.7-M1 * @version v2.1.8-M1
* @link http://swagger.io * @link http://swagger.io
* @license apache 2.0 * @license apache 2.0
*/ */
@@ -346,8 +346,7 @@ SwaggerClient.prototype.initialize = function (url, options) {
if (options.authorizations) { if (options.authorizations) {
this.clientAuthorizations = options.authorizations; this.clientAuthorizations = options.authorizations;
} else { } else {
var e = (typeof window !== 'undefined' ? window : exports); this.clientAuthorizations = authorizations;
this.clientAuthorizations = e.authorizations;
} }
this.supportedSubmitMethods = options.supportedSubmitMethods || []; this.supportedSubmitMethods = options.supportedSubmitMethods || [];
@@ -407,8 +406,7 @@ SwaggerClient.prototype.build = function(mock) {
setTimeout(function() { self.buildFromSpec(self.spec); }, 10); setTimeout(function() { self.buildFromSpec(self.spec); }, 10);
} }
else { else {
var e = (typeof window !== 'undefined' ? window : exports); authorizations.apply(obj);
var status = e.authorizations.apply(obj);
if(mock) if(mock)
return obj; return obj;
new SwaggerHttp().execute(obj); new SwaggerHttp().execute(obj);
@@ -833,7 +831,7 @@ Operation.prototype.help = function(dontPrint) {
var out = this.nickname + ': ' + this.summary + '\n'; var out = this.nickname + ': ' + this.summary + '\n';
for(var i = 0; i < this.parameters.length; i++) { for(var i = 0; i < this.parameters.length; i++) {
var param = this.parameters[i]; var param = this.parameters[i];
var typeInfo = typeFromJsonSchema(param.type, param.format); var typeInfo = param.signature;
out += '\n * ' + param.name + ' (' + typeInfo + '): ' + param.description; out += '\n * ' + param.name + ' (' + typeInfo + '): ' + param.description;
} }
if(typeof dontPrint === 'undefined') if(typeof dontPrint === 'undefined')
@@ -951,9 +949,8 @@ Operation.prototype.getMissingParams = function(args) {
return missingParams; return missingParams;
}; };
Operation.prototype.getBody = function(headers, args) { Operation.prototype.getBody = function(headers, args, opts) {
var formParams = {}; var formParams = {}, body, key;
var body;
for(var i = 0; i < this.parameters.length; i++) { for(var i = 0; i < this.parameters.length; i++) {
var param = this.parameters[i]; var param = this.parameters[i];
@@ -969,7 +966,6 @@ Operation.prototype.getBody = function(headers, args) {
// handle form params // handle form params
if(headers['Content-Type'] === 'application/x-www-form-urlencoded') { if(headers['Content-Type'] === 'application/x-www-form-urlencoded') {
var encoded = ""; var encoded = "";
var key;
for(key in formParams) { for(key in formParams) {
value = formParams[key]; value = formParams[key];
if(typeof value !== 'undefined'){ if(typeof value !== 'undefined'){
@@ -980,6 +976,25 @@ Operation.prototype.getBody = function(headers, args) {
} }
body = encoded; body = encoded;
} }
else if (headers['Content-Type'] && headers['Content-Type'].indexOf('multipart/form-data') >= 0) {
if(opts.useJQuery) {
var bodyParam = new FormData();
bodyParam.type = 'formData';
for (key in formParams) {
value = args[key];
if (typeof value !== 'undefined') {
// required for jquery file upload
if(value.type === 'file' && value.value) {
delete headers['Content-Type'];
bodyParam.append(key, value.value);
}
else
bodyParam.append(key, value);
}
}
body = bodyParam;
}
}
return body; return body;
}; };
@@ -1042,9 +1057,8 @@ Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
success = (success||log); success = (success||log);
error = (error||log); error = (error||log);
if(typeof opts.useJQuery === 'boolean') { if(opts.useJQuery)
this.useJQuery = opts.useJQuery; this.useJQuery = opts.useJQuery;
}
var missingParams = this.getMissingParams(args); var missingParams = this.getMissingParams(args);
if(missingParams.length > 0) { if(missingParams.length > 0) {
@@ -1059,7 +1073,7 @@ Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
for (attrname in allHeaders) { headers[attrname] = allHeaders[attrname]; } for (attrname in allHeaders) { headers[attrname] = allHeaders[attrname]; }
for (attrname in contentTypeHeaders) { headers[attrname] = contentTypeHeaders[attrname]; } for (attrname in contentTypeHeaders) { headers[attrname] = contentTypeHeaders[attrname]; }
var body = this.getBody(headers, args); var body = this.getBody(headers, args, opts);
var url = this.urlify(args); var url = this.urlify(args);
var obj = { var obj = {
@@ -1077,7 +1091,7 @@ Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
} }
} }
}; };
var status = e.authorizations.apply(obj, this.operation.security); var status = authorizations.apply(obj, this.operation.security);
if(opts.mock === true) if(opts.mock === true)
return obj; return obj;
else else
@@ -1160,14 +1174,24 @@ Operation.prototype.setContentTypes = function(args, opts) {
}; };
Operation.prototype.asCurl = function (args) { Operation.prototype.asCurl = function (args) {
var obj = this.execute(args, {mock: true});
authorizations.apply(obj);
var results = []; var results = [];
var headers = this.getHeaderParams(args); results.push('-X ' + this.method.toUpperCase());
if (headers) { if (obj.headers) {
var key; var key;
for (key in headers) for (key in obj.headers)
results.push("--header \"" + key + ": " + headers[key] + "\""); results.push('--header "' + key + ': ' + obj.headers[key] + '"');
} }
return "curl " + (results.join(" ")) + " " + this.urlify(args); if(obj.body) {
var body;
if(typeof obj.body === 'object')
body = JSON.stringify(obj.body);
else
body = obj.body;
results.push('-d "' + body.replace(/"/g, '\\"') + '"');
}
return 'curl ' + (results.join(' ')) + ' "' + obj.url + '"';
}; };
Operation.prototype.encodePathCollection = function(type, name, value) { Operation.prototype.encodePathCollection = function(type, name, value) {
@@ -2812,7 +2836,14 @@ SwaggerHttp.prototype.execute = function(obj, opts) {
this.useJQuery = this.isIE8(); this.useJQuery = this.isIE8();
if(obj && typeof obj.body === 'object') { if(obj && typeof obj.body === 'object') {
if(obj.body.type && obj.body.type !== 'formData')
obj.body = JSON.stringify(obj.body); obj.body = JSON.stringify(obj.body);
else {
obj.contentType = false;
obj.processData = false;
// delete obj.cache;
delete obj.headers['Content-Type'];
}
} }
if(this.useJQuery) if(this.useJQuery)
@@ -2853,7 +2884,9 @@ JQueryHttpClient.prototype.execute = function(obj) {
obj.type = obj.method; obj.type = obj.method;
obj.cache = false; obj.cache = false;
delete obj.useJQuery;
/*
obj.beforeSend = function(xhr) { obj.beforeSend = function(xhr) {
var key, results; var key, results;
if (obj.headers) { if (obj.headers) {
@@ -2869,9 +2902,10 @@ JQueryHttpClient.prototype.execute = function(obj) {
} }
return results; return results;
} }
}; };*/
obj.data = obj.body; obj.data = obj.body;
delete obj.body;
obj.complete = function(response, textStatus, opts) { obj.complete = function(response, textStatus, opts) {
var headers = {}, var headers = {},
headerArray = response.getAllResponseHeaders().split("\n"); headerArray = response.getAllResponseHeaders().split("\n");
@@ -3045,7 +3079,7 @@ ShredHttpClient.prototype.execute = function(obj) {
var e = (typeof window !== 'undefined' ? window : exports); var e = (typeof window !== 'undefined' ? window : exports);
e.authorizations = new SwaggerAuthorizations(); e.authorizations = authorizations = new SwaggerAuthorizations();
e.ApiKeyAuthorization = ApiKeyAuthorization; e.ApiKeyAuthorization = ApiKeyAuthorization;
e.PasswordAuthorization = PasswordAuthorization; e.PasswordAuthorization = PasswordAuthorization;
e.CookieAuthorization = CookieAuthorization; e.CookieAuthorization = CookieAuthorization;

View File

@@ -18,7 +18,7 @@
"dependencies": { "dependencies": {
"shred": "0.8.10", "shred": "0.8.10",
"btoa": "1.1.1", "btoa": "1.1.1",
"swagger-client": "2.1.7-M1" "swagger-client": "2.1.8-M1"
}, },
"devDependencies": { "devDependencies": {
"chai": "^1.10.0", "chai": "^1.10.0",

View File

@@ -230,7 +230,7 @@ class OperationView extends Backbone.View
# add params # add params
for param in @model.parameters for param in @model.parameters
if param.paramType is 'form' if param.paramType is 'form' or param.in is 'formData'
if param.type.toLowerCase() isnt 'file' and map[param.name] != undefined if param.type.toLowerCase() isnt 'file' and map[param.name] != undefined
bodyParam.append(param.name, map[param.name]) bodyParam.append(param.name, map[param.name])