Browse Source

updated version, client

bubble
Tony Tam 9 years ago
parent
commit
3ecc974ee4
3 changed files with 113 additions and 45 deletions
  1. +56
    -22
      dist/lib/swagger-client.js
  2. +56
    -22
      lib/swagger-client.js
  3. +1
    -1
      package.json

+ 56
- 22
dist/lib/swagger-client.js View File

@@ -1,6 +1,6 @@
/**
* 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
* @license apache 2.0
*/
@@ -346,8 +346,7 @@ SwaggerClient.prototype.initialize = function (url, options) {
if (options.authorizations) {
this.clientAuthorizations = options.authorizations;
} else {
var e = (typeof window !== 'undefined' ? window : exports);
this.clientAuthorizations = e.authorizations;
this.clientAuthorizations = authorizations;
}

this.supportedSubmitMethods = options.supportedSubmitMethods || [];
@@ -407,8 +406,7 @@ SwaggerClient.prototype.build = function(mock) {
setTimeout(function() { self.buildFromSpec(self.spec); }, 10);
}
else {
var e = (typeof window !== 'undefined' ? window : exports);
var status = e.authorizations.apply(obj);
authorizations.apply(obj);
if(mock)
return obj;
new SwaggerHttp().execute(obj);
@@ -833,7 +831,7 @@ Operation.prototype.help = function(dontPrint) {
var out = this.nickname + ': ' + this.summary + '\n';
for(var i = 0; i < this.parameters.length; i++) {
var param = this.parameters[i];
var typeInfo = typeFromJsonSchema(param.type, param.format);
var typeInfo = param.signature;
out += '\n * ' + param.name + ' (' + typeInfo + '): ' + param.description;
}
if(typeof dontPrint === 'undefined')
@@ -951,9 +949,8 @@ Operation.prototype.getMissingParams = function(args) {
return missingParams;
};

Operation.prototype.getBody = function(headers, args) {
var formParams = {};
var body;
Operation.prototype.getBody = function(headers, args, opts) {
var formParams = {}, body, key;

for(var i = 0; i < this.parameters.length; i++) {
var param = this.parameters[i];
@@ -969,7 +966,6 @@ Operation.prototype.getBody = function(headers, args) {
// handle form params
if(headers['Content-Type'] === 'application/x-www-form-urlencoded') {
var encoded = "";
var key;
for(key in formParams) {
value = formParams[key];
if(typeof value !== 'undefined'){
@@ -980,6 +976,25 @@ Operation.prototype.getBody = function(headers, args) {
}
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;
};
@@ -1042,9 +1057,8 @@ Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
success = (success||log);
error = (error||log);

if(typeof opts.useJQuery === 'boolean') {
if(opts.useJQuery)
this.useJQuery = opts.useJQuery;
}

var missingParams = this.getMissingParams(args);
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 contentTypeHeaders) { headers[attrname] = contentTypeHeaders[attrname]; }

var body = this.getBody(headers, args);
var body = this.getBody(headers, args, opts);
var url = this.urlify(args);

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)
return obj;
else
@@ -1160,14 +1174,24 @@ Operation.prototype.setContentTypes = function(args, opts) {
};

Operation.prototype.asCurl = function (args) {
var obj = this.execute(args, {mock: true});
authorizations.apply(obj);
var results = [];
var headers = this.getHeaderParams(args);
if (headers) {
results.push('-X ' + this.method.toUpperCase());
if (obj.headers) {
var key;
for (key in headers)
results.push("--header \"" + key + ": " + headers[key] + "\"");
for (key in obj.headers)
results.push('--header "' + key + ': ' + obj.headers[key] + '"');
}
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(" ")) + " " + this.urlify(args);
return 'curl ' + (results.join(' ')) + ' "' + obj.url + '"';
};

Operation.prototype.encodePathCollection = function(type, name, value) {
@@ -2812,7 +2836,14 @@ SwaggerHttp.prototype.execute = function(obj, opts) {
this.useJQuery = this.isIE8();

if(obj && typeof obj.body === 'object') {
obj.body = JSON.stringify(obj.body);
if(obj.body.type && obj.body.type !== 'formData')
obj.body = JSON.stringify(obj.body);
else {
obj.contentType = false;
obj.processData = false;
// delete obj.cache;
delete obj.headers['Content-Type'];
}
}

if(this.useJQuery)
@@ -2853,7 +2884,9 @@ JQueryHttpClient.prototype.execute = function(obj) {

obj.type = obj.method;
obj.cache = false;
delete obj.useJQuery;

/*
obj.beforeSend = function(xhr) {
var key, results;
if (obj.headers) {
@@ -2869,9 +2902,10 @@ JQueryHttpClient.prototype.execute = function(obj) {
}
return results;
}
};
};*/

obj.data = obj.body;
delete obj.body;
obj.complete = function(response, textStatus, opts) {
var headers = {},
headerArray = response.getAllResponseHeaders().split("\n");
@@ -3045,7 +3079,7 @@ ShredHttpClient.prototype.execute = function(obj) {

var e = (typeof window !== 'undefined' ? window : exports);

e.authorizations = new SwaggerAuthorizations();
e.authorizations = authorizations = new SwaggerAuthorizations();
e.ApiKeyAuthorization = ApiKeyAuthorization;
e.PasswordAuthorization = PasswordAuthorization;
e.CookieAuthorization = CookieAuthorization;


+ 56
- 22
lib/swagger-client.js View File

@@ -1,6 +1,6 @@
/**
* 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
* @license apache 2.0
*/
@@ -346,8 +346,7 @@ SwaggerClient.prototype.initialize = function (url, options) {
if (options.authorizations) {
this.clientAuthorizations = options.authorizations;
} else {
var e = (typeof window !== 'undefined' ? window : exports);
this.clientAuthorizations = e.authorizations;
this.clientAuthorizations = authorizations;
}

this.supportedSubmitMethods = options.supportedSubmitMethods || [];
@@ -407,8 +406,7 @@ SwaggerClient.prototype.build = function(mock) {
setTimeout(function() { self.buildFromSpec(self.spec); }, 10);
}
else {
var e = (typeof window !== 'undefined' ? window : exports);
var status = e.authorizations.apply(obj);
authorizations.apply(obj);
if(mock)
return obj;
new SwaggerHttp().execute(obj);
@@ -833,7 +831,7 @@ Operation.prototype.help = function(dontPrint) {
var out = this.nickname + ': ' + this.summary + '\n';
for(var i = 0; i < this.parameters.length; i++) {
var param = this.parameters[i];
var typeInfo = typeFromJsonSchema(param.type, param.format);
var typeInfo = param.signature;
out += '\n * ' + param.name + ' (' + typeInfo + '): ' + param.description;
}
if(typeof dontPrint === 'undefined')
@@ -951,9 +949,8 @@ Operation.prototype.getMissingParams = function(args) {
return missingParams;
};

Operation.prototype.getBody = function(headers, args) {
var formParams = {};
var body;
Operation.prototype.getBody = function(headers, args, opts) {
var formParams = {}, body, key;

for(var i = 0; i < this.parameters.length; i++) {
var param = this.parameters[i];
@@ -969,7 +966,6 @@ Operation.prototype.getBody = function(headers, args) {
// handle form params
if(headers['Content-Type'] === 'application/x-www-form-urlencoded') {
var encoded = "";
var key;
for(key in formParams) {
value = formParams[key];
if(typeof value !== 'undefined'){
@@ -980,6 +976,25 @@ Operation.prototype.getBody = function(headers, args) {
}
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;
};
@@ -1042,9 +1057,8 @@ Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
success = (success||log);
error = (error||log);

if(typeof opts.useJQuery === 'boolean') {
if(opts.useJQuery)
this.useJQuery = opts.useJQuery;
}

var missingParams = this.getMissingParams(args);
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 contentTypeHeaders) { headers[attrname] = contentTypeHeaders[attrname]; }

var body = this.getBody(headers, args);
var body = this.getBody(headers, args, opts);
var url = this.urlify(args);

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)
return obj;
else
@@ -1160,14 +1174,24 @@ Operation.prototype.setContentTypes = function(args, opts) {
};

Operation.prototype.asCurl = function (args) {
var obj = this.execute(args, {mock: true});
authorizations.apply(obj);
var results = [];
var headers = this.getHeaderParams(args);
if (headers) {
results.push('-X ' + this.method.toUpperCase());
if (obj.headers) {
var key;
for (key in headers)
results.push("--header \"" + key + ": " + headers[key] + "\"");
for (key in obj.headers)
results.push('--header "' + key + ': ' + obj.headers[key] + '"');
}
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(" ")) + " " + this.urlify(args);
return 'curl ' + (results.join(' ')) + ' "' + obj.url + '"';
};

Operation.prototype.encodePathCollection = function(type, name, value) {
@@ -2812,7 +2836,14 @@ SwaggerHttp.prototype.execute = function(obj, opts) {
this.useJQuery = this.isIE8();

if(obj && typeof obj.body === 'object') {
obj.body = JSON.stringify(obj.body);
if(obj.body.type && obj.body.type !== 'formData')
obj.body = JSON.stringify(obj.body);
else {
obj.contentType = false;
obj.processData = false;
// delete obj.cache;
delete obj.headers['Content-Type'];
}
}

if(this.useJQuery)
@@ -2853,7 +2884,9 @@ JQueryHttpClient.prototype.execute = function(obj) {

obj.type = obj.method;
obj.cache = false;
delete obj.useJQuery;

/*
obj.beforeSend = function(xhr) {
var key, results;
if (obj.headers) {
@@ -2869,9 +2902,10 @@ JQueryHttpClient.prototype.execute = function(obj) {
}
return results;
}
};
};*/

obj.data = obj.body;
delete obj.body;
obj.complete = function(response, textStatus, opts) {
var headers = {},
headerArray = response.getAllResponseHeaders().split("\n");
@@ -3045,7 +3079,7 @@ ShredHttpClient.prototype.execute = function(obj) {

var e = (typeof window !== 'undefined' ? window : exports);

e.authorizations = new SwaggerAuthorizations();
e.authorizations = authorizations = new SwaggerAuthorizations();
e.ApiKeyAuthorization = ApiKeyAuthorization;
e.PasswordAuthorization = PasswordAuthorization;
e.CookieAuthorization = CookieAuthorization;


+ 1
- 1
package.json View File

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


Loading…
Cancel
Save