msb protocol synch change
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / main / resources / api-doc / lib / swagger-client.js
1
2 /**
3  * swagger-client - swagger.js is a javascript client for use with swaggering APIs.
4  * @version v2.1.9-M1
5  * @link http://swagger.io
6  * @license apache 2.0
7  */
8 (function(){
9 var ArrayModel = function(definition) {
10   this.name = "arrayModel";
11   this.definition = definition || {};
12   this.properties = [];
13   
14   var requiredFields = definition.enum || [];
15   var innerType = definition.items;
16   if(innerType) {
17     if(innerType.type) {
18       this.type = typeFromJsonSchema(innerType.type, innerType.format);
19     }
20     else {
21       this.ref = innerType.$ref;
22     }
23   }
24   return this;
25 };
26
27 ArrayModel.prototype.createJSONSample = function(modelsToIgnore) {
28   var result;
29   modelsToIgnore = (modelsToIgnore||{});
30   if(this.type) {
31     result = this.type;
32   }
33   else if (this.ref) {
34     var name = simpleRef(this.ref);
35     if(typeof modelsToIgnore[name] === 'undefined') {
36       modelsToIgnore[name] = this;
37       result = models[name].createJSONSample(modelsToIgnore);
38     }
39     else {
40       return name;
41     }
42   }
43   return [ result ];
44 };
45
46 ArrayModel.prototype.getSampleValue = function(modelsToIgnore) {
47   var result;
48   modelsToIgnore = (modelsToIgnore || {});
49   if(this.type) {
50     result = type;
51   }
52   else if (this.ref) {
53     var name = simpleRef(this.ref);
54     result = models[name].getSampleValue(modelsToIgnore);
55   }
56   return [ result ];
57 };
58
59 ArrayModel.prototype.getMockSignature = function(modelsToIgnore) {
60   var propertiesStr = [];
61   var i, prop;
62   for (i = 0; i < this.properties.length; i++) {
63     prop = this.properties[i];
64     propertiesStr.push(prop.toString());
65   }
66
67   var strong = '<span class="strong">';
68   var stronger = '<span class="stronger">';
69   var strongClose = '</span>';
70   var classOpen = strong + 'array' + ' {' + strongClose;
71   var classClose = strong + '}' + strongClose;
72   var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
73
74   if (!modelsToIgnore)
75     modelsToIgnore = {};
76   modelsToIgnore[this.name] = this;
77   for (i = 0; i < this.properties.length; i++) {
78     prop = this.properties[i];
79     var ref = prop.$ref;
80     var model = models[ref];
81     if (model && typeof modelsToIgnore[ref] === 'undefined') {
82       returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore));
83     }
84   }
85   return returnVal;
86 };
87
88
89 /**
90  * SwaggerAuthorizations applys the correct authorization to an operation being executed
91  */
92 var SwaggerAuthorizations = function() {
93   this.authz = {};
94 };
95
96 SwaggerAuthorizations.prototype.add = function(name, auth) {
97   this.authz[name] = auth;
98   return auth;
99 };
100
101 SwaggerAuthorizations.prototype.remove = function(name) {
102   return delete this.authz[name];
103 };
104
105 SwaggerAuthorizations.prototype.apply = function (obj, authorizations) {
106   var status = null;
107   var key, name, value, result;
108
109   // if the "authorizations" key is undefined, or has an empty array, add all keys
110   if (typeof authorizations === 'undefined' || Object.keys(authorizations).length === 0) {
111     for (key in this.authz) {
112       value = this.authz[key];
113       result = value.apply(obj, authorizations);
114       if (result === true)
115         status = true;
116     }
117   }
118   else {
119     // 2.0 support
120     if (Array.isArray(authorizations)) {
121
122       for (var i = 0; i < authorizations.length; i++) {
123         var auth = authorizations[i];
124         for (name in auth) {
125           for (key in this.authz) {
126             if (key == name) {
127               value = this.authz[key];
128               result = value.apply(obj, authorizations);
129               if (result === true)
130                 status = true;
131             }
132           }
133         }
134       }
135     }
136     else {
137       // 1.2 support
138       for (name in authorizations) {
139         for (key in this.authz) {
140           if (key == name) {
141             value = this.authz[key];
142             result = value.apply(obj, authorizations);
143             if (result === true)
144               status = true;
145           }
146         }
147       }
148     }
149   }
150
151   return status;
152 };
153
154 /**
155  * ApiKeyAuthorization allows a query param or header to be injected
156  */
157 var ApiKeyAuthorization = function(name, value, type) {
158   this.name = name;
159   this.value = value;
160   this.type = type;
161 };
162
163 ApiKeyAuthorization.prototype.apply = function(obj, authorizations) {
164   if (this.type === "query") {
165     if (obj.url.indexOf('?') > 0)
166       obj.url = obj.url + "&" + this.name + "=" + this.value;
167     else
168       obj.url = obj.url + "?" + this.name + "=" + this.value;
169     return true;
170   } else if (this.type === "header") {
171     obj.headers[this.name] = this.value;
172     return true;
173   }
174 };
175
176 var CookieAuthorization = function(cookie) {
177   this.cookie = cookie;
178 };
179
180 CookieAuthorization.prototype.apply = function(obj, authorizations) {
181   obj.cookieJar = obj.cookieJar || CookieJar();
182   obj.cookieJar.setCookie(this.cookie);
183   return true;
184 };
185
186 /**
187  * Password Authorization is a basic auth implementation
188  */
189 var PasswordAuthorization = function(name, username, password) {
190   this.name = name;
191   this.username = username;
192   this.password = password;
193   this._btoa = null;
194   if (typeof window !== 'undefined')
195     this._btoa = btoa;
196   else
197     this._btoa = require("btoa");
198 };
199
200 PasswordAuthorization.prototype.apply = function(obj, authorizations) {
201   var base64encoder = this._btoa;
202   obj.headers.Authorization = "Basic " + base64encoder(this.username + ":" + this.password);
203   return true;
204 };
205 var __bind = function(fn, me){
206   return function(){
207     return fn.apply(me, arguments);
208   };
209 };
210
211 fail = function(message) {
212   log(message);
213 };
214
215 log = function(){
216   log.history = log.history || [];
217   log.history.push(arguments);
218   if(this.console){
219     console.log( Array.prototype.slice.call(arguments)[0] );
220   }
221 };
222
223 if (!Array.prototype.indexOf) {
224   Array.prototype.indexOf = function(obj, start) {
225     for (var i = (start || 0), j = this.length; i < j; i++) {
226       if (this[i] === obj) { return i; }
227     }
228     return -1;
229   };
230 }
231
232 /**
233  * allows override of the default value based on the parameter being
234  * supplied
235  **/
236 var applyParameterMacro = function (operation, parameter) {
237   var e = (typeof window !== 'undefined' ? window : exports);
238   if(e.parameterMacro)
239     return e.parameterMacro(operation, parameter);
240   else
241     return parameter.defaultValue;
242 };
243
244 /**
245  * allows overriding the default value of an model property
246  **/
247 var applyModelPropertyMacro = function (model, property) {
248   var e = (typeof window !== 'undefined' ? window : exports);
249   if(e.modelPropertyMacro)
250     return e.modelPropertyMacro(model, property);
251   else
252     return property.defaultValue;
253 };
254
255 /**
256  * PrimitiveModel
257  **/
258 var PrimitiveModel = function(definition) {
259   this.name = "name";
260   this.definition = definition || {};
261   this.properties = [];
262
263   var requiredFields = definition.enum || [];
264   this.type = typeFromJsonSchema(definition.type, definition.format);
265 };
266
267 PrimitiveModel.prototype.createJSONSample = function(modelsToIgnore) {
268   var result = this.type;
269   return result;
270 };
271
272 PrimitiveModel.prototype.getSampleValue = function() {
273   var result = this.type;
274   return null;
275 };
276
277 PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) {
278   var propertiesStr = [];
279   var i, prop;
280   for (i = 0; i < this.properties.length; i++) {
281     prop = this.properties[i];
282     propertiesStr.push(prop.toString());
283   }
284
285   var strong = '<span class="strong">';
286   var stronger = '<span class="stronger">';
287   var strongClose = '</span>';
288   var classOpen = strong + this.name + ' {' + strongClose;
289   var classClose = strong + '}' + strongClose;
290   var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
291
292   if (!modelsToIgnore)
293     modelsToIgnore = {};
294   modelsToIgnore[this.name] = this;
295   for (i = 0; i < this.properties.length; i++) {
296     prop = this.properties[i];
297     var ref = prop.$ref;
298     var model = models[ref];
299     if (model && typeof modelsToIgnore[ref] === 'undefined') {
300       returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore));
301     }
302   }
303   return returnVal;
304 };
305 /** 
306  * Resolves a spec's remote references
307  */
308 var Resolver = function (){};
309
310 Resolver.prototype.resolve = function(spec, callback, scope) {
311   this.scope = (scope || this);
312   var host, name, path, property, propertyName, type;
313   var processedCalls = 0, resolvedRefs = {}, unresolvedRefs = {};
314
315   // store objects for dereferencing
316   var resolutionTable = {};
317
318   // models
319   for(name in spec.definitions) {
320     var model = spec.definitions[name];
321     for(propertyName in model.properties) {
322       property = model.properties[propertyName];
323       this.resolveTo(property, resolutionTable);
324     }
325   }
326   // operations
327   for(name in spec.paths) {
328     var method, operation, responseCode;
329     path = spec.paths[name];
330     for(method in path) {
331       operation = path[method];
332       var i, parameters = operation.parameters;
333       for(i in parameters) {
334         var parameter = parameters[i];
335         if(parameter.in === 'body' && parameter.schema) {
336           this.resolveTo(parameter.schema, resolutionTable);
337         }
338         if(parameter.$ref) {
339           this.resolveInline(spec, parameter, resolutionTable, unresolvedRefs);
340         }
341       }
342       for(responseCode in operation.responses) {
343         var response = operation.responses[responseCode];
344         if(response.schema) {
345           this.resolveTo(response.schema, resolutionTable);
346         }
347       }
348     }
349   }
350   // get hosts
351   var opts = {}, expectedCalls = 0;
352   for(name in resolutionTable) {
353     var parts = name.split('#');
354     if(parts.length == 2) {
355       host = parts[0]; path = parts[1];
356       if(!Array.isArray(opts[host])) {
357         opts[host] = [];
358         expectedCalls += 1;
359       }
360       opts[host].push(path);
361     }
362   }
363
364   for(name in opts) {
365     var self = this, opt = opts[name];
366     host = name;
367
368     var obj = {
369       useJQuery: false,  // TODO
370       url: host,
371       method: "get",
372       headers: {
373         accept: this.scope.swaggerRequestHeaders || 'application/json'
374       },
375       on: {
376         error: function(response) {
377           processedCalls += 1;
378           var i;
379           for(i = 0; i < opt.length; i++) {
380             // fail all of these
381             var resolved = host + '#' + opt[i];
382             unresolvedRefs[resolved] = null;
383           }
384           if(processedCalls === expectedCalls)
385             self.finish(spec, resolutionTable, resolvedRefs, unresolvedRefs, callback);
386         },
387         response: function(response) {
388           var i, j, swagger = response.obj;
389           processedCalls += 1;
390           for(i = 0; i < opt.length; i++) {
391             var location = swagger, path = opt[i], parts = path.split('/');
392             for(j = 0; j < parts.length; j++) {
393               var segment = parts[j];
394               if(typeof location === 'undefined')
395                 break;
396               if(segment.length > 0)
397                 location = location[segment];
398             }
399             var resolved = host + '#' + path, resolvedName = parts[j-1];
400             if(typeof location !== 'undefined') {
401               resolvedRefs[resolved] = {
402                 name: resolvedName,
403                 obj: location
404               };
405             }
406             else unresolvedRefs[resolved] = null;
407           }
408           if(processedCalls === expectedCalls)
409             self.finish(spec, resolutionTable, resolvedRefs, unresolvedRefs, callback);
410         }
411       }
412     };
413     authorizations.apply(obj);
414     new SwaggerHttp().execute(obj);
415   }
416   if(Object.keys(opts).length === 0)
417     callback.call(this.scope, spec, unresolvedRefs);
418 };
419
420 Resolver.prototype.finish = function(spec, resolutionTable, resolvedRefs, unresolvedRefs, callback) {
421   // walk resolution table and replace with resolved refs
422   var ref;
423   for(ref in resolutionTable) {
424     var i, locations = resolutionTable[ref];
425     for(i = 0; i < locations.length; i++) {
426       var resolvedTo = resolvedRefs[locations[i].obj.$ref];
427       if(resolvedTo) {
428         if(!spec.definitions)
429           spec.definitions = {};
430         if(locations[i].resolveAs === '$ref') {
431           spec.definitions[resolvedTo.name] = resolvedTo.obj;
432           locations[i].obj.$ref = '#/definitions/' + resolvedTo.name;
433         }
434         else if (locations[i].resolveAs === 'inline') {
435           var key;
436           var targetObj = locations[i].obj;
437           delete targetObj.$ref;
438           for(key in resolvedTo.obj) {
439             targetObj[key] = resolvedTo.obj[key];
440           }
441         }
442       }
443     }
444   }
445   callback.call(this.scope, spec, unresolvedRefs);
446 };
447
448 /**
449  * immediately in-lines local refs, queues remote refs
450  * for inline resolution
451  */
452 Resolver.prototype.resolveInline = function (spec, property, objs, unresolvedRefs) {
453   var ref = property.$ref;
454   if(ref) {
455     if(ref.indexOf('http') === 0) {
456       if(Array.isArray(objs[ref])) {
457         objs[ref].push({obj: property, resolveAs: 'inline'});
458       }
459       else {
460         objs[ref] = [{obj: property, resolveAs: 'inline'}];
461       }
462     }
463     else if (ref.indexOf('#') === 0) {
464       // local resolve
465       var shortenedRef = ref.substring(1);
466       var i, parts = shortenedRef.split('/'), location = spec;
467       for(i = 0; i < parts.length; i++) {
468         var part = parts[i];
469         if(part.length > 0) {
470           location = location[part];
471         }
472       }
473       if(location) {
474         delete property.$ref;
475         var key;
476         for(key in location) {
477           property[key] = location[key];
478         }
479       }
480       else unresolvedRefs[ref] = null;
481     }
482   }
483   else if(property.type === 'array') {
484     this.resolveTo(property.items, objs);
485   }
486 };
487
488 Resolver.prototype.resolveTo = function (property, objs) {
489   var ref = property.$ref;
490   if(ref) {
491     if(ref.indexOf('http') === 0) {
492       if(Array.isArray(objs[ref])) {
493         objs[ref].push({obj: property, resolveAs: '$ref'});
494       }
495       else {
496         objs[ref] = [{obj: property, resolveAs: '$ref'}];
497       }
498     }
499   }
500   else if(property.type === 'array') {
501     var items = property.items;
502     this.resolveTo(items, objs);
503   }
504 };
505 var addModel = function(name, model) {
506   models[name] = model;
507 };
508
509 var SwaggerClient = function(url, options) {
510   this.isBuilt = false;
511   this.url = null;
512   this.debug = false;
513   this.basePath = null;
514   this.modelsArray = [];
515   this.authorizations = null;
516   this.authorizationScheme = null;
517   this.isValid = false;
518   this.info = null;
519   this.useJQuery = false;
520   this.resourceCount = 0;
521
522   if(typeof url !== 'undefined')
523     return this.initialize(url, options);
524 };
525
526 SwaggerClient.prototype.initialize = function (url, options) {
527   this.models = models = {};
528
529   options = (options||{});
530
531   if(typeof url === 'string')
532     this.url = url;
533   else if(typeof url === 'object') {
534     options = url;
535     this.url = options.url;
536   }
537   this.swaggerRequstHeaders = options.swaggerRequstHeaders || 'application/json;charset=utf-8,*/*';
538   this.defaultSuccessCallback = options.defaultSuccessCallback || null;
539   this.defaultErrorCallback = options.defaultErrorCallback || null;
540
541   if (typeof options.success === 'function')
542     this.success = options.success;
543
544   if (options.useJQuery)
545     this.useJQuery = options.useJQuery;
546
547   if (options.authorizations) {
548     this.clientAuthorizations = options.authorizations;
549   } else {
550     this.clientAuthorizations = authorizations;
551   }
552
553   this.supportedSubmitMethods = options.supportedSubmitMethods || [];
554   this.failure = options.failure || function() {};
555   this.progress = options.progress || function() {};
556   this.spec = options.spec;
557   this.options = options;
558
559   if (typeof options.success === 'function') {
560     this.ready = true;
561     this.build();
562   }
563 };
564
565 SwaggerClient.prototype.build = function(mock) {
566   if (this.isBuilt) return this;
567   var self = this;
568   this.progress('fetching resource list: ' + this.url);
569   var obj = {
570     useJQuery: this.useJQuery,
571     url: this.url,
572     method: "get",
573     headers: {
574       accept: this.swaggerRequstHeaders
575     },
576     on: {
577       error: function(response) {
578         if (self.url.substring(0, 4) !== 'http')
579           return self.fail('Please specify the protocol for ' + self.url);
580         else if (response.status === 0)
581           return self.fail('Can\'t read from server.  It may not have the appropriate access-control-origin settings.');
582         else if (response.status === 404)
583           return self.fail('Can\'t read swagger JSON from ' + self.url);
584         else
585           return self.fail(response.status + ' : ' + response.statusText + ' ' + self.url);
586       },
587       response: function(resp) {
588         var responseObj = resp.obj || JSON.parse(resp.data);
589         self.swaggerVersion = responseObj.swaggerVersion;
590
591         if(responseObj.swagger && parseInt(responseObj.swagger) === 2) {
592           self.swaggerVersion = responseObj.swagger;
593           new Resolver().resolve(responseObj, self.buildFromSpec, self);
594           self.isValid = true;
595         }
596         else {
597           if (self.swaggerVersion === '1.2') {
598             return self.buildFrom1_2Spec(responseObj);
599           } else {
600             return self.buildFrom1_1Spec(responseObj);
601           }
602         }
603       }
604     }
605   };
606   if(this.spec) {
607     setTimeout(function() {
608       new Resolver().resolve(self.spec, self.buildFromSpec, self);
609    }, 10);
610   }
611   else {
612     authorizations.apply(obj);
613     if(mock)
614       return obj;
615     new SwaggerHttp().execute(obj);
616   }
617   return this;
618 };
619
620 SwaggerClient.prototype.buildFromSpec = function(response) {
621   if(this.isBuilt) return this;
622
623   this.info = response.info || {};
624   this.title = response.title || '';
625   this.host = response.host || '';
626   this.schemes = response.schemes || [];
627   this.basePath = response.basePath || '';
628   this.apis = {};
629   this.apisArray = [];
630   this.consumes = response.consumes;
631   this.produces = response.produces;
632   this.securityDefinitions = response.securityDefinitions;
633
634   // legacy support
635   this.authSchemes = response.securityDefinitions;
636
637   var definedTags = {};
638   if(Array.isArray(response.tags)) {
639     definedTags = {};
640     for(k = 0; k < response.tags.length; k++) {
641       var t = response.tags[k];
642       definedTags[t.name] = t;
643     }
644   }
645
646   var location;
647   if(typeof this.url === 'string') {
648     location = this.parseUri(this.url);
649   }
650
651   if(typeof this.schemes === 'undefined' || this.schemes.length === 0) {
652     this.scheme = location.scheme || 'http';
653   }
654   else {
655     this.scheme = this.schemes[0];
656   }
657
658   if(typeof this.host === 'undefined' || this.host === '') {
659     this.host = location.host;
660     if (location.port) {
661       this.host = this.host + ':' + location.port;
662     }
663   }
664
665   this.definitions = response.definitions;
666   var key;
667   for(key in this.definitions) {
668     var model = new Model(key, this.definitions[key]);
669     if(model) {
670       models[key] = model;
671     }
672   }
673
674   // get paths, create functions for each operationId
675   var path;
676   var operations = [];
677   for(path in response.paths) {
678     if(typeof response.paths[path] === 'object') {
679       var httpMethod;
680       for(httpMethod in response.paths[path]) {
681         if(['delete', 'get', 'head', 'options', 'patch', 'post', 'put'].indexOf(httpMethod) === -1) {
682           continue;
683         }
684         var operation = response.paths[path][httpMethod];
685         var tags = operation.tags;
686         if(typeof tags === 'undefined') {
687           operation.tags = [ 'default' ];
688           tags = operation.tags;
689         }
690         var operationId = this.idFromOp(path, httpMethod, operation);
691         var operationObject = new Operation (
692           this,
693           operation.scheme,
694           operationId,
695           httpMethod,
696           path,
697           operation,
698           this.definitions
699         );
700         // bind this operation's execute command to the api
701         if(tags.length > 0) {
702           var i;
703           for(i = 0; i < tags.length; i++) {
704             var tag = this.tagFromLabel(tags[i]);
705             var operationGroup = this[tag];
706             if(typeof this.apis[tag] === 'undefined')
707               this.apis[tag] = {};
708             if(typeof operationGroup === 'undefined') {
709               this[tag] = [];
710               operationGroup = this[tag];
711               operationGroup.operations = {};
712               operationGroup.label = tag;
713               operationGroup.apis = [];
714               var tagObject = definedTags[tag];
715               if(typeof tagObject === 'object') {
716                 operationGroup.description = tagObject.description;
717                 operationGroup.externalDocs = tagObject.externalDocs;
718               }
719               this[tag].help = this.help.bind(operationGroup);
720               this.apisArray.push(new OperationGroup(tag, operationGroup.description, operationGroup.externalDocs, operationObject));
721             }
722             if(typeof this.apis[tag].help !== 'function')
723               this.apis[tag].help = this.help.bind(operationGroup);
724             // bind to the apis object
725             this.apis[tag][operationId] = operationObject.execute.bind(operationObject);
726             this.apis[tag][operationId].help = operationObject.help.bind(operationObject);
727             this.apis[tag][operationId].asCurl = operationObject.asCurl.bind(operationObject);
728             operationGroup[operationId] = operationObject.execute.bind(operationObject);
729             operationGroup[operationId].help = operationObject.help.bind(operationObject);
730             operationGroup[operationId].asCurl = operationObject.asCurl.bind(operationObject);
731
732             operationGroup.apis.push(operationObject);
733             operationGroup.operations[operationId] = operationObject;
734
735             // legacy UI feature
736             var j;
737             var api;
738             for(j = 0; j < this.apisArray.length; j++) {
739               if(this.apisArray[j].tag === tag) {
740                 api = this.apisArray[j];
741               }
742             }
743             if(api) {
744               api.operationsArray.push(operationObject);
745             }
746           }
747         }
748         else {
749           log('no group to bind to');
750         }
751       }
752     }
753   }
754   this.isBuilt = true;
755   if (this.success) {
756     this.isValid = true;
757     this.isBuilt = true;
758     this.success();
759   }
760   return this;
761 };
762
763 SwaggerClient.prototype.parseUri = function(uri) {
764   var urlParseRE = /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/;
765   var parts = urlParseRE.exec(uri);
766   return {
767     scheme: parts[4].replace(':',''),
768     host: parts[11],
769     port: parts[12],
770     path: parts[15]
771   };
772 };
773
774 SwaggerClient.prototype.help = function(dontPrint) {
775   var i;
776   var output = 'operations for the "' + this.label + '" tag';
777   for(i = 0; i < this.apis.length; i++) {
778     var api = this.apis[i];
779     output += '\n  * ' + api.nickname + ': ' + api.operation.summary;
780   }
781   if(dontPrint)
782     return output;
783   else {
784     log(output);
785     return output;
786   }
787 };
788
789 SwaggerClient.prototype.tagFromLabel = function(label) {
790   return label;
791 };
792
793 SwaggerClient.prototype.idFromOp = function(path, httpMethod, op) {
794   var opId = op.operationId || (path.substring(1) + '_' + httpMethod);
795   return opId.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()\+\s]/g,'_');
796 };
797
798 SwaggerClient.prototype.fail = function(message) {
799   this.failure(message);
800   throw message;
801 };
802
803 var OperationGroup = function(tag, description, externalDocs, operation) {
804   this.tag = tag;
805   this.path = tag;
806   this.description = description;
807   this.externalDocs = externalDocs;
808   this.name = tag;
809   this.operation = operation;
810   this.operationsArray = [];
811 };
812
813 var Operation = function(parent, scheme, operationId, httpMethod, path, args, definitions) {
814   var errors = [];
815   parent = parent||{};
816   args = args||{};
817
818   this.operations = {};
819   this.operation = args;
820   this.deprecated = args.deprecated;
821   this.consumes = args.consumes;
822   this.produces = args.produces;
823   this.parent = parent;
824   this.host = parent.host || 'localhost';
825   this.schemes = parent.schemes;
826   this.scheme = scheme || parent.scheme || 'http';
827   // this.basePath = window.location.pathname.split("/api-doc")[0]+parent.basePath || '/';
828    // var url = window.location.search.match(/\?url=(.*)\/.*.json.*$/); 
829    var url;
830   if (window.location.search && window.location.search.length > 1) {
831         url = window.location.search.split("&api=")[1];
832
833
834         // //本地json;
835         // if(url.lastIndexOf("runtime")>0){
836         //   var realUrl = window.location.search.match(/\&api=(.*)$/); 
837         //   url=realUrl[1];
838         // }
839
840
841       } else {
842         url = "/api/microservices/v1";
843       }
844
845
846
847   this.basePath=url;
848   
849   this.nickname = (operationId||errors.push('Operations must have a nickname.'));
850   this.method = (httpMethod||errors.push('Operation ' + operationId + ' is missing method.'));
851   this.path = (path||errors.push('Operation ' + this.nickname + ' is missing path.'));
852   this.parameters = args !== null ? (args.parameters||[]) : {};
853   this.summary = args.summary || '';
854   this.responses = (args.responses||{});
855   this.type = null;
856   this.security = args.security;
857   this.authorizations = args.security;
858   this.description = args.description;
859   this.useJQuery = parent.useJQuery;
860
861   if(typeof this.deprecated === 'string') {
862     switch(this.deprecated.toLowerCase()) {
863       case 'true': case 'yes': case '1': {
864         this.deprecated = true;
865         break;
866       }
867       case 'false': case 'no': case '0': case null: {
868         this.deprecated = false;
869         break;
870       }
871       default: this.deprecated = Boolean(this.deprecated);
872     }
873   }
874
875   var i, model;
876
877   if(definitions) {
878     // add to global models
879     var key;
880     for(key in this.definitions) {
881       model = new Model(key, definitions[key]);
882       if(model) {
883         models[key] = model;
884       }
885     }
886   }
887   for(i = 0; i < this.parameters.length; i++) {
888     var param = this.parameters[i];
889     if(param.type === 'array') {
890       param.isList = true;
891       param.allowMultiple = true;
892     }
893     var innerType = this.getType(param);
894     if(innerType && innerType.toString().toLowerCase() === 'boolean') {
895       param.allowableValues = {};
896       param.isList = true;
897       param['enum'] = ["true", "false"];
898     }
899     if(typeof param['enum'] !== 'undefined') {
900       var id;
901       param.allowableValues = {};
902       param.allowableValues.values = [];
903       param.allowableValues.descriptiveValues = [];
904       for(id = 0; id < param['enum'].length; id++) {
905         var value = param['enum'][id];
906         var isDefault = (value === param.default) ? true : false;
907         param.allowableValues.values.push(value);
908         param.allowableValues.descriptiveValues.push({value : value, isDefault: isDefault});
909       }
910     }
911     if(param.type === 'array') {
912       innerType = [innerType];
913       if(typeof param.allowableValues === 'undefined') {
914         // can't show as a list if no values to select from
915         delete param.isList;
916         delete param.allowMultiple;
917       }
918     }
919     param.signature = this.getModelSignature(innerType, models).toString();
920     param.sampleJSON = this.getModelSampleJSON(innerType, models);
921     param.responseClassSignature = param.signature;
922   }
923
924   var defaultResponseCode, response, responses = this.responses;
925
926   if(responses['200']) {
927     response = responses['200'];
928     defaultResponseCode = '200';
929   }
930   else if(responses['201']) {
931     response = responses['201'];
932     defaultResponseCode = '201';
933   }
934   else if(responses['202']) {
935     response = responses['202'];
936     defaultResponseCode = '202';
937   }
938   else if(responses['203']) {
939     response = responses['203'];
940     defaultResponseCode = '203';
941   }
942   else if(responses['204']) {
943     response = responses['204'];
944     defaultResponseCode = '204';
945   }
946   else if(responses['205']) {
947     response = responses['205'];
948     defaultResponseCode = '205';
949   }
950   else if(responses['206']) {
951     response = responses['206'];
952     defaultResponseCode = '206';
953   }
954   else if(responses['default']) {
955     response = responses['default'];
956     defaultResponseCode = 'default';
957   }
958
959   if(response && response.schema) {
960     var resolvedModel = this.resolveModel(response.schema, definitions);
961     delete responses[defaultResponseCode];
962     if(resolvedModel) {
963       this.successResponse = {};
964       this.successResponse[defaultResponseCode] = resolvedModel;
965     }
966     else {
967       this.successResponse = {};
968       this.successResponse[defaultResponseCode] = response.schema.type;
969     }
970     this.type = response;
971   }
972
973   if (errors.length > 0) {
974     if(this.resource && this.resource.api && this.resource.api.fail)
975       this.resource.api.fail(errors);
976   }
977
978   return this;
979 };
980
981 OperationGroup.prototype.sort = function(sorter) {
982
983 };
984
985 Operation.prototype.getType = function (param) {
986   var type = param.type;
987   var format = param.format;
988   var isArray = false;
989   var str;
990   if(type === 'integer' && format === 'int32')
991     str = 'integer';
992   else if(type === 'integer' && format === 'int64')
993     str = 'long';
994   else if(type === 'integer')
995     str = 'integer';
996   else if(type === 'string') {
997     if(format === 'date-time')
998       str = 'date-time';
999     else if(format === 'date')
1000       str = 'date';
1001     else
1002       str = 'string';
1003   }
1004   else if(type === 'number' && format === 'float')
1005     str = 'float';
1006   else if(type === 'number' && format === 'double')
1007     str = 'double';
1008   else if(type === 'number')
1009     str = 'double';
1010   else if(type === 'boolean')
1011     str = 'boolean';
1012   else if(type === 'array') {
1013     isArray = true;
1014     if(param.items)
1015       str = this.getType(param.items);
1016   }
1017   if(param.$ref)
1018     str = param.$ref;
1019
1020   var schema = param.schema;
1021   if(schema) {
1022     var ref = schema.$ref;
1023     if(ref) {
1024       ref = simpleRef(ref);
1025       if(isArray)
1026         return [ ref ];
1027       else
1028         return ref;
1029     }
1030     else
1031       return this.getType(schema);
1032   }
1033   if(isArray)
1034     return [ str ];
1035   else
1036     return str;
1037 };
1038
1039 Operation.prototype.resolveModel = function (schema, definitions) {
1040   if(typeof schema.$ref !== 'undefined') {
1041     var ref = schema.$ref;
1042     if(ref.indexOf('#/definitions/') === 0)
1043       ref = ref.substring('#/definitions/'.length);
1044     if(definitions[ref]) {
1045       return new Model(ref, definitions[ref]);
1046     }
1047   }
1048   if(schema.type === 'array')
1049     return new ArrayModel(schema);
1050   else
1051     return null;
1052 };
1053
1054 Operation.prototype.help = function(dontPrint) {
1055   var out = this.nickname + ': ' + this.summary + '\n';
1056   for(var i = 0; i < this.parameters.length; i++) {
1057     var param = this.parameters[i];
1058     var typeInfo = param.signature;
1059     out += '\n  * ' + param.name + ' (' + typeInfo + '): ' + param.description;
1060   }
1061   if(typeof dontPrint === 'undefined')
1062     log(out);
1063   return out;
1064 };
1065
1066 Operation.prototype.getModelSignature = function(type, definitions) {
1067   var isPrimitive, listType;
1068
1069   if(type instanceof Array) {
1070     listType = true;
1071     type = type[0];
1072   }
1073   else if(typeof type === 'undefined')
1074     type = 'undefined';
1075
1076   if(type === 'string')
1077     isPrimitive = true;
1078   else
1079     isPrimitive = (listType && definitions[listType]) || (definitions[type]) ? false : true;
1080   if (isPrimitive) {
1081     if(listType)
1082       return 'Array[' + type + ']';
1083     else
1084       return type.toString();
1085   } else {
1086     if (listType)
1087       return 'Array[' + definitions[type].getMockSignature() + ']';
1088     else
1089       return definitions[type].getMockSignature();
1090   }
1091 };
1092
1093 Operation.prototype.supportHeaderParams = function () {
1094   return true;
1095 };
1096
1097 Operation.prototype.supportedSubmitMethods = function () {
1098   return this.parent.supportedSubmitMethods;
1099 };
1100
1101 Operation.prototype.getHeaderParams = function (args) {
1102   var headers = this.setContentTypes(args, {});
1103   for(var i = 0; i < this.parameters.length; i++) {
1104     var param = this.parameters[i];
1105     if(typeof args[param.name] !== 'undefined') {
1106       if (param.in === 'header') {
1107         var value = args[param.name];
1108         if(Array.isArray(value))
1109           value = value.toString();
1110         headers[param.name] = value;
1111       }
1112     }
1113   }
1114   return headers;
1115 };
1116
1117 Operation.prototype.urlify = function (args) {
1118   var formParams = {};
1119   var requestUrl = this.path;
1120
1121   // grab params from the args, build the querystring along the way
1122   var querystring = '';
1123   for(var i = 0; i < this.parameters.length; i++) {
1124     var param = this.parameters[i];
1125     if(typeof args[param.name] !== 'undefined') {
1126       if(param.in === 'path') {
1127         var reg = new RegExp('\{' + param.name + '\}', 'gi');
1128         var value = args[param.name];
1129         if(Array.isArray(value))
1130           value = this.encodePathCollection(param.collectionFormat, param.name, value);
1131         else
1132           value = this.encodePathParam(value);
1133         requestUrl = requestUrl.replace(reg, value);
1134       }
1135       else if (param.in === 'query' && typeof args[param.name] !== 'undefined') {
1136         if(querystring === '')
1137           querystring += '?';
1138         else
1139           querystring += '&';
1140         if(typeof param.collectionFormat !== 'undefined') {
1141           var qp = args[param.name];
1142           if(Array.isArray(qp))
1143             querystring += this.encodeQueryCollection(param.collectionFormat, param.name, qp);
1144           else
1145             querystring += this.encodeQueryParam(param.name) + '=' + this.encodeQueryParam(args[param.name]);
1146         }
1147         else
1148           querystring += this.encodeQueryParam(param.name) + '=' + this.encodeQueryParam(args[param.name]);
1149       }
1150       else if (param.in === 'formData')
1151         formParams[param.name] = args[param.name];
1152     }
1153   }
1154   var url = this.scheme + '://' + this.host;
1155
1156   if(this.basePath !== '/')
1157     url += this.basePath;
1158
1159   return url + requestUrl + querystring;
1160 };
1161
1162 Operation.prototype.getMissingParams = function(args) {
1163   var missingParams = [];
1164   // check required params, track the ones that are missing
1165   var i;
1166   for(i = 0; i < this.parameters.length; i++) {
1167     var param = this.parameters[i];
1168     if(param.required === true) {
1169       if(typeof args[param.name] === 'undefined')
1170         missingParams = param.name;
1171     }
1172   }
1173   return missingParams;
1174 };
1175
1176 Operation.prototype.getBody = function(headers, args, opts) {
1177   var formParams = {}, body, key;
1178
1179   for(var i = 0; i < this.parameters.length; i++) {
1180     var param = this.parameters[i];
1181     if(typeof args[param.name] !== 'undefined') {
1182       if (param.in === 'body') {
1183         body = args[param.name];
1184       } else if(param.in === 'formData') {
1185         formParams[param.name] = args[param.name];
1186       }
1187     }
1188   }
1189
1190   // handle form params
1191   if(headers['Content-Type'] === 'application/x-www-form-urlencoded') {
1192     var encoded = "";
1193     for(key in formParams) {
1194       value = formParams[key];
1195       if(typeof value !== 'undefined'){
1196         if(encoded !== "")
1197           encoded += "&";
1198         encoded += encodeURIComponent(key) + '=' + encodeURIComponent(value);
1199       }
1200     }
1201     body = encoded;
1202   }
1203   else if (headers['Content-Type'] && headers['Content-Type'].indexOf('multipart/form-data') >= 0) {
1204     if(opts.useJQuery) {
1205       var bodyParam = new FormData();
1206       bodyParam.type = 'formData';
1207       for (key in formParams) {
1208         value = args[key];
1209         if (typeof value !== 'undefined') {
1210           // required for jquery file upload
1211           if(value.type === 'file' && value.value) {
1212             delete headers['Content-Type'];
1213             bodyParam.append(key, value.value);
1214           }
1215           else
1216             bodyParam.append(key, value);
1217         }
1218       }
1219       body = bodyParam;
1220     }
1221   }
1222
1223   return body;
1224 };
1225
1226 /**
1227  * gets sample response for a single operation
1228  **/
1229 Operation.prototype.getModelSampleJSON = function(type, models) {
1230   var isPrimitive, listType, sampleJson;
1231
1232   listType = (type instanceof Array);
1233   isPrimitive = models[type] ? false : true;
1234   sampleJson = isPrimitive ? void 0 : models[type].createJSONSample();
1235   if (sampleJson) {
1236     sampleJson = listType ? [sampleJson] : sampleJson;
1237     if(typeof sampleJson == 'string')
1238       return sampleJson;
1239     else if(typeof sampleJson === 'object') {
1240       var t = sampleJson;
1241       if(sampleJson instanceof Array && sampleJson.length > 0) {
1242         t = sampleJson[0];
1243       }
1244       if(t.nodeName) {
1245         var xmlString = new XMLSerializer().serializeToString(t);
1246         return this.formatXml(xmlString);
1247       }
1248       else
1249         return JSON.stringify(sampleJson, null, 2);
1250     }
1251     else
1252       return sampleJson;
1253   }
1254 };
1255
1256 /**
1257  * legacy binding
1258  **/
1259 Operation.prototype["do"] = function(args, opts, callback, error, parent) {
1260   return this.execute(args, opts, callback, error, parent);
1261 };
1262
1263
1264 /**
1265  * executes an operation
1266  **/
1267 Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
1268   var args = arg1 || {};
1269   var opts = {}, success, error;
1270   if(typeof arg2 === 'object') {
1271     opts = arg2;
1272     success = arg3;
1273     error = arg4;
1274   }
1275
1276   if(typeof arg2 === 'function') {
1277     success = arg2;
1278     error = arg3;
1279   }
1280
1281   success = (success||log);
1282   error = (error||log);
1283
1284   if(opts.useJQuery)
1285     this.useJQuery = opts.useJQuery;
1286
1287   var missingParams = this.getMissingParams(args);
1288   if(missingParams.length > 0) {
1289     var message = 'missing required params: ' + missingParams;
1290     fail(message);
1291     return;
1292   }
1293   var allHeaders = this.getHeaderParams(args);
1294   var contentTypeHeaders = this.setContentTypes(args, opts);
1295
1296   var headers = {}, attrname;
1297   for (attrname in allHeaders) { headers[attrname] = allHeaders[attrname]; }
1298   for (attrname in contentTypeHeaders) { headers[attrname] = contentTypeHeaders[attrname]; }
1299
1300   var body = this.getBody(headers, args, opts);
1301   var url = this.urlify(args);
1302
1303   var obj = {
1304     url: url,
1305     method: this.method.toUpperCase(),
1306     body: body,
1307     useJQuery: this.useJQuery,
1308     headers: headers,
1309     on: {
1310       response: function(response) {
1311         return success(response, parent);
1312       },
1313       error: function(response) {
1314         return error(response, parent);
1315       }
1316     }
1317   };
1318   var status = authorizations.apply(obj, this.operation.security);
1319   if(opts.mock === true)
1320     return obj;
1321   else
1322     new SwaggerHttp().execute(obj, opts);
1323 };
1324
1325 Operation.prototype.setContentTypes = function(args, opts) {
1326   // default type
1327   var accepts = 'application/json';
1328   var consumes = args.parameterContentType || 'application/json';
1329   var allDefinedParams = this.parameters;
1330   var definedFormParams = [];
1331   var definedFileParams = [];
1332   var body;
1333   var headers = {};
1334
1335   // get params from the operation and set them in definedFileParams, definedFormParams, headers
1336   var i;
1337   for(i = 0; i < allDefinedParams.length; i++) {
1338     var param = allDefinedParams[i];
1339     if(param.in === 'formData') {
1340       if(param.type === 'file')
1341         definedFileParams.push(param);
1342       else
1343         definedFormParams.push(param);
1344     }
1345     else if(param.in === 'header' && opts) {
1346       var key = param.name;
1347       var headerValue = opts[param.name];
1348       if(typeof opts[param.name] !== 'undefined')
1349         headers[key] = headerValue;
1350     }
1351     else if(param.in === 'body' && typeof args[param.name] !== 'undefined') {
1352       body = args[param.name];
1353     }
1354   }
1355
1356   // if there's a body, need to set the consumes header via requestContentType
1357   if (body && (this.method === 'post' || this.method === 'put' || this.method === 'patch' || this.method === 'delete')) {
1358     if (opts.requestContentType)
1359       consumes = opts.requestContentType;
1360   } else {
1361     // if any form params, content type must be set
1362     if(definedFormParams.length > 0) {
1363       if(opts.requestContentType)           // override if set
1364         consumes = opts.requestContentType;
1365       else if(definedFileParams.length > 0) // if a file, must be multipart/form-data
1366         consumes = 'multipart/form-data';
1367       else                                  // default to x-www-from-urlencoded
1368         consumes = 'application/x-www-form-urlencoded';
1369     }
1370     else if (this.type == 'DELETE')
1371       body = '{}';
1372     else if (this.type != 'DELETE')
1373       consumes = null;
1374   }
1375
1376   if (consumes && this.consumes) {
1377     if (this.consumes.indexOf(consumes) === -1) {
1378       log('server doesn\'t consume ' + consumes + ', try ' + JSON.stringify(this.consumes));
1379     }
1380   }
1381
1382   if (opts.responseContentType) {
1383     accepts = opts.responseContentType;
1384   } else {
1385     accepts = 'application/json';
1386   }
1387   if (accepts && this.produces) {
1388     if (this.produces.indexOf(accepts) === -1) {
1389       log('server can\'t produce ' + accepts);
1390     }
1391   }
1392
1393   if ((consumes && body !== '') || (consumes === 'application/x-www-form-urlencoded'))
1394     headers['Content-Type'] = consumes;
1395   if (accepts)
1396     headers.Accept = accepts;
1397   return headers;
1398 };
1399
1400 Operation.prototype.asCurl = function (args) {
1401   var obj = this.execute(args, {mock: true});
1402   authorizations.apply(obj);
1403   var results = [];
1404   results.push('-X ' + this.method.toUpperCase());
1405   if (obj.headers) {
1406     var key;
1407     for (key in obj.headers)
1408       results.push('--header "' + key + ': ' + obj.headers[key] + '"');
1409   }
1410   if(obj.body) {
1411     var body;
1412     if(typeof obj.body === 'object')
1413       body = JSON.stringify(obj.body);
1414     else
1415       body = obj.body;
1416     results.push('-d "' + body.replace(/"/g, '\\"') + '"');
1417   }
1418   return 'curl ' + (results.join(' ')) + ' "' + obj.url + '"';
1419 };
1420
1421 Operation.prototype.encodePathCollection = function(type, name, value) {
1422   var encoded = '';
1423   var i;
1424   var separator = '';
1425   if(type === 'ssv')
1426     separator = '%20';
1427   else if(type === 'tsv')
1428     separator = '\\t';
1429   else if(type === 'pipes')
1430     separator = '|';
1431   else
1432     separator = ',';
1433
1434   for(i = 0; i < value.length; i++) {
1435     if(i === 0)
1436       encoded = this.encodeQueryParam(value[i]);
1437     else
1438       encoded += separator + this.encodeQueryParam(value[i]);
1439   }
1440   return encoded;
1441 };
1442
1443 Operation.prototype.encodeQueryCollection = function(type, name, value) {
1444   var encoded = '';
1445   var i;
1446   if(type === 'default' || type === 'multi') {
1447     for(i = 0; i < value.length; i++) {
1448       if(i > 0) encoded += '&';
1449       encoded += this.encodeQueryParam(name) + '=' + this.encodeQueryParam(value[i]);
1450     }
1451   }
1452   else {
1453     var separator = '';
1454     if(type === 'csv')
1455       separator = ',';
1456     else if(type === 'ssv')
1457       separator = '%20';
1458     else if(type === 'tsv')
1459       separator = '\\t';
1460     else if(type === 'pipes')
1461       separator = '|';
1462     else if(type === 'brackets') {
1463       for(i = 0; i < value.length; i++) {
1464         if(i !== 0)
1465           encoded += '&';
1466         encoded += this.encodeQueryParam(name) + '[]=' + this.encodeQueryParam(value[i]);
1467       }
1468     }
1469     if(separator !== '') {
1470       for(i = 0; i < value.length; i++) {
1471         if(i === 0)
1472           encoded = this.encodeQueryParam(name) + '=' + this.encodeQueryParam(value[i]);
1473         else
1474           encoded += separator + this.encodeQueryParam(value[i]);
1475       }
1476     }
1477   }
1478   return encoded;
1479 };
1480
1481 Operation.prototype.encodeQueryParam = function(arg) {
1482   return encodeURIComponent(arg);
1483 };
1484
1485 /**
1486  * TODO revisit, might not want to leave '/'
1487  **/
1488 Operation.prototype.encodePathParam = function(pathParam) {
1489   var encParts, part, parts, i, len;
1490   pathParam = pathParam.toString();
1491   if (pathParam.indexOf('/') === -1) {
1492     return encodeURIComponent(pathParam);
1493   } else {
1494     parts = pathParam.split('/');
1495     encParts = [];
1496     for (i = 0, len = parts.length; i < len; i++) {
1497       encParts.push(encodeURIComponent(parts[i]));
1498     }
1499     return encParts.join('/');
1500   }
1501 };
1502
1503 var Model = function(name, definition) {
1504   this.name = name;
1505   this.definition = definition || {};
1506   this.properties = [];
1507   var requiredFields = definition.required || [];
1508   if(definition.type === 'array') {
1509     var out = new ArrayModel(definition);
1510     return out;
1511   }
1512   var key;
1513   var props = definition.properties;
1514   if(props) {
1515     for(key in props) {
1516       var required = false;
1517       var property = props[key];
1518       if(requiredFields.indexOf(key) >= 0)
1519         required = true;
1520       this.properties.push(new Property(key, property, required));
1521     }
1522   }
1523 };
1524
1525 Model.prototype.createJSONSample = function(modelsToIgnore) {
1526   var i, result = {}, representations = {};
1527   modelsToIgnore = (modelsToIgnore||{});
1528   modelsToIgnore[this.name] = this;
1529   for (i = 0; i < this.properties.length; i++) {
1530     prop = this.properties[i];
1531     var sample = prop.getSampleValue(modelsToIgnore, representations);
1532     result[prop.name] = sample;
1533   }
1534   delete modelsToIgnore[this.name];
1535   return result;
1536 };
1537
1538 Model.prototype.getSampleValue = function(modelsToIgnore) {
1539   var i, obj = {}, representations = {};
1540   for(i = 0; i < this.properties.length; i++ ) {
1541     var property = this.properties[i];
1542     obj[property.name] = property.sampleValue(false, modelsToIgnore, representations);
1543   }
1544   return obj;
1545 };
1546
1547 Model.prototype.getMockSignature = function(modelsToIgnore) {
1548   var i, prop, propertiesStr = [];
1549   for (i = 0; i < this.properties.length; i++) {
1550     prop = this.properties[i];
1551     propertiesStr.push(prop.toString());
1552   }
1553   var strong = '<span class="strong">';
1554   var stronger = '<span class="stronger">';
1555   var strongClose = '</span>';
1556   var classOpen = strong + this.name + ' {' + strongClose;
1557   var classClose = strong + '}' + strongClose;
1558   var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
1559   if (!modelsToIgnore)
1560     modelsToIgnore = {};
1561
1562   modelsToIgnore[this.name] = this;
1563   for (i = 0; i < this.properties.length; i++) {
1564     prop = this.properties[i];
1565     var ref = prop.$ref;
1566     var model = models[ref];
1567     if (model && typeof modelsToIgnore[model.name] === 'undefined') {
1568       returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore));
1569     }
1570   }
1571   return returnVal;
1572 };
1573
1574 var Property = function(name, obj, required) {
1575   this.schema = obj;
1576   this.required = required;
1577   if(obj.$ref)
1578     this.$ref = simpleRef(obj.$ref);
1579   else if (obj.type === 'array' && obj.items) {
1580     if(obj.items.$ref)
1581       this.$ref = simpleRef(obj.items.$ref);
1582     else
1583       obj = obj.items;
1584   }
1585   this.name = name;
1586   this.description = obj.description;
1587   this.obj = obj;
1588   this.optional = true;
1589   this.optional = !required;
1590   this.default = obj.default || null;
1591   this.example = obj.example !== undefined ? obj.example : null;
1592   this.collectionFormat = obj.collectionFormat || null;
1593   this.maximum = obj.maximum || null;
1594   this.exclusiveMaximum = obj.exclusiveMaximum || null;
1595   this.minimum = obj.minimum || null;
1596   this.exclusiveMinimum = obj.exclusiveMinimum || null;
1597   this.maxLength = obj.maxLength || null;
1598   this.minLength = obj.minLength || null;
1599   this.pattern = obj.pattern || null;
1600   this.maxItems = obj.maxItems || null;
1601   this.minItems = obj.minItems || null;
1602   this.uniqueItems = obj.uniqueItems || null;
1603   this['enum'] = obj['enum'] || null;
1604   this.multipleOf = obj.multipleOf || null;
1605 };
1606
1607 Property.prototype.getSampleValue = function (modelsToIgnore, representations) {
1608   return this.sampleValue(false, modelsToIgnore, representations);
1609 };
1610
1611 Property.prototype.isArray = function () {
1612   var schema = this.schema;
1613   if(schema.type === 'array')
1614     return true;
1615   else
1616     return false;
1617 };
1618
1619 Property.prototype.sampleValue = function(isArray, ignoredModels, representations) {
1620   isArray = (isArray || this.isArray());
1621   ignoredModels = (ignoredModels || {});
1622   // representations = (representations || {});
1623
1624   var type = getStringSignature(this.obj, true);
1625   var output;
1626
1627   if(this.$ref) {
1628     var refModelName = simpleRef(this.$ref);
1629     var refModel = models[refModelName];
1630     if(typeof representations[type] !== 'undefined') {
1631       return representations[type];
1632     }
1633     else
1634
1635     if(refModel && typeof ignoredModels[type] === 'undefined') {
1636       ignoredModels[type] = this;
1637       output = refModel.getSampleValue(ignoredModels, representations);
1638       representations[type] = output;
1639     }
1640     else {
1641       output = (representations[type] || refModelName);
1642     }
1643   }
1644   else if(this.example)
1645     output = this.example;
1646   else if(this.default)
1647     output = this.default;
1648   else if(type === 'date-time')
1649     output = new Date().toISOString();
1650   else if(type === 'date')
1651     output = new Date().toISOString().split("T")[0];
1652   else if(type === 'string')
1653     output = 'string';
1654   else if(type === 'integer')
1655     output = 0;
1656   else if(type === 'long')
1657     output = 0;
1658   else if(type === 'float')
1659     output = 0.0;
1660   else if(type === 'double')
1661     output = 0.0;
1662   else if(type === 'boolean')
1663     output = true;
1664   else
1665     output = {};
1666   ignoredModels[type] = output;
1667   if(isArray)
1668     return [output];
1669   else
1670     return output;
1671 };
1672
1673 getStringSignature = function(obj, baseComponent) {
1674   var str = '';
1675   if(typeof obj.$ref !== 'undefined')
1676     str += simpleRef(obj.$ref);
1677   else if(typeof obj.type === 'undefined')
1678     str += 'object';
1679   else if(obj.type === 'array') {
1680     if(baseComponent)
1681       str += getStringSignature((obj.items || obj.$ref || {}));
1682     else {
1683       str += 'Array[';
1684       str += getStringSignature((obj.items || obj.$ref || {}));
1685       str += ']';
1686     }
1687   }
1688   else if(obj.type === 'integer' && obj.format === 'int32')
1689     str += 'integer';
1690   else if(obj.type === 'integer' && obj.format === 'int64')
1691     str += 'long';
1692   else if(obj.type === 'integer' && typeof obj.format === 'undefined')
1693     str += 'long';
1694   else if(obj.type === 'string' && obj.format === 'date-time')
1695     str += 'date-time';
1696   else if(obj.type === 'string' && obj.format === 'date')
1697     str += 'date';
1698   else if(obj.type === 'string' && typeof obj.format === 'undefined')
1699     str += 'string';
1700   else if(obj.type === 'number' && obj.format === 'float')
1701     str += 'float';
1702   else if(obj.type === 'number' && obj.format === 'double')
1703     str += 'double';
1704   else if(obj.type === 'number' && typeof obj.format === 'undefined')
1705     str += 'double';
1706   else if(obj.type === 'boolean')
1707     str += 'boolean';
1708   else if(obj.$ref)
1709     str += simpleRef(obj.$ref);
1710   else
1711     str += obj.type;
1712   return str;
1713 };
1714
1715 simpleRef = function(name) {
1716   if(typeof name === 'undefined')
1717     return null;
1718   if(name.indexOf("#/definitions/") === 0)
1719     return name.substring('#/definitions/'.length);
1720   else
1721     return name;
1722 };
1723
1724 Property.prototype.toString = function() {
1725   var str = getStringSignature(this.obj);
1726   if(str !== '') {
1727     str = '<span class="propName ' + this.required + '">' + this.name + '</span> (<span class="propType">' + str + '</span>';
1728     if(!this.required)
1729       str += ', <span class="propOptKey">optional</span>';
1730     str += ')';
1731   }
1732   else
1733     str = this.name + ' (' + JSON.stringify(this.obj) + ')';
1734
1735   if(typeof this.description !== 'undefined')
1736     str += ': ' + this.description;
1737
1738   if (this['enum']) {
1739     str += ' = <span class="propVals">[\'' + this['enum'].join('\' or \'') + '\']</span>';
1740   }
1741   if (this.descr) {
1742     str += ': <span class="propDesc">' + this.descr + '</span>';
1743   }
1744
1745
1746   var options = ''; 
1747   var isArray = this.schema.type === 'array';
1748   var type;
1749
1750   if(isArray) {
1751     if(this.schema.items)
1752       type = this.schema.items.type;
1753     else
1754       type = '';
1755   }
1756   else {
1757     type = this.schema.type;
1758   }
1759
1760   if (this.default)
1761     options += optionHtml('Default', this.default);
1762
1763   switch (type) {
1764     case 'string':
1765       if (this.minLength)
1766         options += optionHtml('Min. Length', this.minLength);
1767       if (this.maxLength)
1768         options += optionHtml('Max. Length', this.maxLength);
1769       if (this.pattern)
1770         options += optionHtml('Reg. Exp.', this.pattern);
1771       break;
1772     case 'integer':
1773     case 'number':
1774       if (this.minimum)
1775         options += optionHtml('Min. Value', this.minimum);
1776       if (this.exclusiveMinimum)
1777         options += optionHtml('Exclusive Min.', "true");
1778       if (this.maximum)
1779         options += optionHtml('Max. Value', this.maximum);
1780       if (this.exclusiveMaximum)
1781         options += optionHtml('Exclusive Max.', "true");
1782       if (this.multipleOf)
1783         options += optionHtml('Multiple Of', this.multipleOf);
1784       break;
1785   }
1786
1787   if (isArray) {
1788     if (this.minItems)
1789       options += optionHtml('Min. Items', this.minItems);
1790     if (this.maxItems)
1791       options += optionHtml('Max. Items', this.maxItems);
1792     if (this.uniqueItems)
1793       options += optionHtml('Unique Items', "true");
1794     if (this.collectionFormat)
1795       options += optionHtml('Coll. Format', this.collectionFormat);
1796   }
1797
1798   if (this['enum']) {
1799     var enumString;
1800
1801     if (type === 'number' || type === 'integer')
1802       enumString = this['enum'].join(', ');
1803     else {
1804       enumString = '"' + this['enum'].join('", "') + '"';
1805     }
1806
1807     options += optionHtml('Enum', enumString);
1808   }     
1809
1810   if (options.length > 0)
1811     str = '<span class="propWrap">' + str + '<table class="optionsWrapper"><tr><th colspan="2">' + this.name + '</th></tr>' + options + '</table></span>';
1812   
1813   return str;
1814 };
1815
1816 optionHtml = function(label, value) {
1817   return '<tr><td class="optionName">' + label + ':</td><td>' + value + '</td></tr>';
1818 };
1819
1820 typeFromJsonSchema = function(type, format) {
1821   var str;
1822   if(type === 'integer' && format === 'int32')
1823     str = 'integer';
1824   else if(type === 'integer' && format === 'int64')
1825     str = 'long';
1826   else if(type === 'integer' && typeof format === 'undefined')
1827     str = 'long';
1828   else if(type === 'string' && format === 'date-time')
1829     str = 'date-time';
1830   else if(type === 'string' && format === 'date')
1831     str = 'date';
1832   else if(type === 'number' && format === 'float')
1833     str = 'float';
1834   else if(type === 'number' && format === 'double')
1835     str = 'double';
1836   else if(type === 'number' && typeof format === 'undefined')
1837     str = 'double';
1838   else if(type === 'boolean')
1839     str = 'boolean';
1840   else if(type === 'string')
1841     str = 'string';
1842
1843   return str;
1844 };
1845
1846 var sampleModels = {};
1847 var cookies = {};
1848 var models = {};
1849
1850 SwaggerClient.prototype.buildFrom1_2Spec = function (response) {
1851   if (response.apiVersion !== null) {
1852     this.apiVersion = response.apiVersion;
1853   }
1854   this.apis = {};
1855   this.apisArray = [];
1856   this.consumes = response.consumes;
1857   this.produces = response.produces;
1858   this.authSchemes = response.authorizations;
1859   this.info = this.convertInfo(response.info);
1860
1861   var isApi = false, i, res;
1862   for (i = 0; i < response.apis.length; i++) {
1863     var api = response.apis[i];
1864     if (api.operations) {
1865       var j;
1866       for (j = 0; j < api.operations.length; j++) {
1867         operation = api.operations[j];
1868         isApi = true;
1869       }
1870     }
1871   }
1872   if (response.basePath)
1873     this.basePath = response.basePath;
1874   else if (this.url.indexOf('?') > 0)
1875     this.basePath = this.url.substring(0, this.url.lastIndexOf('?'));
1876   else
1877     this.basePath = this.url;
1878
1879   if (isApi) {
1880     var newName = response.resourcePath.replace(/\//g, '');
1881     this.resourcePath = response.resourcePath;
1882     res = new SwaggerResource(response, this);
1883     this.apis[newName] = res;
1884     this.apisArray.push(res);
1885     this.finish();
1886   } else {
1887     var k;
1888     this.expectedResourceCount = response.apis.length;
1889     for (k = 0; k < response.apis.length; k++) {
1890       var resource = response.apis[k];
1891       res = new SwaggerResource(resource, this);
1892       this.apis[res.name] = res;
1893       this.apisArray.push(res);
1894     }
1895   }
1896   this.isValid = true;
1897   return this;
1898 };
1899
1900 SwaggerClient.prototype.finish = function() {
1901   if (typeof this.success === 'function') {
1902     this.isValid = true;
1903     this.ready = true;
1904     this.isBuilt = true;
1905     this.selfReflect();
1906     this.success();
1907   }  
1908 };
1909
1910 SwaggerClient.prototype.buildFrom1_1Spec = function (response) {
1911   log('This API is using a deprecated version of Swagger!  Please see http://github.com/wordnik/swagger-core/wiki for more info');
1912   if (response.apiVersion !== null)
1913     this.apiVersion = response.apiVersion;
1914   this.apis = {};
1915   this.apisArray = [];
1916   this.produces = response.produces;
1917   this.info = this.convertInfo(response.info);
1918   var isApi = false, res;
1919   for (var i = 0; i < response.apis.length; i++) {
1920     var api = response.apis[i];
1921     if (api.operations) {
1922       for (var j = 0; j < api.operations.length; j++) {
1923         operation = api.operations[j];
1924         isApi = true;
1925       }
1926     }
1927   }
1928   if (response.basePath) {
1929     this.basePath = response.basePath;
1930   } else if (this.url.indexOf('?') > 0) {
1931     this.basePath = this.url.substring(0, this.url.lastIndexOf('?'));
1932   } else {
1933     this.basePath = this.url;
1934   }
1935   if (isApi) {
1936     var newName = response.resourcePath.replace(/\//g, '');
1937     this.resourcePath = response.resourcePath;
1938     res = new SwaggerResource(response, this);
1939     this.apis[newName] = res;
1940     this.apisArray.push(res);
1941     this.finish();
1942   } else {
1943     this.expectedResourceCount = response.apis.length;
1944     for (k = 0; k < response.apis.length; k++) {
1945       resource = response.apis[k];
1946       res = new SwaggerResource(resource, this);
1947       this.apis[res.name] = res;
1948       this.apisArray.push(res);
1949     }
1950   }
1951   this.isValid = true;
1952   return this;
1953 };
1954
1955 SwaggerClient.prototype.convertInfo = function (resp) {
1956   if(typeof resp == 'object') {
1957     var info = {};
1958
1959     info.title = resp.title;
1960     info.description = resp.description;
1961     info.termsOfService = resp.termsOfServiceUrl;
1962     info.contact = {};
1963     info.contact.name = resp.contact;
1964     info.license = {};
1965     info.license.name = resp.license;
1966     info.license.url = resp.licenseUrl;
1967
1968     return info;
1969   }
1970 };
1971
1972 SwaggerClient.prototype.selfReflect = function () {
1973   var resource, tag, ref;
1974   if (this.apis === null) {
1975     return false;
1976   }
1977   ref = this.apis;
1978   for (tag in ref) {
1979     api = ref[tag];
1980     if (api.ready === null) {
1981       return false;
1982     }
1983     this[tag] = api;
1984     this[tag].help = __bind(api.help, api);
1985   }
1986   this.setConsolidatedModels();
1987   this.ready = true;
1988 };
1989
1990 SwaggerClient.prototype.setConsolidatedModels = function () {
1991   var model, modelName, resource, resource_name, i, apis, models, results;
1992   this.models = {};
1993   apis = this.apis;
1994   for (resource_name in apis) {
1995     resource = apis[resource_name];
1996     for (modelName in resource.models) {
1997       if (typeof this.models[modelName] === 'undefined') {
1998         this.models[modelName] = resource.models[modelName];
1999         this.modelsArray.push(resource.models[modelName]);
2000       }
2001     }
2002   }
2003   models = this.modelsArray;
2004   results = [];
2005   for (i = 0; i < models.length; i++) {
2006     model = models[i];
2007     results.push(model.setReferencedModels(this.models));
2008   }
2009   return results;
2010 };
2011
2012 var SwaggerResource = function (resourceObj, api) {
2013   var _this = this;
2014   this.api = api;
2015   this.swaggerRequstHeaders = api.swaggerRequstHeaders;
2016   this.path = (typeof this.api.resourcePath === 'string') ? this.api.resourcePath : resourceObj.path;
2017   this.description = resourceObj.description;
2018   this.authorizations = (resourceObj.authorizations || {});
2019
2020   var parts = this.path.split('/');
2021   this.name = parts[parts.length - 1].replace('.{format}', '');
2022   this.basePath = this.api.basePath;
2023   this.operations = {};
2024   this.operationsArray = [];
2025   this.modelsArray = [];
2026   this.models = api.models || {};
2027   this.rawModels = {};
2028   this.useJQuery = (typeof api.useJQuery !== 'undefined') ? api.useJQuery : null;
2029
2030   if ((resourceObj.apis) && this.api.resourcePath) {
2031     this.addApiDeclaration(resourceObj);
2032   } else {
2033     if (typeof this.path === 'undefined') {
2034       this.api.fail('SwaggerResources must have a path.');
2035     }
2036     if (this.path.substring(0, 4) === 'http') {
2037       this.url = this.path.replace('{format}', 'json');
2038     } else {
2039       this.url = this.api.basePath + this.path.replace('{format}', 'json');
2040     }
2041     this.api.progress('fetching resource ' + this.name + ': ' + this.url);
2042     var obj = {
2043       url: this.url,
2044       method: 'GET',
2045       useJQuery: this.useJQuery,
2046       headers: {
2047         accept: this.swaggerRequstHeaders
2048       },
2049       on: {
2050         response: function (resp) {
2051           var responseObj = resp.obj || JSON.parse(resp.data);
2052           _this.api.resourceCount += 1;
2053           return _this.addApiDeclaration(responseObj);
2054         },
2055         error: function (response) {
2056           _this.api.resourceCount += 1;
2057           return _this.api.fail('Unable to read api \'' +
2058           _this.name + '\' from path ' + _this.url + ' (server returned ' + response.statusText + ')');
2059         }
2060       }
2061     };
2062     var e = typeof window !== 'undefined' ? window : exports;
2063     e.authorizations.apply(obj);
2064     new SwaggerHttp().execute(obj);
2065   }
2066 };
2067
2068 SwaggerResource.prototype.help = function (dontPrint) {
2069   var i;
2070   var output = 'operations for the "' + this.name + '" tag';
2071   for(i = 0; i < this.operationsArray.length; i++) {
2072     var api = this.operationsArray[i];
2073     output += '\n  * ' + api.nickname + ': ' + api.description;
2074   }
2075   if(dontPrint)
2076     return output;
2077   else {
2078     log(output);
2079     return output;
2080   }
2081 };
2082
2083 SwaggerResource.prototype.getAbsoluteBasePath = function (relativeBasePath) {
2084   var pos, url;
2085   url = this.api.basePath;
2086   pos = url.lastIndexOf(relativeBasePath);
2087   var parts = url.split('/');
2088   var rootUrl = parts[0] + '//' + parts[2];
2089
2090   if (relativeBasePath.indexOf('http') === 0)
2091     return relativeBasePath;
2092   if (relativeBasePath === '/')
2093     return rootUrl;
2094   if (relativeBasePath.substring(0, 1) == '/') {
2095     // use root + relative
2096     return rootUrl + relativeBasePath;
2097   }
2098   else {
2099     pos = this.basePath.lastIndexOf('/');
2100     var base = this.basePath.substring(0, pos);
2101     if (base.substring(base.length - 1) == '/')
2102       return base + relativeBasePath;
2103     else
2104       return base + '/' + relativeBasePath;
2105   }
2106 };
2107
2108 SwaggerResource.prototype.addApiDeclaration = function (response) {
2109   if (typeof response.produces === 'string')
2110     this.produces = response.produces;
2111   if (typeof response.consumes === 'string')
2112     this.consumes = response.consumes;
2113   if ((typeof response.basePath === 'string') && response.basePath.replace(/\s/g, '').length > 0)
2114     this.basePath = response.basePath.indexOf('http') === -1 ? this.getAbsoluteBasePath(response.basePath) : response.basePath;
2115   this.resourcePath = response.resourcePath;
2116   this.addModels(response.models);
2117   if (response.apis) {
2118     for (var i = 0 ; i < response.apis.length; i++) {
2119       var endpoint = response.apis[i];
2120       this.addOperations(endpoint.path, endpoint.operations, response.consumes, response.produces);
2121     }
2122   }
2123   this.api[this.name] = this;
2124   this.ready = true;
2125   if(this.api.resourceCount === this.api.expectedResourceCount)
2126     this.api.finish();
2127   return this;
2128 };
2129
2130 SwaggerResource.prototype.addModels = function (models) {
2131   if (typeof models === 'object') {
2132     var modelName;
2133     for (modelName in models) {
2134       if (typeof this.models[modelName] === 'undefined') {
2135         var swaggerModel = new SwaggerModel(modelName, models[modelName]);
2136         this.modelsArray.push(swaggerModel);
2137         this.models[modelName] = swaggerModel;
2138         this.rawModels[modelName] = models[modelName];
2139       }
2140     }
2141     var output = [];
2142     for (var i = 0; i < this.modelsArray.length; i++) {
2143       var model = this.modelsArray[i];
2144       output.push(model.setReferencedModels(this.models));
2145     }
2146     return output;
2147   }
2148 };
2149
2150 SwaggerResource.prototype.addOperations = function (resource_path, ops, consumes, produces) {
2151   if (ops) {
2152     var output = [];
2153     for (var i = 0; i < ops.length; i++) {
2154       var o = ops[i];
2155       consumes = this.consumes;
2156       produces = this.produces;
2157       if (typeof o.consumes !== 'undefined')
2158         consumes = o.consumes;
2159       else
2160         consumes = this.consumes;
2161
2162       if (typeof o.produces !== 'undefined')
2163         produces = o.produces;
2164       else
2165         produces = this.produces;
2166       var type = (o.type || o.responseClass);
2167
2168       if (type === 'array') {
2169         ref = null;
2170         if (o.items)
2171           ref = o.items.type || o.items.$ref;
2172         type = 'array[' + ref + ']';
2173       }
2174       var responseMessages = o.responseMessages;
2175       var method = o.method;
2176       if (o.httpMethod) {
2177         method = o.httpMethod;
2178       }
2179       if (o.supportedContentTypes) {
2180         consumes = o.supportedContentTypes;
2181       }
2182       if (o.errorResponses) {
2183         responseMessages = o.errorResponses;
2184         for (var j = 0; j < responseMessages.length; j++) {
2185           r = responseMessages[j];
2186           r.message = r.reason;
2187           r.reason = null;
2188         }
2189       }
2190       o.nickname = this.sanitize(o.nickname);
2191       var op = new SwaggerOperation(o.nickname,
2192           resource_path,
2193           method,
2194           o.parameters,
2195           o.summary,
2196           o.notes,
2197           type,
2198           responseMessages, 
2199           this, 
2200           consumes, 
2201           produces, 
2202           o.authorizations, 
2203           o.deprecated);
2204
2205       this.operations[op.nickname] = op;
2206       output.push(this.operationsArray.push(op));
2207     }
2208     return output;
2209   }
2210 };
2211
2212 SwaggerResource.prototype.sanitize = function (nickname) {
2213   var op;
2214   op = nickname.replace(/[\s!@#$%^&*()_+=\[{\]};:<>|.\/?,\\'""-]/g, '_');
2215   op = op.replace(/((_){2,})/g, '_');
2216   op = op.replace(/^(_)*/g, '');
2217   op = op.replace(/([_])*$/g, '');
2218   return op;
2219 };
2220
2221 var SwaggerModel = function (modelName, obj) {
2222   this.name = typeof obj.id !== 'undefined' ? obj.id : modelName;
2223   this.properties = [];
2224   var propertyName;
2225   for (propertyName in obj.properties) {
2226     if (obj.required) {
2227       var value;
2228       for (value in obj.required) {
2229         if (propertyName === obj.required[value]) {
2230           obj.properties[propertyName].required = true;
2231         }
2232       }
2233     }
2234     var prop = new SwaggerModelProperty(propertyName, obj.properties[propertyName], this);
2235     this.properties.push(prop);
2236   }
2237 };
2238
2239 SwaggerModel.prototype.setReferencedModels = function (allModels) {
2240   var results = [];
2241   for (var i = 0; i < this.properties.length; i++) {
2242     var property = this.properties[i];
2243     var type = property.type || property.dataType;
2244     if (allModels[type])
2245       results.push(property.refModel = allModels[type]);
2246     else if ((property.refDataType) && (allModels[property.refDataType]))
2247       results.push(property.refModel = allModels[property.refDataType]);
2248     else
2249       results.push(void 0);
2250   }
2251   return results;
2252 };
2253
2254 SwaggerModel.prototype.getMockSignature = function (modelsToIgnore) {
2255   var i, prop, propertiesStr = [];
2256   for (i = 0; i < this.properties.length; i++) {
2257     prop = this.properties[i];
2258     propertiesStr.push(prop.toString());
2259   }
2260
2261   var strong = '<span class="strong">';
2262   var strongClose = '</span>';
2263   var classOpen = strong + this.name + ' {' + strongClose;
2264   var classClose = strong + '}' + strongClose;
2265   var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
2266   if (!modelsToIgnore)
2267     modelsToIgnore = [];
2268   modelsToIgnore.push(this.name);
2269
2270   for (i = 0; i < this.properties.length; i++) {
2271     prop = this.properties[i];
2272     if ((prop.refModel) && modelsToIgnore.indexOf(prop.refModel.name) === -1) {
2273       returnVal = returnVal + ('<br>' + prop.refModel.getMockSignature(modelsToIgnore));
2274     }
2275   }
2276   return returnVal;
2277 };
2278
2279 SwaggerModel.prototype.createJSONSample = function (modelsToIgnore) {
2280   if (sampleModels[this.name]) {
2281     return sampleModels[this.name];
2282   }
2283   else {
2284     var result = {};
2285     modelsToIgnore = (modelsToIgnore || []);
2286     modelsToIgnore.push(this.name);
2287     for (var i = 0; i < this.properties.length; i++) {
2288       var prop = this.properties[i];
2289       result[prop.name] = prop.getSampleValue(modelsToIgnore);
2290     }
2291     modelsToIgnore.pop(this.name);
2292     return result;
2293   }
2294 };
2295
2296 var SwaggerModelProperty = function (name, obj, model) {
2297   this.name = name;
2298   this.dataType = obj.type || obj.dataType || obj.$ref;
2299   this.isCollection = this.dataType && (this.dataType.toLowerCase() === 'array' || this.dataType.toLowerCase() === 'list' || this.dataType.toLowerCase() === 'set');
2300   this.descr = obj.description;
2301   this.required = obj.required;
2302   this.defaultValue = applyModelPropertyMacro(obj, model);
2303   if (obj.items) {
2304     if (obj.items.type) {
2305       this.refDataType = obj.items.type;
2306     }
2307     if (obj.items.$ref) {
2308       this.refDataType = obj.items.$ref;
2309     }
2310   }
2311   this.dataTypeWithRef = this.refDataType ? (this.dataType + '[' + this.refDataType + ']') : this.dataType;
2312   if (obj.allowableValues) {
2313     this.valueType = obj.allowableValues.valueType;
2314     this.values = obj.allowableValues.values;
2315     if (this.values) {
2316       this.valuesString = '\'' + this.values.join('\' or \'') + '\'';
2317     }
2318   }
2319   if (obj['enum']) {
2320     this.valueType = 'string';
2321     this.values = obj['enum'];
2322     if (this.values) {
2323       this.valueString = '\'' + this.values.join('\' or \'') + '\'';
2324     }
2325   }
2326 };
2327
2328 SwaggerModelProperty.prototype.getSampleValue = function (modelsToIgnore) {
2329   var result;
2330   if ((this.refModel) && (modelsToIgnore.indexOf(this.refModel.name) === -1)) {
2331     result = this.refModel.createJSONSample(modelsToIgnore);
2332   } else {
2333     if (this.isCollection) {
2334       result = this.toSampleValue(this.refDataType);
2335     } else {
2336       result = this.toSampleValue(this.dataType);
2337     }
2338   }
2339   if (this.isCollection) {
2340     return [result];
2341   } else {
2342     return result;
2343   }
2344 };
2345
2346 SwaggerModelProperty.prototype.toSampleValue = function (value) {
2347   var result;
2348   if ((typeof this.defaultValue !== 'undefined') && this.defaultValue) {
2349     result = this.defaultValue;
2350   } else if (value === 'integer') {
2351     result = 0;
2352   } else if (value === 'boolean') {
2353     result = false;
2354   } else if (value === 'double' || value === 'number') {
2355     result = 0.0;
2356   } else if (value === 'string') {
2357     result = '';
2358   } else {
2359     result = value;
2360   }
2361   return result;
2362 };
2363
2364 SwaggerModelProperty.prototype.toString = function () {
2365   var req = this.required ? 'propReq' : 'propOpt';
2366   var str = '<span class="propName ' + req + '">' + this.name + '</span> (<span class="propType">' + this.dataTypeWithRef + '</span>';
2367   if (!this.required) {
2368     str += ', <span class="propOptKey">optional</span>';
2369   }
2370   str += ')';
2371   if (this.values) {
2372     str += ' = <span class="propVals">[\'' + this.values.join('\' or \'') + '\']</span>';
2373   }
2374   if (this.descr) {
2375     str += ': <span class="propDesc">' + this.descr + '</span>';
2376   }
2377   return str;
2378 };
2379
2380 var SwaggerOperation = function (nickname, path, method, parameters, summary, notes, type, responseMessages, resource, consumes, produces, authorizations, deprecated) {
2381   var _this = this;
2382
2383   var errors = [];
2384   this.nickname = (nickname || errors.push('SwaggerOperations must have a nickname.'));
2385   this.path = (path || errors.push('SwaggerOperation ' + nickname + ' is missing path.'));
2386   this.method = (method || errors.push('SwaggerOperation ' + nickname + ' is missing method.'));
2387   this.parameters = parameters ? parameters : [];
2388   this.summary = summary;
2389   this.notes = notes;
2390   this.type = type;
2391   this.responseMessages = (responseMessages || []);
2392   this.resource = (resource || errors.push('Resource is required'));
2393   this.consumes = consumes;
2394   this.produces = produces;
2395   this.authorizations = typeof authorizations !== 'undefined' ? authorizations : resource.authorizations;
2396   this.deprecated = deprecated;
2397   this['do'] = __bind(this['do'], this);
2398
2399
2400   if(typeof this.deprecated === 'string') {
2401     switch(this.deprecated.toLowerCase()) {
2402       case 'true': case 'yes': case '1': {
2403         this.deprecated = true;
2404         break;
2405       }
2406       case 'false': case 'no': case '0': case null: {
2407         this.deprecated = false;
2408         break;
2409       }
2410       default: this.deprecated = Boolean(this.deprecated);
2411     }
2412   }
2413
2414   if (errors.length > 0) {
2415     console.error('SwaggerOperation errors', errors, arguments);
2416     this.resource.api.fail(errors);
2417   }
2418
2419   this.path = this.path.replace('{format}', 'json');
2420   this.method = this.method.toLowerCase();
2421   this.isGetMethod = this.method === 'get';
2422
2423   var i, j, v;
2424   this.resourceName = this.resource.name;
2425   if (typeof this.type !== 'undefined' && this.type === 'void')
2426     this.type = null;
2427   else {
2428     this.responseClassSignature = this.getSignature(this.type, this.resource.models);
2429     this.responseSampleJSON = this.getSampleJSON(this.type, this.resource.models);
2430   }
2431
2432   for (i = 0; i < this.parameters.length; i++) {
2433     var param = this.parameters[i];
2434     // might take this away
2435     param.name = param.name || param.type || param.dataType;
2436     // for 1.1 compatibility
2437     type = param.type || param.dataType;
2438     if (type === 'array') {
2439       type = 'array[' + (param.items.$ref ? param.items.$ref : param.items.type) + ']';
2440     }
2441     param.type = type;
2442
2443     if (type && type.toLowerCase() === 'boolean') {
2444       param.allowableValues = {};
2445       param.allowableValues.values = ['true', 'false'];
2446     }
2447     param.signature = this.getSignature(type, this.resource.models);
2448     param.sampleJSON = this.getSampleJSON(type, this.resource.models);
2449
2450     var enumValue = param['enum'];
2451     if (typeof enumValue !== 'undefined') {
2452       param.isList = true;
2453       param.allowableValues = {};
2454       param.allowableValues.descriptiveValues = [];
2455
2456       for (j = 0; j < enumValue.length; j++) {
2457         v = enumValue[j];
2458         if (param.defaultValue) {
2459           param.allowableValues.descriptiveValues.push({
2460             value: String(v),
2461             isDefault: (v === param.defaultValue)
2462           });
2463         }
2464         else {
2465           param.allowableValues.descriptiveValues.push({
2466             value: String(v),
2467             isDefault: false
2468           });
2469         }
2470       }
2471     }
2472     else if (param.allowableValues) {
2473       if (param.allowableValues.valueType === 'RANGE')
2474         param.isRange = true;
2475       else
2476         param.isList = true;
2477       if (param.allowableValues) {
2478         param.allowableValues.descriptiveValues = [];
2479         if (param.allowableValues.values) {
2480           for (j = 0; j < param.allowableValues.values.length; j++) {
2481             v = param.allowableValues.values[j];
2482             if (param.defaultValue !== null) {
2483               param.allowableValues.descriptiveValues.push({
2484                 value: String(v),
2485                 isDefault: (v === param.defaultValue)
2486               });
2487             }
2488             else {
2489               param.allowableValues.descriptiveValues.push({
2490                 value: String(v),
2491                 isDefault: false
2492               });
2493             }
2494           }
2495         }
2496       }
2497     }
2498     param.defaultValue = applyParameterMacro(this, param);
2499   }
2500   var defaultSuccessCallback = this.resource.api.defaultSuccessCallback || null;
2501   var defaultErrorCallback = this.resource.api.defaultErrorCallback || null;
2502
2503   this.resource[this.nickname] = function (args, opts, callback, error) {
2504     var arg1, arg2, arg3, arg4;
2505     if(typeof args === 'function') {  // right shift 3
2506       arg1 = {}; arg2 = {}; arg3 = args; arg4 = opts;
2507     }
2508     else if(typeof args === 'object' && typeof opts === 'function') { // right shift 2
2509       arg1 = args; arg2 = {}; arg3 = opts; arg4 = callback;
2510     }
2511     else {
2512       arg1 = args; arg2 = opts; arg3 = callback; arg4 = error;
2513     }
2514     return _this['do'](arg1 || {}, arg2 || {}, arg3 || defaultSuccessCallback, arg4 || defaultErrorCallback);
2515   };
2516
2517   this.resource[this.nickname].help = function (dontPrint) {
2518     return _this.help(dontPrint);
2519   };
2520   this.resource[this.nickname].asCurl = function (args) {
2521     return _this.asCurl(args);
2522   };
2523 };
2524
2525 SwaggerOperation.prototype.isListType = function (type) {
2526   if (type && type.indexOf('[') >= 0) {
2527     return type.substring(type.indexOf('[') + 1, type.indexOf(']'));
2528   } else {
2529     return void 0;
2530   }
2531 };
2532
2533 SwaggerOperation.prototype.getSignature = function (type, models) {
2534   var isPrimitive, listType;
2535   listType = this.isListType(type);
2536   isPrimitive = ((typeof listType !== 'undefined') && models[listType]) || (typeof models[type] !== 'undefined') ? false : true;
2537   if (isPrimitive) {
2538     return type;
2539   } else {
2540     if (typeof listType !== 'undefined') {
2541       return models[listType].getMockSignature();
2542     } else {
2543       return models[type].getMockSignature();
2544     }
2545   }
2546 };
2547
2548 SwaggerOperation.prototype.getSampleJSON = function (type, models) {
2549   var isPrimitive, listType, val;
2550   listType = this.isListType(type);
2551   isPrimitive = ((typeof listType !== 'undefined') && models[listType]) || (typeof models[type] !== 'undefined') ? false : true;
2552   val = isPrimitive ? void 0 : (listType ? models[listType].createJSONSample() : models[type].createJSONSample());
2553   if (val) {
2554     val = listType ? [val] : val;
2555     if (typeof val == 'string')
2556       return val;
2557     else if (typeof val === 'object') {
2558       var t = val;
2559       if (val instanceof Array && val.length > 0) {
2560         t = val[0];
2561       }
2562       if (t.nodeName) {
2563         var xmlString = new XMLSerializer().serializeToString(t);
2564         return this.formatXml(xmlString);
2565       }
2566       else
2567         return JSON.stringify(val, null, 2);
2568     }
2569     else
2570       return val;
2571   }
2572 };
2573
2574 SwaggerOperation.prototype['do'] = function (args, opts, callback, error) {
2575   var key, param, params, possibleParams = [], req, value;
2576
2577   if (typeof error !== 'function') {
2578     error = function (xhr, textStatus, error) {
2579       return log(xhr, textStatus, error);
2580     };
2581   }
2582
2583   if (typeof callback !== 'function') {
2584     callback = function (response) {
2585       var content;
2586       content = null;
2587       if (response !== null) {
2588         content = response.data;
2589       } else {
2590         content = 'no data';
2591       }
2592       return log('default callback: ' + content);
2593     };
2594   }
2595
2596   params = {};
2597   params.headers = [];
2598   if (args.headers) {
2599     params.headers = args.headers;
2600     delete args.headers;
2601   }
2602   // allow override from the opts
2603   if(opts && opts.responseContentType) {
2604     params.headers['Content-Type'] = opts.responseContentType;
2605   }
2606   if(opts && opts.requestContentType) {
2607     params.headers.Accept = opts.requestContentType;
2608   }
2609
2610   for (var i = 0; i < this.parameters.length; i++) {
2611     param = this.parameters[i];
2612     if (param.paramType === 'header') {
2613       if (typeof args[param.name] !== 'undefined')
2614         params.headers[param.name] = args[param.name];
2615     }
2616     else if (param.paramType === 'form' || param.paramType.toLowerCase() === 'file')
2617       possibleParams.push(param);
2618     else if (param.paramType === 'body' && param.name !== 'body' && typeof args[param.name] !== 'undefined') {
2619       if (args.body) {
2620         throw new Error('Saw two body params in an API listing; expecting a max of one.');
2621       }
2622       args.body = args[param.name];
2623     }
2624   }
2625
2626   if (typeof args.body !== 'undefined') {
2627     params.body = args.body;
2628     delete args.body;
2629   }
2630
2631   if (possibleParams) {
2632     for (key in possibleParams) {
2633       value = possibleParams[key];
2634       if (args[value.name]) {
2635         params[value.name] = args[value.name];
2636       }
2637     }
2638   }
2639
2640   req = new SwaggerRequest(this.method, this.urlify(args), params, opts, callback, error, this);
2641   if (opts.mock) {
2642     return req;
2643   } else {
2644     return true;
2645   }
2646 };
2647
2648 SwaggerOperation.prototype.pathJson = function () {
2649   return this.path.replace('{format}', 'json');
2650 };
2651
2652 SwaggerOperation.prototype.pathXml = function () {
2653   return this.path.replace('{format}', 'xml');
2654 };
2655
2656 SwaggerOperation.prototype.encodePathParam = function (pathParam) {
2657   var encParts, part, parts, _i, _len;
2658   pathParam = pathParam.toString();
2659   if (pathParam.indexOf('/') === -1) {
2660     return encodeURIComponent(pathParam);
2661   } else {
2662     parts = pathParam.split('/');
2663     encParts = [];
2664     for (_i = 0, _len = parts.length; _i < _len; _i++) {
2665       part = parts[_i];
2666       encParts.push(encodeURIComponent(part));
2667     }
2668     return encParts.join('/');
2669   }
2670 };
2671
2672 SwaggerOperation.prototype.urlify = function (args) {
2673   var i, j, param, url;
2674   // ensure no double slashing...
2675   if(this.resource.basePath.length > 1 && this.resource.basePath.slice(-1) === '/' && this.pathJson().charAt(0) === '/')
2676     url = this.resource.basePath + this.pathJson().substring(1);
2677   else
2678     url = this.resource.basePath + this.pathJson();
2679   var params = this.parameters;
2680   for (i = 0; i < params.length; i++) {
2681     param = params[i];
2682     if (param.paramType === 'path') {
2683       if (typeof args[param.name] !== 'undefined') {
2684         // apply path params and remove from args
2685         var reg = new RegExp('\\{\\s*?' + param.name + '[^\\{\\}\\/]*(?:\\{.*?\\}[^\\{\\}\\/]*)*\\}(?=(\\/?|$))', 'gi');
2686         url = url.replace(reg, this.encodePathParam(args[param.name]));
2687         delete args[param.name];
2688       }
2689       else
2690         throw '' + param.name + ' is a required path param.';
2691     }
2692   }
2693
2694   var queryParams = '';
2695   for (i = 0; i < params.length; i++) {
2696     param = params[i];
2697     if(param.paramType === 'query') {
2698       if (queryParams !== '')
2699         queryParams += '&';    
2700       if (Array.isArray(param)) {
2701         var output = '';   
2702         for(j = 0; j < param.length; j++) {    
2703           if(j > 0)    
2704             output += ',';   
2705           output += encodeURIComponent(param[j]);    
2706         }    
2707         queryParams += encodeURIComponent(param.name) + '=' + output;    
2708       }
2709       else {
2710         if (typeof args[param.name] !== 'undefined') {
2711           queryParams += encodeURIComponent(param.name) + '=' + encodeURIComponent(args[param.name]);
2712         } else {
2713           if (param.required)
2714             throw '' + param.name + ' is a required query param.';
2715         }
2716       }
2717     }
2718   }
2719   if ((queryParams) && queryParams.length > 0)
2720     url += '?' + queryParams;
2721   return url;
2722 };
2723
2724 SwaggerOperation.prototype.supportHeaderParams = function () {
2725   return this.resource.api.supportHeaderParams;
2726 };
2727
2728 SwaggerOperation.prototype.supportedSubmitMethods = function () {
2729   return this.resource.api.supportedSubmitMethods;
2730 };
2731
2732 SwaggerOperation.prototype.getQueryParams = function (args) {
2733   return this.getMatchingParams(['query'], args);
2734 };
2735
2736 SwaggerOperation.prototype.getHeaderParams = function (args) {
2737   return this.getMatchingParams(['header'], args);
2738 };
2739
2740 SwaggerOperation.prototype.getMatchingParams = function (paramTypes, args) {
2741   var matchingParams = {};
2742   var params = this.parameters;
2743   for (var i = 0; i < params.length; i++) {
2744     param = params[i];
2745     if (args && args[param.name])
2746       matchingParams[param.name] = args[param.name];
2747   }
2748   var headers = this.resource.api.headers;
2749   var name;
2750   for (name in headers) {
2751     var value = headers[name];
2752     matchingParams[name] = value;
2753   }
2754   return matchingParams;
2755 };
2756
2757 SwaggerOperation.prototype.help = function (dontPrint) {
2758   var msg = this.nickname + ': ' + this.summary;
2759   var params = this.parameters;
2760   for (var i = 0; i < params.length; i++) {
2761     var param = params[i];
2762     msg += '\n* ' + param.name + (param.required ? ' (required)' : '') + " - " + param.description;
2763   }
2764   if(dontPrint)
2765     return msg;
2766   else {
2767     console.log(msg);
2768     return msg;
2769   }
2770 };
2771
2772 SwaggerOperation.prototype.asCurl = function (args) {
2773   var results = [];
2774   var i;
2775
2776   var headers = SwaggerRequest.prototype.setHeaders(args, {}, this);    
2777   for(i = 0; i < this.parameters.length; i++) {
2778     var param = this.parameters[i];
2779     if(param.paramType && param.paramType === 'header' && args[param.name]) {
2780       headers[param.name] = args[param.name];
2781     }
2782   }
2783
2784   var key;
2785   for (key in headers) {
2786     results.push('--header "' + key + ': ' + headers[key] + '"');
2787   }
2788   return 'curl ' + (results.join(' ')) + ' ' + this.urlify(args);
2789 };
2790
2791 SwaggerOperation.prototype.formatXml = function (xml) {
2792   var contexp, formatted, indent, lastType, lines, ln, pad, reg, transitions, wsexp, _fn, _i, _len;
2793   reg = /(>)(<)(\/*)/g;
2794   wsexp = /[ ]*(.*)[ ]+\n/g;
2795   contexp = /(<.+>)(.+\n)/g;
2796   xml = xml.replace(reg, '$1\n$2$3').replace(wsexp, '$1\n').replace(contexp, '$1\n$2');
2797   pad = 0;
2798   formatted = '';
2799   lines = xml.split('\n');
2800   indent = 0;
2801   lastType = 'other';
2802   transitions = {
2803     'single->single': 0,
2804     'single->closing': -1,
2805     'single->opening': 0,
2806     'single->other': 0,
2807     'closing->single': 0,
2808     'closing->closing': -1,
2809     'closing->opening': 0,
2810     'closing->other': 0,
2811     'opening->single': 1,
2812     'opening->closing': 0,
2813     'opening->opening': 1,
2814     'opening->other': 1,
2815     'other->single': 0,
2816     'other->closing': -1,
2817     'other->opening': 0,
2818     'other->other': 0
2819   };
2820   _fn = function (ln) {
2821     var fromTo, j, key, padding, type, types, value;
2822     types = {
2823       single: Boolean(ln.match(/<.+\/>/)),
2824       closing: Boolean(ln.match(/<\/.+>/)),
2825       opening: Boolean(ln.match(/<[^!?].*>/))
2826     };
2827     type = ((function () {
2828       var _results;
2829       _results = [];
2830       for (key in types) {
2831         value = types[key];
2832         if (value) {
2833           _results.push(key);
2834         }
2835       }
2836       return _results;
2837     })())[0];
2838     type = type === void 0 ? 'other' : type;
2839     fromTo = lastType + '->' + type;
2840     lastType = type;
2841     padding = '';
2842     indent += transitions[fromTo];
2843     padding = ((function () {
2844       var _j, _ref5, _results;
2845       _results = [];
2846       for (j = _j = 0, _ref5 = indent; 0 <= _ref5 ? _j < _ref5 : _j > _ref5; j = 0 <= _ref5 ? ++_j : --_j) {
2847         _results.push('  ');
2848       }
2849       return _results;
2850     })()).join('');
2851     if (fromTo === 'opening->closing') {
2852       formatted = formatted.substr(0, formatted.length - 1) + ln + '\n';
2853     } else {
2854       formatted += padding + ln + '\n';
2855     }
2856   };
2857   for (_i = 0, _len = lines.length; _i < _len; _i++) {
2858     ln = lines[_i];
2859     _fn(ln);
2860   }
2861   return formatted;
2862 };
2863
2864 var SwaggerRequest = function (type, url, params, opts, successCallback, errorCallback, operation, execution) {
2865   var _this = this;
2866   var errors = [];
2867
2868   this.useJQuery = (typeof operation.resource.useJQuery !== 'undefined' ? operation.resource.useJQuery : null);
2869   this.type = (type || errors.push('SwaggerRequest type is required (get/post/put/delete/patch/options).'));
2870   this.url = (url || errors.push('SwaggerRequest url is required.'));
2871   this.params = params;
2872   this.opts = opts;
2873   this.successCallback = (successCallback || errors.push('SwaggerRequest successCallback is required.'));
2874   this.errorCallback = (errorCallback || errors.push('SwaggerRequest error callback is required.'));
2875   this.operation = (operation || errors.push('SwaggerRequest operation is required.'));
2876   this.execution = execution;
2877   this.headers = (params.headers || {});
2878
2879   if (errors.length > 0) {
2880     throw errors;
2881   }
2882
2883   this.type = this.type.toUpperCase();
2884
2885   // set request, response content type headers
2886   var headers = this.setHeaders(params, opts, this.operation);
2887   var body = params.body;
2888
2889   // encode the body for form submits
2890   if (headers['Content-Type']) {
2891     var key, value, values = {}, i;
2892     var operationParams = this.operation.parameters;
2893     for (i = 0; i < operationParams.length; i++) {
2894       var param = operationParams[i];
2895       if (param.paramType === 'form')
2896         values[param.name] = param;
2897     }
2898
2899     if (headers['Content-Type'].indexOf('application/x-www-form-urlencoded') === 0) {
2900       var encoded = '';
2901       for (key in values) {
2902         value = this.params[key];
2903         if (typeof value !== 'undefined') {
2904           if (encoded !== '')
2905             encoded += '&';
2906           encoded += encodeURIComponent(key) + '=' + encodeURIComponent(value);
2907         }
2908       }
2909       body = encoded;
2910     }
2911     else if (headers['Content-Type'].indexOf('multipart/form-data') === 0) {
2912       // encode the body for form submits
2913       var data = '';
2914       var boundary = '----SwaggerFormBoundary' + Date.now();
2915       for (key in values) {
2916         value = this.params[key];
2917         if (typeof value !== 'undefined') {
2918           data += '--' + boundary + '\n';
2919           data += 'Content-Disposition: form-data; name="' + key + '"';
2920           data += '\n\n';
2921           data += value + '\n';
2922         }
2923       }
2924       data += '--' + boundary + '--\n';
2925       headers['Content-Type'] = 'multipart/form-data; boundary=' + boundary;
2926       body = data;
2927     }
2928   }
2929
2930   var obj;
2931   if (!((this.headers) && (this.headers.mock))) {
2932     obj = {
2933       url: this.url,
2934       method: this.type,
2935       headers: headers,
2936       body: body,
2937       useJQuery: this.useJQuery,
2938       on: {
2939         error: function (response) {
2940           return _this.errorCallback(response, _this.opts.parent);
2941         },
2942         redirect: function (response) {
2943           return _this.successCallback(response, _this.opts.parent);
2944         },
2945         307: function (response) {
2946           return _this.successCallback(response, _this.opts.parent);
2947         },
2948         response: function (response) {
2949           return _this.successCallback(response, _this.opts.parent);
2950         }
2951       }
2952     };
2953
2954     var status = false;
2955     if (this.operation.resource && this.operation.resource.api && this.operation.resource.api.clientAuthorizations) {
2956       // Get the client authorizations from the resource declaration
2957       status = this.operation.resource.api.clientAuthorizations.apply(obj, this.operation.authorizations);
2958     } else {
2959       // Get the client authorization from the default authorization declaration
2960       var e;
2961       if (typeof window !== 'undefined') {
2962         e = window;
2963       } else {
2964         e = exports;
2965       }
2966       status = e.authorizations.apply(obj, this.operation.authorizations);
2967     }
2968
2969     if (!opts.mock) {
2970       if (status !== false) {
2971         new SwaggerHttp().execute(obj);
2972       } else {
2973         obj.canceled = true;
2974       }
2975     } else {
2976       return obj;
2977     }
2978   }
2979   return obj;
2980 };
2981
2982 SwaggerRequest.prototype.setHeaders = function (params, opts, operation) {
2983   // default type
2984   var accepts = opts.responseContentType || 'application/json';
2985   var consumes = opts.requestContentType || 'application/json';
2986
2987   var allDefinedParams = operation.parameters;
2988   var definedFormParams = [];
2989   var definedFileParams = [];
2990   var body = params.body;
2991   var headers = {};
2992
2993   // get params from the operation and set them in definedFileParams, definedFormParams, headers
2994   var i;
2995   for (i = 0; i < allDefinedParams.length; i++) {
2996     var param = allDefinedParams[i];
2997     if (param.paramType === 'form')
2998       definedFormParams.push(param);
2999     else if (param.paramType === 'file')
3000       definedFileParams.push(param);
3001     else if (param.paramType === 'header' && this.params.headers) {
3002       var key = param.name;
3003       var headerValue = this.params.headers[param.name];
3004       if (typeof this.params.headers[param.name] !== 'undefined')
3005         headers[key] = headerValue;
3006     }
3007   }
3008
3009   // if there's a body, need to set the accepts header via requestContentType
3010   if (body && (this.type === 'POST' || this.type === 'PUT' || this.type === 'PATCH' || this.type === 'DELETE')) {
3011     if (this.opts.requestContentType)
3012       consumes = this.opts.requestContentType;
3013   } else {
3014     // if any form params, content type must be set
3015     if (definedFormParams.length > 0) {
3016       if (definedFileParams.length > 0)
3017         consumes = 'multipart/form-data';
3018       else
3019         consumes = 'application/x-www-form-urlencoded';
3020     }
3021     else if (this.type === 'DELETE')
3022       body = '{}';
3023     else if (this.type != 'DELETE')
3024       consumes = null;
3025   }
3026
3027   if (consumes && this.operation.consumes) {
3028     if (this.operation.consumes.indexOf(consumes) === -1) {
3029       log('server doesn\'t consume ' + consumes + ', try ' + JSON.stringify(this.operation.consumes));
3030     }
3031   }
3032
3033   if (this.opts && this.opts.responseContentType) {
3034     accepts = this.opts.responseContentType;
3035   } else {
3036     accepts = 'application/json';
3037   }
3038   if (accepts && operation.produces) {
3039     if (operation.produces.indexOf(accepts) === -1) {
3040       log('server can\'t produce ' + accepts);
3041     }
3042   }
3043
3044   if ((consumes && body !== '') || (consumes === 'application/x-www-form-urlencoded'))
3045     headers['Content-Type'] = consumes;
3046   if (accepts)
3047     headers.Accept = accepts;
3048   return headers;
3049 };
3050
3051 /**
3052  * SwaggerHttp is a wrapper for executing requests
3053  */
3054 var SwaggerHttp = function() {};
3055
3056 SwaggerHttp.prototype.execute = function(obj, opts) {
3057   if(obj && (typeof obj.useJQuery === 'boolean'))
3058     this.useJQuery = obj.useJQuery;
3059   else
3060     this.useJQuery = this.isIE8();
3061
3062   if(obj && typeof obj.body === 'object') {
3063     if(obj.body.type && obj.body.type !== 'formData')
3064       obj.body = JSON.stringify(obj.body);
3065     else {
3066       obj.contentType = false;
3067       obj.processData = false;
3068       // delete obj.cache;
3069       delete obj.headers['Content-Type'];
3070     }
3071   }
3072
3073   if(this.useJQuery)
3074     return new JQueryHttpClient(opts).execute(obj);
3075   else
3076     return new ShredHttpClient(opts).execute(obj);
3077 };
3078
3079 SwaggerHttp.prototype.isIE8 = function() {
3080   var detectedIE = false;
3081   if (typeof navigator !== 'undefined' && navigator.userAgent) {
3082     nav = navigator.userAgent.toLowerCase();
3083     if (nav.indexOf('msie') !== -1) {
3084       var version = parseInt(nav.split('msie')[1]);
3085       if (version <= 8) {
3086         detectedIE = true;
3087       }
3088     }
3089   }
3090   return detectedIE;
3091 };
3092
3093 /*
3094  * JQueryHttpClient lets a browser take advantage of JQuery's cross-browser magic.
3095  * NOTE: when jQuery is available it will export both '$' and 'jQuery' to the global space.
3096  *       Since we are using closures here we need to alias it for internal use.
3097  */
3098 var JQueryHttpClient = function(options) {
3099   "use strict";
3100   if(!jQuery){
3101     var jQuery = window.jQuery;
3102   }
3103 };
3104
3105 JQueryHttpClient.prototype.execute = function(obj) {
3106   var cb = obj.on;
3107   var request = obj;
3108
3109   obj.type = obj.method;
3110   obj.cache = false;
3111   delete obj.useJQuery;
3112
3113   /*
3114   obj.beforeSend = function(xhr) {
3115     var key, results;
3116     if (obj.headers) {
3117       results = [];
3118       for (key in obj.headers) {
3119         if (key.toLowerCase() === "content-type") {
3120           results.push(obj.contentType = obj.headers[key]);
3121         } else if (key.toLowerCase() === "accept") {
3122           results.push(obj.accepts = obj.headers[key]);
3123         } else {
3124           results.push(xhr.setRequestHeader(key, obj.headers[key]));
3125         }
3126       }
3127       return results;
3128     }
3129   };*/
3130
3131   obj.data = obj.body;
3132   delete obj.body;
3133   obj.complete = function(response, textStatus, opts) {
3134     var headers = {},
3135       headerArray = response.getAllResponseHeaders().split("\n");
3136
3137     for(var i = 0; i < headerArray.length; i++) {
3138       var toSplit = headerArray[i].trim();
3139       if(toSplit.length === 0)
3140         continue;
3141       var separator = toSplit.indexOf(":");
3142       if(separator === -1) {
3143         // Name but no value in the header
3144         headers[toSplit] = null;
3145         continue;
3146       }
3147       var name = toSplit.substring(0, separator).trim(),
3148         value = toSplit.substring(separator + 1).trim();
3149       headers[name] = value;
3150     }
3151
3152     var out = {
3153       url: request.url,
3154       method: request.method,
3155       status: response.status,
3156       statusText: response.statusText,
3157       data: response.responseText,
3158       headers: headers
3159     };
3160
3161     var contentType = (headers["content-type"]||headers["Content-Type"]||null);
3162     if(contentType) {
3163       if(contentType.indexOf("application/json") === 0 || contentType.indexOf("+json") > 0) {
3164         try {
3165           out.obj = response.responseJSON || JSON.parse(out.data) || {};
3166         } catch (ex) {
3167           // do not set out.obj
3168           log("unable to parse JSON content");
3169         }
3170       }
3171     }
3172
3173     if(response.status >= 200 && response.status < 300)
3174       cb.response(out);
3175     else if(response.status === 0 || (response.status >= 400 && response.status < 599))
3176       cb.error(out);
3177     else
3178       return cb.response(out);
3179   };
3180
3181   jQuery.support.cors = true;
3182   return jQuery.ajax(obj);
3183 };
3184
3185 /*
3186  * ShredHttpClient is a light-weight, node or browser HTTP client
3187  */
3188 var ShredHttpClient = function(opts) {
3189   this.opts = (opts||{});
3190   this.isInitialized = false;
3191
3192   var identity, toString;
3193
3194   if (typeof window !== 'undefined') {
3195     this.Shred = require("./shred");
3196     this.content = require("./shred/content");
3197   }
3198   else
3199     this.Shred = require("shred");
3200   this.shred = new this.Shred(opts);
3201 };
3202
3203 ShredHttpClient.prototype.initShred = function () {
3204   this.isInitialized = true;
3205   this.registerProcessors(this.shred);
3206 };
3207
3208 ShredHttpClient.prototype.registerProcessors = function(shred) {
3209   var identity = function(x) {
3210     return x;
3211   };
3212   var toString = function(x) {
3213     return x.toString();
3214   };
3215
3216   if (typeof window !== 'undefined') {
3217     this.content.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], {
3218       parser: identity,
3219       stringify: toString
3220     });
3221   } else {
3222     this.Shred.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], {
3223       parser: identity,
3224       stringify: toString
3225     });
3226   }
3227 };
3228
3229 ShredHttpClient.prototype.execute = function(obj) {
3230   if(!this.isInitialized)
3231     this.initShred();
3232
3233   var cb = obj.on, res;
3234   var transform = function(response) {
3235     var out = {
3236       headers: response._headers,
3237       url: response.request.url,
3238       method: response.request.method,
3239       status: response.status,
3240       data: response.content.data
3241     };
3242
3243     var headers = response._headers.normalized || response._headers;
3244     var contentType = (headers["content-type"]||headers["Content-Type"]||null);
3245
3246     if(contentType) {
3247       if(contentType.indexOf("application/json") === 0 || contentType.indexOf("+json") > 0) {
3248         if(response.content.data && response.content.data !== "")
3249           try{
3250             out.obj = JSON.parse(response.content.data);
3251           }
3252           catch (e) {
3253             // unable to parse
3254           }
3255         else
3256           out.obj = {};
3257       }
3258     }
3259     return out;
3260   };
3261
3262   // Transform an error into a usable response-like object
3263   var transformError = function (error) {
3264     var out = {
3265       // Default to a status of 0 - The client will treat this as a generic permissions sort of error
3266       status: 0,
3267       data: error.message || error
3268     };
3269
3270     if (error.code) {
3271       out.obj = error;
3272
3273       if (error.code === 'ENOTFOUND' || error.code === 'ECONNREFUSED') {
3274         // We can tell the client that this should be treated as a missing resource and not as a permissions thing
3275         out.status = 404;
3276       }
3277     }
3278     return out;
3279   };
3280
3281   res = {
3282     error: function (response) {
3283       if (obj)
3284         return cb.error(transform(response));
3285     },
3286     // Catch the Shred error raised when the request errors as it is made (i.e. No Response is coming)
3287     request_error: function (err) {
3288       if (obj)
3289         return cb.error(transformError(err));
3290     },
3291     response: function (response) {
3292       if (obj) {
3293         return cb.response(transform(response));
3294       }
3295     }
3296   };
3297   if (obj) {
3298     obj.on = res;
3299   }
3300   return this.shred.request(obj);
3301 };
3302
3303
3304 var e = (typeof window !== 'undefined' ? window : exports);
3305
3306 e.authorizations = authorizations = new SwaggerAuthorizations();
3307 e.ApiKeyAuthorization = ApiKeyAuthorization;
3308 e.PasswordAuthorization = PasswordAuthorization;
3309 e.CookieAuthorization = CookieAuthorization;
3310 e.SwaggerClient = SwaggerClient;
3311 e.SwaggerApi = SwaggerClient;
3312 e.Operation = Operation;
3313 e.Model = Model;
3314 e.addModel = addModel;
3315 e.Resolver = Resolver;
3316 })();