2 * Copyright 2016 2015-2016 ZTE, Inc. and others. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * Author: Zhaoxing Meng
17 * email: meng.zhaoxing1@zte.com.cn
20 * swagger-client - swagger.js is a javascript client for use with swaggering APIs.
22 * @link http://swagger.io
26 var ArrayModel = function(definition) {
27 this.name = "arrayModel";
28 this.definition = definition || {};
31 var requiredFields = definition.enum || [];
32 var innerType = definition.items;
35 this.type = typeFromJsonSchema(innerType.type, innerType.format);
38 this.ref = innerType.$ref;
44 ArrayModel.prototype.createJSONSample = function(modelsToIgnore) {
46 modelsToIgnore = (modelsToIgnore||{});
51 var name = simpleRef(this.ref);
52 if(typeof modelsToIgnore[name] === 'undefined') {
53 modelsToIgnore[name] = this;
54 result = models[name].createJSONSample(modelsToIgnore);
63 ArrayModel.prototype.getSampleValue = function(modelsToIgnore) {
65 modelsToIgnore = (modelsToIgnore || {});
70 var name = simpleRef(this.ref);
71 result = models[name].getSampleValue(modelsToIgnore);
76 ArrayModel.prototype.getMockSignature = function(modelsToIgnore) {
77 var propertiesStr = [];
79 for (i = 0; i < this.properties.length; i++) {
80 prop = this.properties[i];
81 propertiesStr.push(prop.toString());
84 var strong = '<span class="strong">';
85 var stronger = '<span class="stronger">';
86 var strongClose = '</span>';
87 var classOpen = strong + 'array' + ' {' + strongClose;
88 var classClose = strong + '}' + strongClose;
89 var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
93 modelsToIgnore[this.name] = this;
94 for (i = 0; i < this.properties.length; i++) {
95 prop = this.properties[i];
97 var model = models[ref];
98 if (model && typeof modelsToIgnore[ref] === 'undefined') {
99 returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore));
107 * SwaggerAuthorizations applys the correct authorization to an operation being executed
109 var SwaggerAuthorizations = function() {
113 SwaggerAuthorizations.prototype.add = function(name, auth) {
114 this.authz[name] = auth;
118 SwaggerAuthorizations.prototype.remove = function(name) {
119 return delete this.authz[name];
122 SwaggerAuthorizations.prototype.apply = function (obj, authorizations) {
124 var key, name, value, result;
126 // if the "authorizations" key is undefined, or has an empty array, add all keys
127 if (typeof authorizations === 'undefined' || Object.keys(authorizations).length === 0) {
128 for (key in this.authz) {
129 value = this.authz[key];
130 result = value.apply(obj, authorizations);
137 if (Array.isArray(authorizations)) {
139 for (var i = 0; i < authorizations.length; i++) {
140 var auth = authorizations[i];
142 for (key in this.authz) {
144 value = this.authz[key];
145 result = value.apply(obj, authorizations);
155 for (name in authorizations) {
156 for (key in this.authz) {
158 value = this.authz[key];
159 result = value.apply(obj, authorizations);
172 * ApiKeyAuthorization allows a query param or header to be injected
174 var ApiKeyAuthorization = function(name, value, type) {
180 ApiKeyAuthorization.prototype.apply = function(obj, authorizations) {
181 if (this.type === "query") {
182 if (obj.url.indexOf('?') > 0)
183 obj.url = obj.url + "&" + this.name + "=" + this.value;
185 obj.url = obj.url + "?" + this.name + "=" + this.value;
187 } else if (this.type === "header") {
188 obj.headers[this.name] = this.value;
193 var CookieAuthorization = function(cookie) {
194 this.cookie = cookie;
197 CookieAuthorization.prototype.apply = function(obj, authorizations) {
198 obj.cookieJar = obj.cookieJar || CookieJar();
199 obj.cookieJar.setCookie(this.cookie);
204 * Password Authorization is a basic auth implementation
206 var PasswordAuthorization = function(name, username, password) {
208 this.username = username;
209 this.password = password;
211 if (typeof window !== 'undefined')
214 this._btoa = require("btoa");
217 PasswordAuthorization.prototype.apply = function(obj, authorizations) {
218 var base64encoder = this._btoa;
219 obj.headers.Authorization = "Basic " + base64encoder(this.username + ":" + this.password);
222 var __bind = function(fn, me){
224 return fn.apply(me, arguments);
228 fail = function(message) {
233 log.history = log.history || [];
234 log.history.push(arguments);
236 console.log( Array.prototype.slice.call(arguments)[0] );
240 if (!Array.prototype.indexOf) {
241 Array.prototype.indexOf = function(obj, start) {
242 for (var i = (start || 0), j = this.length; i < j; i++) {
243 if (this[i] === obj) { return i; }
250 * allows override of the default value based on the parameter being
253 var applyParameterMacro = function (operation, parameter) {
254 var e = (typeof window !== 'undefined' ? window : exports);
256 return e.parameterMacro(operation, parameter);
258 return parameter.defaultValue;
262 * allows overriding the default value of an model property
264 var applyModelPropertyMacro = function (model, property) {
265 var e = (typeof window !== 'undefined' ? window : exports);
266 if(e.modelPropertyMacro)
267 return e.modelPropertyMacro(model, property);
269 return property.defaultValue;
275 var PrimitiveModel = function(definition) {
277 this.definition = definition || {};
278 this.properties = [];
280 var requiredFields = definition.enum || [];
281 this.type = typeFromJsonSchema(definition.type, definition.format);
284 PrimitiveModel.prototype.createJSONSample = function(modelsToIgnore) {
285 var result = this.type;
289 PrimitiveModel.prototype.getSampleValue = function() {
290 var result = this.type;
294 PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) {
295 var propertiesStr = [];
297 for (i = 0; i < this.properties.length; i++) {
298 prop = this.properties[i];
299 propertiesStr.push(prop.toString());
302 var strong = '<span class="strong">';
303 var stronger = '<span class="stronger">';
304 var strongClose = '</span>';
305 var classOpen = strong + this.name + ' {' + strongClose;
306 var classClose = strong + '}' + strongClose;
307 var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
311 modelsToIgnore[this.name] = this;
312 for (i = 0; i < this.properties.length; i++) {
313 prop = this.properties[i];
315 var model = models[ref];
316 if (model && typeof modelsToIgnore[ref] === 'undefined') {
317 returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore));
323 * Resolves a spec's remote references
325 var Resolver = function (){};
327 Resolver.prototype.resolve = function(spec, callback, scope) {
328 this.scope = (scope || this);
329 var host, name, path, property, propertyName, type;
330 var processedCalls = 0, resolvedRefs = {}, unresolvedRefs = {};
332 // store objects for dereferencing
333 var resolutionTable = {};
336 for(name in spec.definitions) {
337 var model = spec.definitions[name];
338 for(propertyName in model.properties) {
339 property = model.properties[propertyName];
340 this.resolveTo(property, resolutionTable);
344 for(name in spec.paths) {
345 var method, operation, responseCode;
346 path = spec.paths[name];
347 for(method in path) {
348 operation = path[method];
349 var i, parameters = operation.parameters;
350 for(i in parameters) {
351 var parameter = parameters[i];
352 if(parameter.in === 'body' && parameter.schema) {
353 this.resolveTo(parameter.schema, resolutionTable);
356 this.resolveInline(spec, parameter, resolutionTable, unresolvedRefs);
359 for(responseCode in operation.responses) {
360 var response = operation.responses[responseCode];
361 if(response.schema) {
362 this.resolveTo(response.schema, resolutionTable);
368 var opts = {}, expectedCalls = 0;
369 for(name in resolutionTable) {
370 var parts = name.split('#');
371 if(parts.length == 2) {
372 host = parts[0]; path = parts[1];
373 if(!Array.isArray(opts[host])) {
377 opts[host].push(path);
382 var self = this, opt = opts[name];
386 useJQuery: false, // TODO
390 accept: this.scope.swaggerRequestHeaders || 'application/json'
393 error: function(response) {
396 for(i = 0; i < opt.length; i++) {
398 var resolved = host + '#' + opt[i];
399 unresolvedRefs[resolved] = null;
401 if(processedCalls === expectedCalls)
402 self.finish(spec, resolutionTable, resolvedRefs, unresolvedRefs, callback);
404 response: function(response) {
405 var i, j, swagger = response.obj;
407 for(i = 0; i < opt.length; i++) {
408 var location = swagger, path = opt[i], parts = path.split('/');
409 for(j = 0; j < parts.length; j++) {
410 var segment = parts[j];
411 if(typeof location === 'undefined')
413 if(segment.length > 0)
414 location = location[segment];
416 var resolved = host + '#' + path, resolvedName = parts[j-1];
417 if(typeof location !== 'undefined') {
418 resolvedRefs[resolved] = {
423 else unresolvedRefs[resolved] = null;
425 if(processedCalls === expectedCalls)
426 self.finish(spec, resolutionTable, resolvedRefs, unresolvedRefs, callback);
430 authorizations.apply(obj);
431 new SwaggerHttp().execute(obj);
433 if(Object.keys(opts).length === 0)
434 callback.call(this.scope, spec, unresolvedRefs);
437 Resolver.prototype.finish = function(spec, resolutionTable, resolvedRefs, unresolvedRefs, callback) {
438 // walk resolution table and replace with resolved refs
440 for(ref in resolutionTable) {
441 var i, locations = resolutionTable[ref];
442 for(i = 0; i < locations.length; i++) {
443 var resolvedTo = resolvedRefs[locations[i].obj.$ref];
445 if(!spec.definitions)
446 spec.definitions = {};
447 if(locations[i].resolveAs === '$ref') {
448 spec.definitions[resolvedTo.name] = resolvedTo.obj;
449 locations[i].obj.$ref = '#/definitions/' + resolvedTo.name;
451 else if (locations[i].resolveAs === 'inline') {
453 var targetObj = locations[i].obj;
454 delete targetObj.$ref;
455 for(key in resolvedTo.obj) {
456 targetObj[key] = resolvedTo.obj[key];
462 callback.call(this.scope, spec, unresolvedRefs);
466 * immediately in-lines local refs, queues remote refs
467 * for inline resolution
469 Resolver.prototype.resolveInline = function (spec, property, objs, unresolvedRefs) {
470 var ref = property.$ref;
472 if(ref.indexOf('http') === 0) {
473 if(Array.isArray(objs[ref])) {
474 objs[ref].push({obj: property, resolveAs: 'inline'});
477 objs[ref] = [{obj: property, resolveAs: 'inline'}];
480 else if (ref.indexOf('#') === 0) {
482 var shortenedRef = ref.substring(1);
483 var i, parts = shortenedRef.split('/'), location = spec;
484 for(i = 0; i < parts.length; i++) {
486 if(part.length > 0) {
487 location = location[part];
491 delete property.$ref;
493 for(key in location) {
494 property[key] = location[key];
497 else unresolvedRefs[ref] = null;
500 else if(property.type === 'array') {
501 this.resolveTo(property.items, objs);
505 Resolver.prototype.resolveTo = function (property, objs) {
506 var ref = property.$ref;
508 if(ref.indexOf('http') === 0) {
509 if(Array.isArray(objs[ref])) {
510 objs[ref].push({obj: property, resolveAs: '$ref'});
513 objs[ref] = [{obj: property, resolveAs: '$ref'}];
517 else if(property.type === 'array') {
518 var items = property.items;
519 this.resolveTo(items, objs);
522 var addModel = function(name, model) {
523 models[name] = model;
526 var SwaggerClient = function(url, options) {
527 this.isBuilt = false;
530 this.basePath = null;
531 this.modelsArray = [];
532 this.authorizations = null;
533 this.authorizationScheme = null;
534 this.isValid = false;
536 this.useJQuery = false;
537 this.resourceCount = 0;
539 if(typeof url !== 'undefined')
540 return this.initialize(url, options);
543 SwaggerClient.prototype.initialize = function (url, options) {
544 this.models = models = {};
546 options = (options||{});
548 if(typeof url === 'string')
550 else if(typeof url === 'object') {
552 this.url = options.url;
554 this.swaggerRequstHeaders = options.swaggerRequstHeaders || 'application/json;charset=utf-8,*/*';
555 this.defaultSuccessCallback = options.defaultSuccessCallback || null;
556 this.defaultErrorCallback = options.defaultErrorCallback || null;
558 if (typeof options.success === 'function')
559 this.success = options.success;
561 if (options.useJQuery)
562 this.useJQuery = options.useJQuery;
564 if (options.authorizations) {
565 this.clientAuthorizations = options.authorizations;
567 this.clientAuthorizations = authorizations;
570 this.supportedSubmitMethods = options.supportedSubmitMethods || [];
571 this.failure = options.failure || function() {};
572 this.progress = options.progress || function() {};
573 this.spec = options.spec;
574 this.options = options;
576 if (typeof options.success === 'function') {
582 SwaggerClient.prototype.build = function(mock) {
583 if (this.isBuilt) return this;
585 this.progress('fetching resource list: ' + this.url);
587 useJQuery: this.useJQuery,
591 accept: this.swaggerRequstHeaders
594 error: function(response) {
595 if (self.url.substring(0, 4) !== 'http')
596 return self.fail('Please specify the protocol for ' + self.url);
597 else if (response.status === 0)
598 return self.fail('Can\'t read from server. It may not have the appropriate access-control-origin settings.');
599 else if (response.status === 404)
600 return self.fail('Can\'t read swagger JSON from ' + self.url);
602 return self.fail(response.status + ' : ' + response.statusText + ' ' + self.url);
604 response: function(resp) {
605 var responseObj = resp.obj || JSON.parse(resp.data);
606 self.swaggerVersion = responseObj.swaggerVersion;
608 if(responseObj.swagger && parseInt(responseObj.swagger) === 2) {
609 self.swaggerVersion = responseObj.swagger;
610 new Resolver().resolve(responseObj, self.buildFromSpec, self);
614 if (self.swaggerVersion === '1.2') {
615 return self.buildFrom1_2Spec(responseObj);
617 return self.buildFrom1_1Spec(responseObj);
624 setTimeout(function() {
625 new Resolver().resolve(self.spec, self.buildFromSpec, self);
629 authorizations.apply(obj);
632 new SwaggerHttp().execute(obj);
637 SwaggerClient.prototype.buildFromSpec = function(response) {
638 if(this.isBuilt) return this;
640 this.info = response.info || {};
641 this.title = response.title || '';
642 this.host = response.host || '';
643 this.schemes = response.schemes || [];
644 this.basePath = response.basePath || '';
647 this.consumes = response.consumes;
648 this.produces = response.produces;
649 this.securityDefinitions = response.securityDefinitions;
652 this.authSchemes = response.securityDefinitions;
654 var definedTags = {};
655 if(Array.isArray(response.tags)) {
657 for(k = 0; k < response.tags.length; k++) {
658 var t = response.tags[k];
659 definedTags[t.name] = t;
664 if(typeof this.url === 'string') {
665 location = this.parseUri(this.url);
668 if(typeof this.schemes === 'undefined' || this.schemes.length === 0) {
669 this.scheme = location.scheme || 'http';
672 this.scheme = this.schemes[0];
675 if(typeof this.host === 'undefined' || this.host === '') {
676 this.host = location.host;
678 this.host = this.host + ':' + location.port;
682 this.definitions = response.definitions;
684 for(key in this.definitions) {
685 var model = new Model(key, this.definitions[key]);
691 // get paths, create functions for each operationId
694 for(path in response.paths) {
695 if(typeof response.paths[path] === 'object') {
697 for(httpMethod in response.paths[path]) {
698 if(['delete', 'get', 'head', 'options', 'patch', 'post', 'put'].indexOf(httpMethod) === -1) {
701 var operation = response.paths[path][httpMethod];
702 var tags = operation.tags;
703 if(typeof tags === 'undefined') {
704 operation.tags = [ 'default' ];
705 tags = operation.tags;
707 var operationId = this.idFromOp(path, httpMethod, operation);
708 var operationObject = new Operation (
717 // bind this operation's execute command to the api
718 if(tags.length > 0) {
720 for(i = 0; i < tags.length; i++) {
721 var tag = this.tagFromLabel(tags[i]);
722 var operationGroup = this[tag];
723 if(typeof this.apis[tag] === 'undefined')
725 if(typeof operationGroup === 'undefined') {
727 operationGroup = this[tag];
728 operationGroup.operations = {};
729 operationGroup.label = tag;
730 operationGroup.apis = [];
731 var tagObject = definedTags[tag];
732 if(typeof tagObject === 'object') {
733 operationGroup.description = tagObject.description;
734 operationGroup.externalDocs = tagObject.externalDocs;
736 this[tag].help = this.help.bind(operationGroup);
737 this.apisArray.push(new OperationGroup(tag, operationGroup.description, operationGroup.externalDocs, operationObject));
739 if(typeof this.apis[tag].help !== 'function')
740 this.apis[tag].help = this.help.bind(operationGroup);
741 // bind to the apis object
742 this.apis[tag][operationId] = operationObject.execute.bind(operationObject);
743 this.apis[tag][operationId].help = operationObject.help.bind(operationObject);
744 this.apis[tag][operationId].asCurl = operationObject.asCurl.bind(operationObject);
745 operationGroup[operationId] = operationObject.execute.bind(operationObject);
746 operationGroup[operationId].help = operationObject.help.bind(operationObject);
747 operationGroup[operationId].asCurl = operationObject.asCurl.bind(operationObject);
749 operationGroup.apis.push(operationObject);
750 operationGroup.operations[operationId] = operationObject;
755 for(j = 0; j < this.apisArray.length; j++) {
756 if(this.apisArray[j].tag === tag) {
757 api = this.apisArray[j];
761 api.operationsArray.push(operationObject);
766 log('no group to bind to');
780 SwaggerClient.prototype.parseUri = function(uri) {
781 var urlParseRE = /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/;
782 var parts = urlParseRE.exec(uri);
784 scheme: parts[4].replace(':',''),
791 SwaggerClient.prototype.help = function(dontPrint) {
793 var output = 'operations for the "' + this.label + '" tag';
794 for(i = 0; i < this.apis.length; i++) {
795 var api = this.apis[i];
796 output += '\n * ' + api.nickname + ': ' + api.operation.summary;
806 SwaggerClient.prototype.tagFromLabel = function(label) {
810 SwaggerClient.prototype.idFromOp = function(path, httpMethod, op) {
811 var opId = op.operationId || (path.substring(1) + '_' + httpMethod);
812 return opId.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()\+\s]/g,'_');
815 SwaggerClient.prototype.fail = function(message) {
816 this.failure(message);
820 var OperationGroup = function(tag, description, externalDocs, operation) {
823 this.description = description;
824 this.externalDocs = externalDocs;
826 this.operation = operation;
827 this.operationsArray = [];
830 var Operation = function(parent, scheme, operationId, httpMethod, path, args, definitions) {
835 this.operations = {};
836 this.operation = args;
837 this.deprecated = args.deprecated;
838 this.consumes = args.consumes;
839 this.produces = args.produces;
840 this.parent = parent;
841 this.host = parent.host || 'localhost';
842 this.schemes = parent.schemes;
843 this.scheme = scheme || parent.scheme || 'http';
844 // this.basePath = window.location.pathname.split("/api-doc")[0]+parent.basePath || '/';
845 // var url = window.location.search.match(/\?url=(.*)\/.*.json.*$/);
847 if (window.location.search && window.location.search.length > 1) {
848 url = window.location.search.split("&api=")[1];
852 // if(url.lastIndexOf("runtime")>0){
853 // var realUrl = window.location.search.match(/\&api=(.*)$/);
859 url = "/api/microservices/v1";
866 this.nickname = (operationId||errors.push('Operations must have a nickname.'));
867 this.method = (httpMethod||errors.push('Operation ' + operationId + ' is missing method.'));
868 this.path = (path||errors.push('Operation ' + this.nickname + ' is missing path.'));
869 this.parameters = args !== null ? (args.parameters||[]) : {};
870 this.summary = args.summary || '';
871 this.responses = (args.responses||{});
873 this.security = args.security;
874 this.authorizations = args.security;
875 this.description = args.description;
876 this.useJQuery = parent.useJQuery;
878 if(typeof this.deprecated === 'string') {
879 switch(this.deprecated.toLowerCase()) {
880 case 'true': case 'yes': case '1': {
881 this.deprecated = true;
884 case 'false': case 'no': case '0': case null: {
885 this.deprecated = false;
888 default: this.deprecated = Boolean(this.deprecated);
895 // add to global models
897 for(key in this.definitions) {
898 model = new Model(key, definitions[key]);
904 for(i = 0; i < this.parameters.length; i++) {
905 var param = this.parameters[i];
906 if(param.type === 'array') {
908 param.allowMultiple = true;
910 var innerType = this.getType(param);
911 if(innerType && innerType.toString().toLowerCase() === 'boolean') {
912 param.allowableValues = {};
914 param['enum'] = ["true", "false"];
916 if(typeof param['enum'] !== 'undefined') {
918 param.allowableValues = {};
919 param.allowableValues.values = [];
920 param.allowableValues.descriptiveValues = [];
921 for(id = 0; id < param['enum'].length; id++) {
922 var value = param['enum'][id];
923 var isDefault = (value === param.default) ? true : false;
924 param.allowableValues.values.push(value);
925 param.allowableValues.descriptiveValues.push({value : value, isDefault: isDefault});
928 if(param.type === 'array') {
929 innerType = [innerType];
930 if(typeof param.allowableValues === 'undefined') {
931 // can't show as a list if no values to select from
933 delete param.allowMultiple;
936 param.signature = this.getModelSignature(innerType, models).toString();
937 param.sampleJSON = this.getModelSampleJSON(innerType, models);
938 param.responseClassSignature = param.signature;
941 var defaultResponseCode, response, responses = this.responses;
943 if(responses['200']) {
944 response = responses['200'];
945 defaultResponseCode = '200';
947 else if(responses['201']) {
948 response = responses['201'];
949 defaultResponseCode = '201';
951 else if(responses['202']) {
952 response = responses['202'];
953 defaultResponseCode = '202';
955 else if(responses['203']) {
956 response = responses['203'];
957 defaultResponseCode = '203';
959 else if(responses['204']) {
960 response = responses['204'];
961 defaultResponseCode = '204';
963 else if(responses['205']) {
964 response = responses['205'];
965 defaultResponseCode = '205';
967 else if(responses['206']) {
968 response = responses['206'];
969 defaultResponseCode = '206';
971 else if(responses['default']) {
972 response = responses['default'];
973 defaultResponseCode = 'default';
976 if(response && response.schema) {
977 var resolvedModel = this.resolveModel(response.schema, definitions);
978 delete responses[defaultResponseCode];
980 this.successResponse = {};
981 this.successResponse[defaultResponseCode] = resolvedModel;
984 this.successResponse = {};
985 this.successResponse[defaultResponseCode] = response.schema.type;
987 this.type = response;
990 if (errors.length > 0) {
991 if(this.resource && this.resource.api && this.resource.api.fail)
992 this.resource.api.fail(errors);
998 OperationGroup.prototype.sort = function(sorter) {
1002 Operation.prototype.getType = function (param) {
1003 var type = param.type;
1004 var format = param.format;
1005 var isArray = false;
1007 if(type === 'integer' && format === 'int32')
1009 else if(type === 'integer' && format === 'int64')
1011 else if(type === 'integer')
1013 else if(type === 'string') {
1014 if(format === 'date-time')
1016 else if(format === 'date')
1021 else if(type === 'number' && format === 'float')
1023 else if(type === 'number' && format === 'double')
1025 else if(type === 'number')
1027 else if(type === 'boolean')
1029 else if(type === 'array') {
1032 str = this.getType(param.items);
1037 var schema = param.schema;
1039 var ref = schema.$ref;
1041 ref = simpleRef(ref);
1048 return this.getType(schema);
1056 Operation.prototype.resolveModel = function (schema, definitions) {
1057 if(typeof schema.$ref !== 'undefined') {
1058 var ref = schema.$ref;
1059 if(ref.indexOf('#/definitions/') === 0)
1060 ref = ref.substring('#/definitions/'.length);
1061 if(definitions[ref]) {
1062 return new Model(ref, definitions[ref]);
1065 if(schema.type === 'array')
1066 return new ArrayModel(schema);
1071 Operation.prototype.help = function(dontPrint) {
1072 var out = this.nickname + ': ' + this.summary + '\n';
1073 for(var i = 0; i < this.parameters.length; i++) {
1074 var param = this.parameters[i];
1075 var typeInfo = param.signature;
1076 out += '\n * ' + param.name + ' (' + typeInfo + '): ' + param.description;
1078 if(typeof dontPrint === 'undefined')
1083 Operation.prototype.getModelSignature = function(type, definitions) {
1084 var isPrimitive, listType;
1086 if(type instanceof Array) {
1090 else if(typeof type === 'undefined')
1093 if(type === 'string')
1096 isPrimitive = (listType && definitions[listType]) || (definitions[type]) ? false : true;
1099 return 'Array[' + type + ']';
1101 return type.toString();
1104 return 'Array[' + definitions[type].getMockSignature() + ']';
1106 return definitions[type].getMockSignature();
1110 Operation.prototype.supportHeaderParams = function () {
1114 Operation.prototype.supportedSubmitMethods = function () {
1115 return this.parent.supportedSubmitMethods;
1118 Operation.prototype.getHeaderParams = function (args) {
1119 var headers = this.setContentTypes(args, {});
1120 for(var i = 0; i < this.parameters.length; i++) {
1121 var param = this.parameters[i];
1122 if(typeof args[param.name] !== 'undefined') {
1123 if (param.in === 'header') {
1124 var value = args[param.name];
1125 if(Array.isArray(value))
1126 value = value.toString();
1127 headers[param.name] = value;
1134 Operation.prototype.urlify = function (args) {
1135 var formParams = {};
1136 var requestUrl = this.path;
1138 // grab params from the args, build the querystring along the way
1139 var querystring = '';
1140 for(var i = 0; i < this.parameters.length; i++) {
1141 var param = this.parameters[i];
1142 if(typeof args[param.name] !== 'undefined') {
1143 if(param.in === 'path') {
1144 var reg = new RegExp('\{' + param.name + '\}', 'gi');
1145 var value = args[param.name];
1146 if(Array.isArray(value))
1147 value = this.encodePathCollection(param.collectionFormat, param.name, value);
1149 value = this.encodePathParam(value);
1150 requestUrl = requestUrl.replace(reg, value);
1152 else if (param.in === 'query' && typeof args[param.name] !== 'undefined') {
1153 if(querystring === '')
1157 if(typeof param.collectionFormat !== 'undefined') {
1158 var qp = args[param.name];
1159 if(Array.isArray(qp))
1160 querystring += this.encodeQueryCollection(param.collectionFormat, param.name, qp);
1162 querystring += this.encodeQueryParam(param.name) + '=' + this.encodeQueryParam(args[param.name]);
1165 querystring += this.encodeQueryParam(param.name) + '=' + this.encodeQueryParam(args[param.name]);
1167 else if (param.in === 'formData')
1168 formParams[param.name] = args[param.name];
1171 var url = this.scheme + '://' + this.host;
1173 if(this.basePath !== '/')
1174 url += this.basePath;
1176 return url + requestUrl + querystring;
1179 Operation.prototype.getMissingParams = function(args) {
1180 var missingParams = [];
1181 // check required params, track the ones that are missing
1183 for(i = 0; i < this.parameters.length; i++) {
1184 var param = this.parameters[i];
1185 if(param.required === true) {
1186 if(typeof args[param.name] === 'undefined')
1187 missingParams = param.name;
1190 return missingParams;
1193 Operation.prototype.getBody = function(headers, args, opts) {
1194 var formParams = {}, body, key;
1196 for(var i = 0; i < this.parameters.length; i++) {
1197 var param = this.parameters[i];
1198 if(typeof args[param.name] !== 'undefined') {
1199 if (param.in === 'body') {
1200 body = args[param.name];
1201 } else if(param.in === 'formData') {
1202 formParams[param.name] = args[param.name];
1207 // handle form params
1208 if(headers['Content-Type'] === 'application/x-www-form-urlencoded') {
1210 for(key in formParams) {
1211 value = formParams[key];
1212 if(typeof value !== 'undefined'){
1215 encoded += encodeURIComponent(key) + '=' + encodeURIComponent(value);
1220 else if (headers['Content-Type'] && headers['Content-Type'].indexOf('multipart/form-data') >= 0) {
1221 if(opts.useJQuery) {
1222 var bodyParam = new FormData();
1223 bodyParam.type = 'formData';
1224 for (key in formParams) {
1226 if (typeof value !== 'undefined') {
1227 // required for jquery file upload
1228 if(value.type === 'file' && value.value) {
1229 delete headers['Content-Type'];
1230 bodyParam.append(key, value.value);
1233 bodyParam.append(key, value);
1244 * gets sample response for a single operation
1246 Operation.prototype.getModelSampleJSON = function(type, models) {
1247 var isPrimitive, listType, sampleJson;
1249 listType = (type instanceof Array);
1250 isPrimitive = models[type] ? false : true;
1251 sampleJson = isPrimitive ? void 0 : models[type].createJSONSample();
1253 sampleJson = listType ? [sampleJson] : sampleJson;
1254 if(typeof sampleJson == 'string')
1256 else if(typeof sampleJson === 'object') {
1258 if(sampleJson instanceof Array && sampleJson.length > 0) {
1262 var xmlString = new XMLSerializer().serializeToString(t);
1263 return this.formatXml(xmlString);
1266 return JSON.stringify(sampleJson, null, 2);
1276 Operation.prototype["do"] = function(args, opts, callback, error, parent) {
1277 return this.execute(args, opts, callback, error, parent);
1282 * executes an operation
1284 Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
1285 var args = arg1 || {};
1286 var opts = {}, success, error;
1287 if(typeof arg2 === 'object') {
1293 if(typeof arg2 === 'function') {
1298 success = (success||log);
1299 error = (error||log);
1302 this.useJQuery = opts.useJQuery;
1304 var missingParams = this.getMissingParams(args);
1305 if(missingParams.length > 0) {
1306 var message = 'missing required params: ' + missingParams;
1310 var allHeaders = this.getHeaderParams(args);
1311 var contentTypeHeaders = this.setContentTypes(args, opts);
1313 var headers = {}, attrname;
1314 for (attrname in allHeaders) { headers[attrname] = allHeaders[attrname]; }
1315 for (attrname in contentTypeHeaders) { headers[attrname] = contentTypeHeaders[attrname]; }
1317 var body = this.getBody(headers, args, opts);
1318 var url = this.urlify(args);
1322 method: this.method.toUpperCase(),
1324 useJQuery: this.useJQuery,
1327 response: function(response) {
1328 return success(response, parent);
1330 error: function(response) {
1331 return error(response, parent);
1335 var status = authorizations.apply(obj, this.operation.security);
1336 if(opts.mock === true)
1339 new SwaggerHttp().execute(obj, opts);
1342 Operation.prototype.setContentTypes = function(args, opts) {
1344 var accepts = 'application/json';
1345 var consumes = args.parameterContentType || 'application/json';
1346 var allDefinedParams = this.parameters;
1347 var definedFormParams = [];
1348 var definedFileParams = [];
1352 // get params from the operation and set them in definedFileParams, definedFormParams, headers
1354 for(i = 0; i < allDefinedParams.length; i++) {
1355 var param = allDefinedParams[i];
1356 if(param.in === 'formData') {
1357 if(param.type === 'file')
1358 definedFileParams.push(param);
1360 definedFormParams.push(param);
1362 else if(param.in === 'header' && opts) {
1363 var key = param.name;
1364 var headerValue = opts[param.name];
1365 if(typeof opts[param.name] !== 'undefined')
1366 headers[key] = headerValue;
1368 else if(param.in === 'body' && typeof args[param.name] !== 'undefined') {
1369 body = args[param.name];
1373 // if there's a body, need to set the consumes header via requestContentType
1374 if (body && (this.method === 'post' || this.method === 'put' || this.method === 'patch' || this.method === 'delete')) {
1375 if (opts.requestContentType)
1376 consumes = opts.requestContentType;
1378 // if any form params, content type must be set
1379 if(definedFormParams.length > 0) {
1380 if(opts.requestContentType) // override if set
1381 consumes = opts.requestContentType;
1382 else if(definedFileParams.length > 0) // if a file, must be multipart/form-data
1383 consumes = 'multipart/form-data';
1384 else // default to x-www-from-urlencoded
1385 consumes = 'application/x-www-form-urlencoded';
1387 else if (this.type == 'DELETE')
1389 else if (this.type != 'DELETE')
1393 if (consumes && this.consumes) {
1394 if (this.consumes.indexOf(consumes) === -1) {
1395 log('server doesn\'t consume ' + consumes + ', try ' + JSON.stringify(this.consumes));
1399 if (opts.responseContentType) {
1400 accepts = opts.responseContentType;
1402 accepts = 'application/json';
1404 if (accepts && this.produces) {
1405 if (this.produces.indexOf(accepts) === -1) {
1406 log('server can\'t produce ' + accepts);
1410 if ((consumes && body !== '') || (consumes === 'application/x-www-form-urlencoded'))
1411 headers['Content-Type'] = consumes;
1413 headers.Accept = accepts;
1417 Operation.prototype.asCurl = function (args) {
1418 var obj = this.execute(args, {mock: true});
1419 authorizations.apply(obj);
1421 results.push('-X ' + this.method.toUpperCase());
1424 for (key in obj.headers)
1425 results.push('--header "' + key + ': ' + obj.headers[key] + '"');
1429 if(typeof obj.body === 'object')
1430 body = JSON.stringify(obj.body);
1433 results.push('-d "' + body.replace(/"/g, '\\"') + '"');
1435 return 'curl ' + (results.join(' ')) + ' "' + obj.url + '"';
1438 Operation.prototype.encodePathCollection = function(type, name, value) {
1444 else if(type === 'tsv')
1446 else if(type === 'pipes')
1451 for(i = 0; i < value.length; i++) {
1453 encoded = this.encodeQueryParam(value[i]);
1455 encoded += separator + this.encodeQueryParam(value[i]);
1460 Operation.prototype.encodeQueryCollection = function(type, name, value) {
1463 if(type === 'default' || type === 'multi') {
1464 for(i = 0; i < value.length; i++) {
1465 if(i > 0) encoded += '&';
1466 encoded += this.encodeQueryParam(name) + '=' + this.encodeQueryParam(value[i]);
1473 else if(type === 'ssv')
1475 else if(type === 'tsv')
1477 else if(type === 'pipes')
1479 else if(type === 'brackets') {
1480 for(i = 0; i < value.length; i++) {
1483 encoded += this.encodeQueryParam(name) + '[]=' + this.encodeQueryParam(value[i]);
1486 if(separator !== '') {
1487 for(i = 0; i < value.length; i++) {
1489 encoded = this.encodeQueryParam(name) + '=' + this.encodeQueryParam(value[i]);
1491 encoded += separator + this.encodeQueryParam(value[i]);
1498 Operation.prototype.encodeQueryParam = function(arg) {
1499 return encodeURIComponent(arg);
1503 * TODO revisit, might not want to leave '/'
1505 Operation.prototype.encodePathParam = function(pathParam) {
1506 var encParts, part, parts, i, len;
1507 pathParam = pathParam.toString();
1508 if (pathParam.indexOf('/') === -1) {
1509 return encodeURIComponent(pathParam);
1511 parts = pathParam.split('/');
1513 for (i = 0, len = parts.length; i < len; i++) {
1514 encParts.push(encodeURIComponent(parts[i]));
1516 return encParts.join('/');
1520 var Model = function(name, definition) {
1522 this.definition = definition || {};
1523 this.properties = [];
1524 var requiredFields = definition.required || [];
1525 if(definition.type === 'array') {
1526 var out = new ArrayModel(definition);
1530 var props = definition.properties;
1533 var required = false;
1534 var property = props[key];
1535 if(requiredFields.indexOf(key) >= 0)
1537 this.properties.push(new Property(key, property, required));
1542 Model.prototype.createJSONSample = function(modelsToIgnore) {
1543 var i, result = {}, representations = {};
1544 modelsToIgnore = (modelsToIgnore||{});
1545 modelsToIgnore[this.name] = this;
1546 for (i = 0; i < this.properties.length; i++) {
1547 prop = this.properties[i];
1548 var sample = prop.getSampleValue(modelsToIgnore, representations);
1549 result[prop.name] = sample;
1551 delete modelsToIgnore[this.name];
1555 Model.prototype.getSampleValue = function(modelsToIgnore) {
1556 var i, obj = {}, representations = {};
1557 for(i = 0; i < this.properties.length; i++ ) {
1558 var property = this.properties[i];
1559 obj[property.name] = property.sampleValue(false, modelsToIgnore, representations);
1564 Model.prototype.getMockSignature = function(modelsToIgnore) {
1565 var i, prop, propertiesStr = [];
1566 for (i = 0; i < this.properties.length; i++) {
1567 prop = this.properties[i];
1568 propertiesStr.push(prop.toString());
1570 var strong = '<span class="strong">';
1571 var stronger = '<span class="stronger">';
1572 var strongClose = '</span>';
1573 var classOpen = strong + this.name + ' {' + strongClose;
1574 var classClose = strong + '}' + strongClose;
1575 var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
1576 if (!modelsToIgnore)
1577 modelsToIgnore = {};
1579 modelsToIgnore[this.name] = this;
1580 for (i = 0; i < this.properties.length; i++) {
1581 prop = this.properties[i];
1582 var ref = prop.$ref;
1583 var model = models[ref];
1584 if (model && typeof modelsToIgnore[model.name] === 'undefined') {
1585 returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore));
1591 var Property = function(name, obj, required) {
1593 this.required = required;
1595 this.$ref = simpleRef(obj.$ref);
1596 else if (obj.type === 'array' && obj.items) {
1598 this.$ref = simpleRef(obj.items.$ref);
1603 this.description = obj.description;
1605 this.optional = true;
1606 this.optional = !required;
1607 this.default = obj.default || null;
1608 this.example = obj.example !== undefined ? obj.example : null;
1609 this.collectionFormat = obj.collectionFormat || null;
1610 this.maximum = obj.maximum || null;
1611 this.exclusiveMaximum = obj.exclusiveMaximum || null;
1612 this.minimum = obj.minimum || null;
1613 this.exclusiveMinimum = obj.exclusiveMinimum || null;
1614 this.maxLength = obj.maxLength || null;
1615 this.minLength = obj.minLength || null;
1616 this.pattern = obj.pattern || null;
1617 this.maxItems = obj.maxItems || null;
1618 this.minItems = obj.minItems || null;
1619 this.uniqueItems = obj.uniqueItems || null;
1620 this['enum'] = obj['enum'] || null;
1621 this.multipleOf = obj.multipleOf || null;
1624 Property.prototype.getSampleValue = function (modelsToIgnore, representations) {
1625 return this.sampleValue(false, modelsToIgnore, representations);
1628 Property.prototype.isArray = function () {
1629 var schema = this.schema;
1630 if(schema.type === 'array')
1636 Property.prototype.sampleValue = function(isArray, ignoredModels, representations) {
1637 isArray = (isArray || this.isArray());
1638 ignoredModels = (ignoredModels || {});
1639 // representations = (representations || {});
1641 var type = getStringSignature(this.obj, true);
1645 var refModelName = simpleRef(this.$ref);
1646 var refModel = models[refModelName];
1647 if(typeof representations[type] !== 'undefined') {
1648 return representations[type];
1652 if(refModel && typeof ignoredModels[type] === 'undefined') {
1653 ignoredModels[type] = this;
1654 output = refModel.getSampleValue(ignoredModels, representations);
1655 representations[type] = output;
1658 output = (representations[type] || refModelName);
1661 else if(this.example)
1662 output = this.example;
1663 else if(this.default)
1664 output = this.default;
1665 else if(type === 'date-time')
1666 output = new Date().toISOString();
1667 else if(type === 'date')
1668 output = new Date().toISOString().split("T")[0];
1669 else if(type === 'string')
1671 else if(type === 'integer')
1673 else if(type === 'long')
1675 else if(type === 'float')
1677 else if(type === 'double')
1679 else if(type === 'boolean')
1683 ignoredModels[type] = output;
1690 getStringSignature = function(obj, baseComponent) {
1692 if(typeof obj.$ref !== 'undefined')
1693 str += simpleRef(obj.$ref);
1694 else if(typeof obj.type === 'undefined')
1696 else if(obj.type === 'array') {
1698 str += getStringSignature((obj.items || obj.$ref || {}));
1701 str += getStringSignature((obj.items || obj.$ref || {}));
1705 else if(obj.type === 'integer' && obj.format === 'int32')
1707 else if(obj.type === 'integer' && obj.format === 'int64')
1709 else if(obj.type === 'integer' && typeof obj.format === 'undefined')
1711 else if(obj.type === 'string' && obj.format === 'date-time')
1713 else if(obj.type === 'string' && obj.format === 'date')
1715 else if(obj.type === 'string' && typeof obj.format === 'undefined')
1717 else if(obj.type === 'number' && obj.format === 'float')
1719 else if(obj.type === 'number' && obj.format === 'double')
1721 else if(obj.type === 'number' && typeof obj.format === 'undefined')
1723 else if(obj.type === 'boolean')
1726 str += simpleRef(obj.$ref);
1732 simpleRef = function(name) {
1733 if(typeof name === 'undefined')
1735 if(name.indexOf("#/definitions/") === 0)
1736 return name.substring('#/definitions/'.length);
1741 Property.prototype.toString = function() {
1742 var str = getStringSignature(this.obj);
1744 str = '<span class="propName ' + this.required + '">' + this.name + '</span> (<span class="propType">' + str + '</span>';
1746 str += ', <span class="propOptKey">optional</span>';
1750 str = this.name + ' (' + JSON.stringify(this.obj) + ')';
1752 if(typeof this.description !== 'undefined')
1753 str += ': ' + this.description;
1756 str += ' = <span class="propVals">[\'' + this['enum'].join('\' or \'') + '\']</span>';
1759 str += ': <span class="propDesc">' + this.descr + '</span>';
1764 var isArray = this.schema.type === 'array';
1768 if(this.schema.items)
1769 type = this.schema.items.type;
1774 type = this.schema.type;
1778 options += optionHtml('Default', this.default);
1783 options += optionHtml('Min. Length', this.minLength);
1785 options += optionHtml('Max. Length', this.maxLength);
1787 options += optionHtml('Reg. Exp.', this.pattern);
1792 options += optionHtml('Min. Value', this.minimum);
1793 if (this.exclusiveMinimum)
1794 options += optionHtml('Exclusive Min.', "true");
1796 options += optionHtml('Max. Value', this.maximum);
1797 if (this.exclusiveMaximum)
1798 options += optionHtml('Exclusive Max.', "true");
1799 if (this.multipleOf)
1800 options += optionHtml('Multiple Of', this.multipleOf);
1806 options += optionHtml('Min. Items', this.minItems);
1808 options += optionHtml('Max. Items', this.maxItems);
1809 if (this.uniqueItems)
1810 options += optionHtml('Unique Items', "true");
1811 if (this.collectionFormat)
1812 options += optionHtml('Coll. Format', this.collectionFormat);
1818 if (type === 'number' || type === 'integer')
1819 enumString = this['enum'].join(', ');
1821 enumString = '"' + this['enum'].join('", "') + '"';
1824 options += optionHtml('Enum', enumString);
1827 if (options.length > 0)
1828 str = '<span class="propWrap">' + str + '<table class="optionsWrapper"><tr><th colspan="2">' + this.name + '</th></tr>' + options + '</table></span>';
1833 optionHtml = function(label, value) {
1834 return '<tr><td class="optionName">' + label + ':</td><td>' + value + '</td></tr>';
1837 typeFromJsonSchema = function(type, format) {
1839 if(type === 'integer' && format === 'int32')
1841 else if(type === 'integer' && format === 'int64')
1843 else if(type === 'integer' && typeof format === 'undefined')
1845 else if(type === 'string' && format === 'date-time')
1847 else if(type === 'string' && format === 'date')
1849 else if(type === 'number' && format === 'float')
1851 else if(type === 'number' && format === 'double')
1853 else if(type === 'number' && typeof format === 'undefined')
1855 else if(type === 'boolean')
1857 else if(type === 'string')
1863 var sampleModels = {};
1867 SwaggerClient.prototype.buildFrom1_2Spec = function (response) {
1868 if (response.apiVersion !== null) {
1869 this.apiVersion = response.apiVersion;
1872 this.apisArray = [];
1873 this.consumes = response.consumes;
1874 this.produces = response.produces;
1875 this.authSchemes = response.authorizations;
1876 this.info = this.convertInfo(response.info);
1878 var isApi = false, i, res;
1879 for (i = 0; i < response.apis.length; i++) {
1880 var api = response.apis[i];
1881 if (api.operations) {
1883 for (j = 0; j < api.operations.length; j++) {
1884 operation = api.operations[j];
1889 if (response.basePath)
1890 this.basePath = response.basePath;
1891 else if (this.url.indexOf('?') > 0)
1892 this.basePath = this.url.substring(0, this.url.lastIndexOf('?'));
1894 this.basePath = this.url;
1897 var newName = response.resourcePath.replace(/\//g, '');
1898 this.resourcePath = response.resourcePath;
1899 res = new SwaggerResource(response, this);
1900 this.apis[newName] = res;
1901 this.apisArray.push(res);
1905 this.expectedResourceCount = response.apis.length;
1906 for (k = 0; k < response.apis.length; k++) {
1907 var resource = response.apis[k];
1908 res = new SwaggerResource(resource, this);
1909 this.apis[res.name] = res;
1910 this.apisArray.push(res);
1913 this.isValid = true;
1917 SwaggerClient.prototype.finish = function() {
1918 if (typeof this.success === 'function') {
1919 this.isValid = true;
1921 this.isBuilt = true;
1927 SwaggerClient.prototype.buildFrom1_1Spec = function (response) {
1928 log('This API is using a deprecated version of Swagger! Please see http://github.com/wordnik/swagger-core/wiki for more info');
1929 if (response.apiVersion !== null)
1930 this.apiVersion = response.apiVersion;
1932 this.apisArray = [];
1933 this.produces = response.produces;
1934 this.info = this.convertInfo(response.info);
1935 var isApi = false, res;
1936 for (var i = 0; i < response.apis.length; i++) {
1937 var api = response.apis[i];
1938 if (api.operations) {
1939 for (var j = 0; j < api.operations.length; j++) {
1940 operation = api.operations[j];
1945 if (response.basePath) {
1946 this.basePath = response.basePath;
1947 } else if (this.url.indexOf('?') > 0) {
1948 this.basePath = this.url.substring(0, this.url.lastIndexOf('?'));
1950 this.basePath = this.url;
1953 var newName = response.resourcePath.replace(/\//g, '');
1954 this.resourcePath = response.resourcePath;
1955 res = new SwaggerResource(response, this);
1956 this.apis[newName] = res;
1957 this.apisArray.push(res);
1960 this.expectedResourceCount = response.apis.length;
1961 for (k = 0; k < response.apis.length; k++) {
1962 resource = response.apis[k];
1963 res = new SwaggerResource(resource, this);
1964 this.apis[res.name] = res;
1965 this.apisArray.push(res);
1968 this.isValid = true;
1972 SwaggerClient.prototype.convertInfo = function (resp) {
1973 if(typeof resp == 'object') {
1976 info.title = resp.title;
1977 info.description = resp.description;
1978 info.termsOfService = resp.termsOfServiceUrl;
1980 info.contact.name = resp.contact;
1982 info.license.name = resp.license;
1983 info.license.url = resp.licenseUrl;
1989 SwaggerClient.prototype.selfReflect = function () {
1990 var resource, tag, ref;
1991 if (this.apis === null) {
1997 if (api.ready === null) {
2001 this[tag].help = __bind(api.help, api);
2003 this.setConsolidatedModels();
2007 SwaggerClient.prototype.setConsolidatedModels = function () {
2008 var model, modelName, resource, resource_name, i, apis, models, results;
2011 for (resource_name in apis) {
2012 resource = apis[resource_name];
2013 for (modelName in resource.models) {
2014 if (typeof this.models[modelName] === 'undefined') {
2015 this.models[modelName] = resource.models[modelName];
2016 this.modelsArray.push(resource.models[modelName]);
2020 models = this.modelsArray;
2022 for (i = 0; i < models.length; i++) {
2024 results.push(model.setReferencedModels(this.models));
2029 var SwaggerResource = function (resourceObj, api) {
2032 this.swaggerRequstHeaders = api.swaggerRequstHeaders;
2033 this.path = (typeof this.api.resourcePath === 'string') ? this.api.resourcePath : resourceObj.path;
2034 this.description = resourceObj.description;
2035 this.authorizations = (resourceObj.authorizations || {});
2037 var parts = this.path.split('/');
2038 this.name = parts[parts.length - 1].replace('.{format}', '');
2039 this.basePath = this.api.basePath;
2040 this.operations = {};
2041 this.operationsArray = [];
2042 this.modelsArray = [];
2043 this.models = api.models || {};
2044 this.rawModels = {};
2045 this.useJQuery = (typeof api.useJQuery !== 'undefined') ? api.useJQuery : null;
2047 if ((resourceObj.apis) && this.api.resourcePath) {
2048 this.addApiDeclaration(resourceObj);
2050 if (typeof this.path === 'undefined') {
2051 this.api.fail('SwaggerResources must have a path.');
2053 if (this.path.substring(0, 4) === 'http') {
2054 this.url = this.path.replace('{format}', 'json');
2056 this.url = this.api.basePath + this.path.replace('{format}', 'json');
2058 this.api.progress('fetching resource ' + this.name + ': ' + this.url);
2062 useJQuery: this.useJQuery,
2064 accept: this.swaggerRequstHeaders
2067 response: function (resp) {
2068 var responseObj = resp.obj || JSON.parse(resp.data);
2069 _this.api.resourceCount += 1;
2070 return _this.addApiDeclaration(responseObj);
2072 error: function (response) {
2073 _this.api.resourceCount += 1;
2074 return _this.api.fail('Unable to read api \'' +
2075 _this.name + '\' from path ' + _this.url + ' (server returned ' + response.statusText + ')');
2079 var e = typeof window !== 'undefined' ? window : exports;
2080 e.authorizations.apply(obj);
2081 new SwaggerHttp().execute(obj);
2085 SwaggerResource.prototype.help = function (dontPrint) {
2087 var output = 'operations for the "' + this.name + '" tag';
2088 for(i = 0; i < this.operationsArray.length; i++) {
2089 var api = this.operationsArray[i];
2090 output += '\n * ' + api.nickname + ': ' + api.description;
2100 SwaggerResource.prototype.getAbsoluteBasePath = function (relativeBasePath) {
2102 url = this.api.basePath;
2103 pos = url.lastIndexOf(relativeBasePath);
2104 var parts = url.split('/');
2105 var rootUrl = parts[0] + '//' + parts[2];
2107 if (relativeBasePath.indexOf('http') === 0)
2108 return relativeBasePath;
2109 if (relativeBasePath === '/')
2111 if (relativeBasePath.substring(0, 1) == '/') {
2112 // use root + relative
2113 return rootUrl + relativeBasePath;
2116 pos = this.basePath.lastIndexOf('/');
2117 var base = this.basePath.substring(0, pos);
2118 if (base.substring(base.length - 1) == '/')
2119 return base + relativeBasePath;
2121 return base + '/' + relativeBasePath;
2125 SwaggerResource.prototype.addApiDeclaration = function (response) {
2126 if (typeof response.produces === 'string')
2127 this.produces = response.produces;
2128 if (typeof response.consumes === 'string')
2129 this.consumes = response.consumes;
2130 if ((typeof response.basePath === 'string') && response.basePath.replace(/\s/g, '').length > 0)
2131 this.basePath = response.basePath.indexOf('http') === -1 ? this.getAbsoluteBasePath(response.basePath) : response.basePath;
2132 this.resourcePath = response.resourcePath;
2133 this.addModels(response.models);
2134 if (response.apis) {
2135 for (var i = 0 ; i < response.apis.length; i++) {
2136 var endpoint = response.apis[i];
2137 this.addOperations(endpoint.path, endpoint.operations, response.consumes, response.produces);
2140 this.api[this.name] = this;
2142 if(this.api.resourceCount === this.api.expectedResourceCount)
2147 SwaggerResource.prototype.addModels = function (models) {
2148 if (typeof models === 'object') {
2150 for (modelName in models) {
2151 if (typeof this.models[modelName] === 'undefined') {
2152 var swaggerModel = new SwaggerModel(modelName, models[modelName]);
2153 this.modelsArray.push(swaggerModel);
2154 this.models[modelName] = swaggerModel;
2155 this.rawModels[modelName] = models[modelName];
2159 for (var i = 0; i < this.modelsArray.length; i++) {
2160 var model = this.modelsArray[i];
2161 output.push(model.setReferencedModels(this.models));
2167 SwaggerResource.prototype.addOperations = function (resource_path, ops, consumes, produces) {
2170 for (var i = 0; i < ops.length; i++) {
2172 consumes = this.consumes;
2173 produces = this.produces;
2174 if (typeof o.consumes !== 'undefined')
2175 consumes = o.consumes;
2177 consumes = this.consumes;
2179 if (typeof o.produces !== 'undefined')
2180 produces = o.produces;
2182 produces = this.produces;
2183 var type = (o.type || o.responseClass);
2185 if (type === 'array') {
2188 ref = o.items.type || o.items.$ref;
2189 type = 'array[' + ref + ']';
2191 var responseMessages = o.responseMessages;
2192 var method = o.method;
2194 method = o.httpMethod;
2196 if (o.supportedContentTypes) {
2197 consumes = o.supportedContentTypes;
2199 if (o.errorResponses) {
2200 responseMessages = o.errorResponses;
2201 for (var j = 0; j < responseMessages.length; j++) {
2202 r = responseMessages[j];
2203 r.message = r.reason;
2207 o.nickname = this.sanitize(o.nickname);
2208 var op = new SwaggerOperation(o.nickname,
2222 this.operations[op.nickname] = op;
2223 output.push(this.operationsArray.push(op));
2229 SwaggerResource.prototype.sanitize = function (nickname) {
2231 op = nickname.replace(/[\s!@#$%^&*()_+=\[{\]};:<>|.\/?,\\'""-]/g, '_');
2232 op = op.replace(/((_){2,})/g, '_');
2233 op = op.replace(/^(_)*/g, '');
2234 op = op.replace(/([_])*$/g, '');
2238 var SwaggerModel = function (modelName, obj) {
2239 this.name = typeof obj.id !== 'undefined' ? obj.id : modelName;
2240 this.properties = [];
2242 for (propertyName in obj.properties) {
2245 for (value in obj.required) {
2246 if (propertyName === obj.required[value]) {
2247 obj.properties[propertyName].required = true;
2251 var prop = new SwaggerModelProperty(propertyName, obj.properties[propertyName], this);
2252 this.properties.push(prop);
2256 SwaggerModel.prototype.setReferencedModels = function (allModels) {
2258 for (var i = 0; i < this.properties.length; i++) {
2259 var property = this.properties[i];
2260 var type = property.type || property.dataType;
2261 if (allModels[type])
2262 results.push(property.refModel = allModels[type]);
2263 else if ((property.refDataType) && (allModels[property.refDataType]))
2264 results.push(property.refModel = allModels[property.refDataType]);
2266 results.push(void 0);
2271 SwaggerModel.prototype.getMockSignature = function (modelsToIgnore) {
2272 var i, prop, propertiesStr = [];
2273 for (i = 0; i < this.properties.length; i++) {
2274 prop = this.properties[i];
2275 propertiesStr.push(prop.toString());
2278 var strong = '<span class="strong">';
2279 var strongClose = '</span>';
2280 var classOpen = strong + this.name + ' {' + strongClose;
2281 var classClose = strong + '}' + strongClose;
2282 var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
2283 if (!modelsToIgnore)
2284 modelsToIgnore = [];
2285 modelsToIgnore.push(this.name);
2287 for (i = 0; i < this.properties.length; i++) {
2288 prop = this.properties[i];
2289 if ((prop.refModel) && modelsToIgnore.indexOf(prop.refModel.name) === -1) {
2290 returnVal = returnVal + ('<br>' + prop.refModel.getMockSignature(modelsToIgnore));
2296 SwaggerModel.prototype.createJSONSample = function (modelsToIgnore) {
2297 if (sampleModels[this.name]) {
2298 return sampleModels[this.name];
2302 modelsToIgnore = (modelsToIgnore || []);
2303 modelsToIgnore.push(this.name);
2304 for (var i = 0; i < this.properties.length; i++) {
2305 var prop = this.properties[i];
2306 result[prop.name] = prop.getSampleValue(modelsToIgnore);
2308 modelsToIgnore.pop(this.name);
2313 var SwaggerModelProperty = function (name, obj, model) {
2315 this.dataType = obj.type || obj.dataType || obj.$ref;
2316 this.isCollection = this.dataType && (this.dataType.toLowerCase() === 'array' || this.dataType.toLowerCase() === 'list' || this.dataType.toLowerCase() === 'set');
2317 this.descr = obj.description;
2318 this.required = obj.required;
2319 this.defaultValue = applyModelPropertyMacro(obj, model);
2321 if (obj.items.type) {
2322 this.refDataType = obj.items.type;
2324 if (obj.items.$ref) {
2325 this.refDataType = obj.items.$ref;
2328 this.dataTypeWithRef = this.refDataType ? (this.dataType + '[' + this.refDataType + ']') : this.dataType;
2329 if (obj.allowableValues) {
2330 this.valueType = obj.allowableValues.valueType;
2331 this.values = obj.allowableValues.values;
2333 this.valuesString = '\'' + this.values.join('\' or \'') + '\'';
2337 this.valueType = 'string';
2338 this.values = obj['enum'];
2340 this.valueString = '\'' + this.values.join('\' or \'') + '\'';
2345 SwaggerModelProperty.prototype.getSampleValue = function (modelsToIgnore) {
2347 if ((this.refModel) && (modelsToIgnore.indexOf(this.refModel.name) === -1)) {
2348 result = this.refModel.createJSONSample(modelsToIgnore);
2350 if (this.isCollection) {
2351 result = this.toSampleValue(this.refDataType);
2353 result = this.toSampleValue(this.dataType);
2356 if (this.isCollection) {
2363 SwaggerModelProperty.prototype.toSampleValue = function (value) {
2365 if ((typeof this.defaultValue !== 'undefined') && this.defaultValue) {
2366 result = this.defaultValue;
2367 } else if (value === 'integer') {
2369 } else if (value === 'boolean') {
2371 } else if (value === 'double' || value === 'number') {
2373 } else if (value === 'string') {
2381 SwaggerModelProperty.prototype.toString = function () {
2382 var req = this.required ? 'propReq' : 'propOpt';
2383 var str = '<span class="propName ' + req + '">' + this.name + '</span> (<span class="propType">' + this.dataTypeWithRef + '</span>';
2384 if (!this.required) {
2385 str += ', <span class="propOptKey">optional</span>';
2389 str += ' = <span class="propVals">[\'' + this.values.join('\' or \'') + '\']</span>';
2392 str += ': <span class="propDesc">' + this.descr + '</span>';
2397 var SwaggerOperation = function (nickname, path, method, parameters, summary, notes, type, responseMessages, resource, consumes, produces, authorizations, deprecated) {
2401 this.nickname = (nickname || errors.push('SwaggerOperations must have a nickname.'));
2402 this.path = (path || errors.push('SwaggerOperation ' + nickname + ' is missing path.'));
2403 this.method = (method || errors.push('SwaggerOperation ' + nickname + ' is missing method.'));
2404 this.parameters = parameters ? parameters : [];
2405 this.summary = summary;
2408 this.responseMessages = (responseMessages || []);
2409 this.resource = (resource || errors.push('Resource is required'));
2410 this.consumes = consumes;
2411 this.produces = produces;
2412 this.authorizations = typeof authorizations !== 'undefined' ? authorizations : resource.authorizations;
2413 this.deprecated = deprecated;
2414 this['do'] = __bind(this['do'], this);
2417 if(typeof this.deprecated === 'string') {
2418 switch(this.deprecated.toLowerCase()) {
2419 case 'true': case 'yes': case '1': {
2420 this.deprecated = true;
2423 case 'false': case 'no': case '0': case null: {
2424 this.deprecated = false;
2427 default: this.deprecated = Boolean(this.deprecated);
2431 if (errors.length > 0) {
2432 console.error('SwaggerOperation errors', errors, arguments);
2433 this.resource.api.fail(errors);
2436 this.path = this.path.replace('{format}', 'json');
2437 this.method = this.method.toLowerCase();
2438 this.isGetMethod = this.method === 'get';
2441 this.resourceName = this.resource.name;
2442 if (typeof this.type !== 'undefined' && this.type === 'void')
2445 this.responseClassSignature = this.getSignature(this.type, this.resource.models);
2446 this.responseSampleJSON = this.getSampleJSON(this.type, this.resource.models);
2449 for (i = 0; i < this.parameters.length; i++) {
2450 var param = this.parameters[i];
2451 // might take this away
2452 param.name = param.name || param.type || param.dataType;
2453 // for 1.1 compatibility
2454 type = param.type || param.dataType;
2455 if (type === 'array') {
2456 type = 'array[' + (param.items.$ref ? param.items.$ref : param.items.type) + ']';
2460 if (type && type.toLowerCase() === 'boolean') {
2461 param.allowableValues = {};
2462 param.allowableValues.values = ['true', 'false'];
2464 param.signature = this.getSignature(type, this.resource.models);
2465 param.sampleJSON = this.getSampleJSON(type, this.resource.models);
2467 var enumValue = param['enum'];
2468 if (typeof enumValue !== 'undefined') {
2469 param.isList = true;
2470 param.allowableValues = {};
2471 param.allowableValues.descriptiveValues = [];
2473 for (j = 0; j < enumValue.length; j++) {
2475 if (param.defaultValue) {
2476 param.allowableValues.descriptiveValues.push({
2478 isDefault: (v === param.defaultValue)
2482 param.allowableValues.descriptiveValues.push({
2489 else if (param.allowableValues) {
2490 if (param.allowableValues.valueType === 'RANGE')
2491 param.isRange = true;
2493 param.isList = true;
2494 if (param.allowableValues) {
2495 param.allowableValues.descriptiveValues = [];
2496 if (param.allowableValues.values) {
2497 for (j = 0; j < param.allowableValues.values.length; j++) {
2498 v = param.allowableValues.values[j];
2499 if (param.defaultValue !== null) {
2500 param.allowableValues.descriptiveValues.push({
2502 isDefault: (v === param.defaultValue)
2506 param.allowableValues.descriptiveValues.push({
2515 param.defaultValue = applyParameterMacro(this, param);
2517 var defaultSuccessCallback = this.resource.api.defaultSuccessCallback || null;
2518 var defaultErrorCallback = this.resource.api.defaultErrorCallback || null;
2520 this.resource[this.nickname] = function (args, opts, callback, error) {
2521 var arg1, arg2, arg3, arg4;
2522 if(typeof args === 'function') { // right shift 3
2523 arg1 = {}; arg2 = {}; arg3 = args; arg4 = opts;
2525 else if(typeof args === 'object' && typeof opts === 'function') { // right shift 2
2526 arg1 = args; arg2 = {}; arg3 = opts; arg4 = callback;
2529 arg1 = args; arg2 = opts; arg3 = callback; arg4 = error;
2531 return _this['do'](arg1 || {}, arg2 || {}, arg3 || defaultSuccessCallback, arg4 || defaultErrorCallback);
2534 this.resource[this.nickname].help = function (dontPrint) {
2535 return _this.help(dontPrint);
2537 this.resource[this.nickname].asCurl = function (args) {
2538 return _this.asCurl(args);
2542 SwaggerOperation.prototype.isListType = function (type) {
2543 if (type && type.indexOf('[') >= 0) {
2544 return type.substring(type.indexOf('[') + 1, type.indexOf(']'));
2550 SwaggerOperation.prototype.getSignature = function (type, models) {
2551 var isPrimitive, listType;
2552 listType = this.isListType(type);
2553 isPrimitive = ((typeof listType !== 'undefined') && models[listType]) || (typeof models[type] !== 'undefined') ? false : true;
2557 if (typeof listType !== 'undefined') {
2558 return models[listType].getMockSignature();
2560 return models[type].getMockSignature();
2565 SwaggerOperation.prototype.getSampleJSON = function (type, models) {
2566 var isPrimitive, listType, val;
2567 listType = this.isListType(type);
2568 isPrimitive = ((typeof listType !== 'undefined') && models[listType]) || (typeof models[type] !== 'undefined') ? false : true;
2569 val = isPrimitive ? void 0 : (listType ? models[listType].createJSONSample() : models[type].createJSONSample());
2571 val = listType ? [val] : val;
2572 if (typeof val == 'string')
2574 else if (typeof val === 'object') {
2576 if (val instanceof Array && val.length > 0) {
2580 var xmlString = new XMLSerializer().serializeToString(t);
2581 return this.formatXml(xmlString);
2584 return JSON.stringify(val, null, 2);
2591 SwaggerOperation.prototype['do'] = function (args, opts, callback, error) {
2592 var key, param, params, possibleParams = [], req, value;
2594 if (typeof error !== 'function') {
2595 error = function (xhr, textStatus, error) {
2596 return log(xhr, textStatus, error);
2600 if (typeof callback !== 'function') {
2601 callback = function (response) {
2604 if (response !== null) {
2605 content = response.data;
2607 content = 'no data';
2609 return log('default callback: ' + content);
2614 params.headers = [];
2616 params.headers = args.headers;
2617 delete args.headers;
2619 // allow override from the opts
2620 if(opts && opts.responseContentType) {
2621 params.headers['Content-Type'] = opts.responseContentType;
2623 if(opts && opts.requestContentType) {
2624 params.headers.Accept = opts.requestContentType;
2627 for (var i = 0; i < this.parameters.length; i++) {
2628 param = this.parameters[i];
2629 if (param.paramType === 'header') {
2630 if (typeof args[param.name] !== 'undefined')
2631 params.headers[param.name] = args[param.name];
2633 else if (param.paramType === 'form' || param.paramType.toLowerCase() === 'file')
2634 possibleParams.push(param);
2635 else if (param.paramType === 'body' && param.name !== 'body' && typeof args[param.name] !== 'undefined') {
2637 throw new Error('Saw two body params in an API listing; expecting a max of one.');
2639 args.body = args[param.name];
2643 if (typeof args.body !== 'undefined') {
2644 params.body = args.body;
2648 if (possibleParams) {
2649 for (key in possibleParams) {
2650 value = possibleParams[key];
2651 if (args[value.name]) {
2652 params[value.name] = args[value.name];
2657 req = new SwaggerRequest(this.method, this.urlify(args), params, opts, callback, error, this);
2665 SwaggerOperation.prototype.pathJson = function () {
2666 return this.path.replace('{format}', 'json');
2669 SwaggerOperation.prototype.pathXml = function () {
2670 return this.path.replace('{format}', 'xml');
2673 SwaggerOperation.prototype.encodePathParam = function (pathParam) {
2674 var encParts, part, parts, _i, _len;
2675 pathParam = pathParam.toString();
2676 if (pathParam.indexOf('/') === -1) {
2677 return encodeURIComponent(pathParam);
2679 parts = pathParam.split('/');
2681 for (_i = 0, _len = parts.length; _i < _len; _i++) {
2683 encParts.push(encodeURIComponent(part));
2685 return encParts.join('/');
2689 SwaggerOperation.prototype.urlify = function (args) {
2690 var i, j, param, url;
2691 // ensure no double slashing...
2692 if(this.resource.basePath.length > 1 && this.resource.basePath.slice(-1) === '/' && this.pathJson().charAt(0) === '/')
2693 url = this.resource.basePath + this.pathJson().substring(1);
2695 url = this.resource.basePath + this.pathJson();
2696 var params = this.parameters;
2697 for (i = 0; i < params.length; i++) {
2699 if (param.paramType === 'path') {
2700 if (typeof args[param.name] !== 'undefined') {
2701 // apply path params and remove from args
2702 var reg = new RegExp('\\{\\s*?' + param.name + '[^\\{\\}\\/]*(?:\\{.*?\\}[^\\{\\}\\/]*)*\\}(?=(\\/?|$))', 'gi');
2703 url = url.replace(reg, this.encodePathParam(args[param.name]));
2704 delete args[param.name];
2707 throw '' + param.name + ' is a required path param.';
2711 var queryParams = '';
2712 for (i = 0; i < params.length; i++) {
2714 if(param.paramType === 'query') {
2715 if (queryParams !== '')
2717 if (Array.isArray(param)) {
2719 for(j = 0; j < param.length; j++) {
2722 output += encodeURIComponent(param[j]);
2724 queryParams += encodeURIComponent(param.name) + '=' + output;
2727 if (typeof args[param.name] !== 'undefined') {
2728 queryParams += encodeURIComponent(param.name) + '=' + encodeURIComponent(args[param.name]);
2731 throw '' + param.name + ' is a required query param.';
2736 if ((queryParams) && queryParams.length > 0)
2737 url += '?' + queryParams;
2741 SwaggerOperation.prototype.supportHeaderParams = function () {
2742 return this.resource.api.supportHeaderParams;
2745 SwaggerOperation.prototype.supportedSubmitMethods = function () {
2746 return this.resource.api.supportedSubmitMethods;
2749 SwaggerOperation.prototype.getQueryParams = function (args) {
2750 return this.getMatchingParams(['query'], args);
2753 SwaggerOperation.prototype.getHeaderParams = function (args) {
2754 return this.getMatchingParams(['header'], args);
2757 SwaggerOperation.prototype.getMatchingParams = function (paramTypes, args) {
2758 var matchingParams = {};
2759 var params = this.parameters;
2760 for (var i = 0; i < params.length; i++) {
2762 if (args && args[param.name])
2763 matchingParams[param.name] = args[param.name];
2765 var headers = this.resource.api.headers;
2767 for (name in headers) {
2768 var value = headers[name];
2769 matchingParams[name] = value;
2771 return matchingParams;
2774 SwaggerOperation.prototype.help = function (dontPrint) {
2775 var msg = this.nickname + ': ' + this.summary;
2776 var params = this.parameters;
2777 for (var i = 0; i < params.length; i++) {
2778 var param = params[i];
2779 msg += '\n* ' + param.name + (param.required ? ' (required)' : '') + " - " + param.description;
2789 SwaggerOperation.prototype.asCurl = function (args) {
2793 var headers = SwaggerRequest.prototype.setHeaders(args, {}, this);
2794 for(i = 0; i < this.parameters.length; i++) {
2795 var param = this.parameters[i];
2796 if(param.paramType && param.paramType === 'header' && args[param.name]) {
2797 headers[param.name] = args[param.name];
2802 for (key in headers) {
2803 results.push('--header "' + key + ': ' + headers[key] + '"');
2805 return 'curl ' + (results.join(' ')) + ' ' + this.urlify(args);
2808 SwaggerOperation.prototype.formatXml = function (xml) {
2809 var contexp, formatted, indent, lastType, lines, ln, pad, reg, transitions, wsexp, _fn, _i, _len;
2810 reg = /(>)(<)(\/*)/g;
2811 wsexp = /[ ]*(.*)[ ]+\n/g;
2812 contexp = /(<.+>)(.+\n)/g;
2813 xml = xml.replace(reg, '$1\n$2$3').replace(wsexp, '$1\n').replace(contexp, '$1\n$2');
2816 lines = xml.split('\n');
2820 'single->single': 0,
2821 'single->closing': -1,
2822 'single->opening': 0,
2824 'closing->single': 0,
2825 'closing->closing': -1,
2826 'closing->opening': 0,
2827 'closing->other': 0,
2828 'opening->single': 1,
2829 'opening->closing': 0,
2830 'opening->opening': 1,
2831 'opening->other': 1,
2833 'other->closing': -1,
2834 'other->opening': 0,
2837 _fn = function (ln) {
2838 var fromTo, j, key, padding, type, types, value;
2840 single: Boolean(ln.match(/<.+\/>/)),
2841 closing: Boolean(ln.match(/<\/.+>/)),
2842 opening: Boolean(ln.match(/<[^!?].*>/))
2844 type = ((function () {
2847 for (key in types) {
2855 type = type === void 0 ? 'other' : type;
2856 fromTo = lastType + '->' + type;
2859 indent += transitions[fromTo];
2860 padding = ((function () {
2861 var _j, _ref5, _results;
2863 for (j = _j = 0, _ref5 = indent; 0 <= _ref5 ? _j < _ref5 : _j > _ref5; j = 0 <= _ref5 ? ++_j : --_j) {
2868 if (fromTo === 'opening->closing') {
2869 formatted = formatted.substr(0, formatted.length - 1) + ln + '\n';
2871 formatted += padding + ln + '\n';
2874 for (_i = 0, _len = lines.length; _i < _len; _i++) {
2881 var SwaggerRequest = function (type, url, params, opts, successCallback, errorCallback, operation, execution) {
2885 this.useJQuery = (typeof operation.resource.useJQuery !== 'undefined' ? operation.resource.useJQuery : null);
2886 this.type = (type || errors.push('SwaggerRequest type is required (get/post/put/delete/patch/options).'));
2887 this.url = (url || errors.push('SwaggerRequest url is required.'));
2888 this.params = params;
2890 this.successCallback = (successCallback || errors.push('SwaggerRequest successCallback is required.'));
2891 this.errorCallback = (errorCallback || errors.push('SwaggerRequest error callback is required.'));
2892 this.operation = (operation || errors.push('SwaggerRequest operation is required.'));
2893 this.execution = execution;
2894 this.headers = (params.headers || {});
2896 if (errors.length > 0) {
2900 this.type = this.type.toUpperCase();
2902 // set request, response content type headers
2903 var headers = this.setHeaders(params, opts, this.operation);
2904 var body = params.body;
2906 // encode the body for form submits
2907 if (headers['Content-Type']) {
2908 var key, value, values = {}, i;
2909 var operationParams = this.operation.parameters;
2910 for (i = 0; i < operationParams.length; i++) {
2911 var param = operationParams[i];
2912 if (param.paramType === 'form')
2913 values[param.name] = param;
2916 if (headers['Content-Type'].indexOf('application/x-www-form-urlencoded') === 0) {
2918 for (key in values) {
2919 value = this.params[key];
2920 if (typeof value !== 'undefined') {
2923 encoded += encodeURIComponent(key) + '=' + encodeURIComponent(value);
2928 else if (headers['Content-Type'].indexOf('multipart/form-data') === 0) {
2929 // encode the body for form submits
2931 var boundary = '----SwaggerFormBoundary' + Date.now();
2932 for (key in values) {
2933 value = this.params[key];
2934 if (typeof value !== 'undefined') {
2935 data += '--' + boundary + '\n';
2936 data += 'Content-Disposition: form-data; name="' + key + '"';
2938 data += value + '\n';
2941 data += '--' + boundary + '--\n';
2942 headers['Content-Type'] = 'multipart/form-data; boundary=' + boundary;
2948 if (!((this.headers) && (this.headers.mock))) {
2954 useJQuery: this.useJQuery,
2956 error: function (response) {
2957 return _this.errorCallback(response, _this.opts.parent);
2959 redirect: function (response) {
2960 return _this.successCallback(response, _this.opts.parent);
2962 307: function (response) {
2963 return _this.successCallback(response, _this.opts.parent);
2965 response: function (response) {
2966 return _this.successCallback(response, _this.opts.parent);
2972 if (this.operation.resource && this.operation.resource.api && this.operation.resource.api.clientAuthorizations) {
2973 // Get the client authorizations from the resource declaration
2974 status = this.operation.resource.api.clientAuthorizations.apply(obj, this.operation.authorizations);
2976 // Get the client authorization from the default authorization declaration
2978 if (typeof window !== 'undefined') {
2983 status = e.authorizations.apply(obj, this.operation.authorizations);
2987 if (status !== false) {
2988 new SwaggerHttp().execute(obj);
2990 obj.canceled = true;
2999 SwaggerRequest.prototype.setHeaders = function (params, opts, operation) {
3001 var accepts = opts.responseContentType || 'application/json';
3002 var consumes = opts.requestContentType || 'application/json';
3004 var allDefinedParams = operation.parameters;
3005 var definedFormParams = [];
3006 var definedFileParams = [];
3007 var body = params.body;
3010 // get params from the operation and set them in definedFileParams, definedFormParams, headers
3012 for (i = 0; i < allDefinedParams.length; i++) {
3013 var param = allDefinedParams[i];
3014 if (param.paramType === 'form')
3015 definedFormParams.push(param);
3016 else if (param.paramType === 'file')
3017 definedFileParams.push(param);
3018 else if (param.paramType === 'header' && this.params.headers) {
3019 var key = param.name;
3020 var headerValue = this.params.headers[param.name];
3021 if (typeof this.params.headers[param.name] !== 'undefined')
3022 headers[key] = headerValue;
3026 // if there's a body, need to set the accepts header via requestContentType
3027 if (body && (this.type === 'POST' || this.type === 'PUT' || this.type === 'PATCH' || this.type === 'DELETE')) {
3028 if (this.opts.requestContentType)
3029 consumes = this.opts.requestContentType;
3031 // if any form params, content type must be set
3032 if (definedFormParams.length > 0) {
3033 if (definedFileParams.length > 0)
3034 consumes = 'multipart/form-data';
3036 consumes = 'application/x-www-form-urlencoded';
3038 else if (this.type === 'DELETE')
3040 else if (this.type != 'DELETE')
3044 if (consumes && this.operation.consumes) {
3045 if (this.operation.consumes.indexOf(consumes) === -1) {
3046 log('server doesn\'t consume ' + consumes + ', try ' + JSON.stringify(this.operation.consumes));
3050 if (this.opts && this.opts.responseContentType) {
3051 accepts = this.opts.responseContentType;
3053 accepts = 'application/json';
3055 if (accepts && operation.produces) {
3056 if (operation.produces.indexOf(accepts) === -1) {
3057 log('server can\'t produce ' + accepts);
3061 if ((consumes && body !== '') || (consumes === 'application/x-www-form-urlencoded'))
3062 headers['Content-Type'] = consumes;
3064 headers.Accept = accepts;
3069 * SwaggerHttp is a wrapper for executing requests
3071 var SwaggerHttp = function() {};
3073 SwaggerHttp.prototype.execute = function(obj, opts) {
3074 if(obj && (typeof obj.useJQuery === 'boolean'))
3075 this.useJQuery = obj.useJQuery;
3077 this.useJQuery = this.isIE8();
3079 if(obj && typeof obj.body === 'object') {
3080 if(obj.body.type && obj.body.type !== 'formData')
3081 obj.body = JSON.stringify(obj.body);
3083 obj.contentType = false;
3084 obj.processData = false;
3085 // delete obj.cache;
3086 delete obj.headers['Content-Type'];
3091 return new JQueryHttpClient(opts).execute(obj);
3093 return new ShredHttpClient(opts).execute(obj);
3096 SwaggerHttp.prototype.isIE8 = function() {
3097 var detectedIE = false;
3098 if (typeof navigator !== 'undefined' && navigator.userAgent) {
3099 nav = navigator.userAgent.toLowerCase();
3100 if (nav.indexOf('msie') !== -1) {
3101 var version = parseInt(nav.split('msie')[1]);
3111 * JQueryHttpClient lets a browser take advantage of JQuery's cross-browser magic.
3112 * NOTE: when jQuery is available it will export both '$' and 'jQuery' to the global space.
3113 * Since we are using closures here we need to alias it for internal use.
3115 var JQueryHttpClient = function(options) {
3118 var jQuery = window.jQuery;
3122 JQueryHttpClient.prototype.execute = function(obj) {
3126 obj.type = obj.method;
3128 delete obj.useJQuery;
3131 obj.beforeSend = function(xhr) {
3135 for (key in obj.headers) {
3136 if (key.toLowerCase() === "content-type") {
3137 results.push(obj.contentType = obj.headers[key]);
3138 } else if (key.toLowerCase() === "accept") {
3139 results.push(obj.accepts = obj.headers[key]);
3141 results.push(xhr.setRequestHeader(key, obj.headers[key]));
3148 obj.data = obj.body;
3150 obj.complete = function(response, textStatus, opts) {
3152 headerArray = response.getAllResponseHeaders().split("\n");
3154 for(var i = 0; i < headerArray.length; i++) {
3155 var toSplit = headerArray[i].trim();
3156 if(toSplit.length === 0)
3158 var separator = toSplit.indexOf(":");
3159 if(separator === -1) {
3160 // Name but no value in the header
3161 headers[toSplit] = null;
3164 var name = toSplit.substring(0, separator).trim(),
3165 value = toSplit.substring(separator + 1).trim();
3166 headers[name] = value;
3171 method: request.method,
3172 status: response.status,
3173 statusText: response.statusText,
3174 data: response.responseText,
3178 var contentType = (headers["content-type"]||headers["Content-Type"]||null);
3180 if(contentType.indexOf("application/json") === 0 || contentType.indexOf("+json") > 0) {
3182 out.obj = response.responseJSON || JSON.parse(out.data) || {};
3184 // do not set out.obj
3185 log("unable to parse JSON content");
3190 if(response.status >= 200 && response.status < 300)
3192 else if(response.status === 0 || (response.status >= 400 && response.status < 599))
3195 return cb.response(out);
3198 jQuery.support.cors = true;
3199 return jQuery.ajax(obj);
3203 * ShredHttpClient is a light-weight, node or browser HTTP client
3205 var ShredHttpClient = function(opts) {
3206 this.opts = (opts||{});
3207 this.isInitialized = false;
3209 var identity, toString;
3211 if (typeof window !== 'undefined') {
3212 this.Shred = require("./shred");
3213 this.content = require("./shred/content");
3216 this.Shred = require("shred");
3217 this.shred = new this.Shred(opts);
3220 ShredHttpClient.prototype.initShred = function () {
3221 this.isInitialized = true;
3222 this.registerProcessors(this.shred);
3225 ShredHttpClient.prototype.registerProcessors = function(shred) {
3226 var identity = function(x) {
3229 var toString = function(x) {
3230 return x.toString();
3233 if (typeof window !== 'undefined') {
3234 this.content.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], {
3239 this.Shred.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], {
3246 ShredHttpClient.prototype.execute = function(obj) {
3247 if(!this.isInitialized)
3250 var cb = obj.on, res;
3251 var transform = function(response) {
3253 headers: response._headers,
3254 url: response.request.url,
3255 method: response.request.method,
3256 status: response.status,
3257 data: response.content.data
3260 var headers = response._headers.normalized || response._headers;
3261 var contentType = (headers["content-type"]||headers["Content-Type"]||null);
3264 if(contentType.indexOf("application/json") === 0 || contentType.indexOf("+json") > 0) {
3265 if(response.content.data && response.content.data !== "")
3267 out.obj = JSON.parse(response.content.data);
3279 // Transform an error into a usable response-like object
3280 var transformError = function (error) {
3282 // Default to a status of 0 - The client will treat this as a generic permissions sort of error
3284 data: error.message || error
3290 if (error.code === 'ENOTFOUND' || error.code === 'ECONNREFUSED') {
3291 // We can tell the client that this should be treated as a missing resource and not as a permissions thing
3299 error: function (response) {
3301 return cb.error(transform(response));
3303 // Catch the Shred error raised when the request errors as it is made (i.e. No Response is coming)
3304 request_error: function (err) {
3306 return cb.error(transformError(err));
3308 response: function (response) {
3310 return cb.response(transform(response));
3317 return this.shred.request(obj);
3321 var e = (typeof window !== 'undefined' ? window : exports);
3323 e.authorizations = authorizations = new SwaggerAuthorizations();
3324 e.ApiKeyAuthorization = ApiKeyAuthorization;
3325 e.PasswordAuthorization = PasswordAuthorization;
3326 e.CookieAuthorization = CookieAuthorization;
3327 e.SwaggerClient = SwaggerClient;
3328 e.SwaggerApi = SwaggerClient;
3329 e.Operation = Operation;
3331 e.addModel = addModel;
3332 e.Resolver = Resolver;