swagui 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1569 @@
1
+ // swagger.js
2
+ // version 2.0.31
3
+
4
+ var __bind = function(fn, me){
5
+ return function(){
6
+ return fn.apply(me, arguments);
7
+ };
8
+ };
9
+
10
+ log = function(){
11
+ log.history = log.history || [];
12
+ log.history.push(arguments);
13
+ if(this.console){
14
+ console.log( Array.prototype.slice.call(arguments) );
15
+ }
16
+ };
17
+
18
+ if (!Array.prototype.indexOf) {
19
+ Array.prototype.indexOf = function(obj, start) {
20
+ for (var i = (start || 0), j = this.length; i < j; i++) {
21
+ if (this[i] === obj) { return i; }
22
+ }
23
+ return -1;
24
+ }
25
+ }
26
+
27
+ if (!('filter' in Array.prototype)) {
28
+ Array.prototype.filter= function(filter, that /*opt*/) {
29
+ var other= [], v;
30
+ for (var i=0, n= this.length; i<n; i++)
31
+ if (i in this && filter.call(that, v= this[i], i, this))
32
+ other.push(v);
33
+ return other;
34
+ };
35
+ }
36
+
37
+ if (!('map' in Array.prototype)) {
38
+ Array.prototype.map= function(mapper, that /*opt*/) {
39
+ var other= new Array(this.length);
40
+ for (var i= 0, n= this.length; i<n; i++)
41
+ if (i in this)
42
+ other[i]= mapper.call(that, this[i], i, this);
43
+ return other;
44
+ };
45
+ }
46
+
47
+ Object.keys = Object.keys || (function () {
48
+ var hasOwnProperty = Object.prototype.hasOwnProperty,
49
+ hasDontEnumBug = !{toString:null}.propertyIsEnumerable("toString"),
50
+ DontEnums = [
51
+ 'toString',
52
+ 'toLocaleString',
53
+ 'valueOf',
54
+ 'hasOwnProperty',
55
+ 'isPrototypeOf',
56
+ 'propertyIsEnumerable',
57
+ 'constructor'
58
+ ],
59
+ DontEnumsLength = DontEnums.length;
60
+
61
+ return function (o) {
62
+ if (typeof o != "object" && typeof o != "function" || o === null)
63
+ throw new TypeError("Object.keys called on a non-object");
64
+
65
+ var result = [];
66
+ for (var name in o) {
67
+ if (hasOwnProperty.call(o, name))
68
+ result.push(name);
69
+ }
70
+
71
+ if (hasDontEnumBug) {
72
+ for (var i = 0; i < DontEnumsLength; i++) {
73
+ if (hasOwnProperty.call(o, DontEnums[i]))
74
+ result.push(DontEnums[i]);
75
+ }
76
+ }
77
+
78
+ return result;
79
+ };
80
+ })();
81
+
82
+
83
+ var SwaggerApi = function(url, options) {
84
+ this.url = null;
85
+ this.debug = false;
86
+ this.basePath = null;
87
+ this.authorizations = null;
88
+ this.authorizationScheme = null;
89
+ this.info = null;
90
+ this.useJQuery = false;
91
+
92
+ options = (options||{});
93
+ if (url)
94
+ if (url.url)
95
+ options = url;
96
+ else
97
+ this.url = url;
98
+ else
99
+ options = url;
100
+
101
+ if (options.url != null)
102
+ this.url = options.url;
103
+
104
+ if (options.success != null)
105
+ this.success = options.success;
106
+
107
+ if (typeof options.useJQuery === 'boolean')
108
+ this.useJQuery = options.useJQuery;
109
+
110
+ this.failure = options.failure != null ? options.failure : function() {};
111
+ this.progress = options.progress != null ? options.progress : function() {};
112
+ if (options.success != null)
113
+ this.build();
114
+ }
115
+
116
+ SwaggerApi.prototype.build = function() {
117
+ var _this = this;
118
+ this.progress('fetching resource list: ' + this.url);
119
+ var obj = {
120
+ useJQuery: this.useJQuery,
121
+ url: this.url,
122
+ method: "get",
123
+ headers: {
124
+ accept: "application/json"
125
+ },
126
+ on: {
127
+ error: function(response) {
128
+ if (_this.url.substring(0, 4) !== 'http') {
129
+ return _this.fail('Please specify the protocol for ' + _this.url);
130
+ } else if (response.status === 0) {
131
+ return _this.fail('Can\'t read from server. It may not have the appropriate access-control-origin settings.');
132
+ } else if (response.status === 404) {
133
+ return _this.fail('Can\'t read swagger JSON from ' + _this.url);
134
+ } else {
135
+ return _this.fail(response.status + ' : ' + response.statusText + ' ' + _this.url);
136
+ }
137
+ },
138
+ response: function(resp) {
139
+ var responseObj = resp.obj || JSON.parse(resp.data);
140
+ _this.swaggerVersion = responseObj.swaggerVersion;
141
+ if (_this.swaggerVersion === "1.2") {
142
+ return _this.buildFromSpec(responseObj);
143
+ } else {
144
+ return _this.buildFrom1_1Spec(responseObj);
145
+ }
146
+ }
147
+ }
148
+ };
149
+ var e = (typeof window !== 'undefined' ? window : exports);
150
+ e.authorizations.apply(obj);
151
+ new SwaggerHttp().execute(obj);
152
+ return this;
153
+ };
154
+
155
+ SwaggerApi.prototype.buildFromSpec = function(response) {
156
+ if (response.apiVersion != null) {
157
+ this.apiVersion = response.apiVersion;
158
+ }
159
+ this.apis = {};
160
+ this.apisArray = [];
161
+ this.produces = response.produces;
162
+ this.authSchemes = response.authorizations;
163
+ if (response.info != null) {
164
+ this.info = response.info;
165
+ }
166
+ var isApi = false;
167
+ var i;
168
+ for (i = 0; i < response.apis.length; i++) {
169
+ var api = response.apis[i];
170
+ if (api.operations) {
171
+ var j;
172
+ for (j = 0; j < api.operations.length; j++) {
173
+ operation = api.operations[j];
174
+ isApi = true;
175
+ }
176
+ }
177
+ }
178
+ if (response.basePath) {
179
+ this.basePath = response.basePath;
180
+ } else if (this.url.indexOf('?') > 0) {
181
+ this.basePath = this.url.substring(0, this.url.lastIndexOf('?'));
182
+ } else {
183
+ this.basePath = this.url;
184
+ }
185
+ if (isApi) {
186
+ var newName = response.resourcePath.replace(/\//g, '');
187
+ this.resourcePath = response.resourcePath;
188
+ res = new SwaggerResource(response, this);
189
+ this.apis[newName] = res;
190
+ this.apisArray.push(res);
191
+ } else {
192
+ var k;
193
+ for (k = 0; k < response.apis.length; k++) {
194
+ var resource = response.apis[k];
195
+ res = new SwaggerResource(resource, this);
196
+ this.apis[res.name] = res;
197
+ this.apisArray.push(res);
198
+ }
199
+ }
200
+ if (this.success) {
201
+ this.success();
202
+ }
203
+ return this;
204
+ };
205
+
206
+ SwaggerApi.prototype.buildFrom1_1Spec = function(response) {
207
+ log("This API is using a deprecated version of Swagger! Please see http://github.com/wordnik/swagger-core/wiki for more info");
208
+ if (response.apiVersion != null)
209
+ this.apiVersion = response.apiVersion;
210
+ this.apis = {};
211
+ this.apisArray = [];
212
+ this.produces = response.produces;
213
+ if (response.info != null) {
214
+ this.info = response.info;
215
+ }
216
+ var isApi = false;
217
+ for (var i = 0; i < response.apis.length; i++) {
218
+ var api = response.apis[i];
219
+ if (api.operations) {
220
+ for (var j = 0; j < api.operations.length; j++) {
221
+ operation = api.operations[j];
222
+ isApi = true;
223
+ }
224
+ }
225
+ }
226
+ if (response.basePath) {
227
+ this.basePath = response.basePath;
228
+ } else if (this.url.indexOf('?') > 0) {
229
+ this.basePath = this.url.substring(0, this.url.lastIndexOf('?'));
230
+ } else {
231
+ this.basePath = this.url;
232
+ }
233
+ if (isApi) {
234
+ var newName = response.resourcePath.replace(/\//g, '');
235
+ this.resourcePath = response.resourcePath;
236
+ var res = new SwaggerResource(response, this);
237
+ this.apis[newName] = res;
238
+ this.apisArray.push(res);
239
+ } else {
240
+ for (k = 0; k < response.apis.length; k++) {
241
+ resource = response.apis[k];
242
+ res = new SwaggerResource(resource, this);
243
+ this.apis[res.name] = res;
244
+ this.apisArray.push(res);
245
+ }
246
+ }
247
+ if (this.success) {
248
+ this.success();
249
+ }
250
+ return this;
251
+ };
252
+
253
+ SwaggerApi.prototype.selfReflect = function() {
254
+ var resource, resource_name, _ref;
255
+ if (this.apis == null) {
256
+ return false;
257
+ }
258
+ _ref = this.apis;
259
+ for (resource_name in _ref) {
260
+ resource = _ref[resource_name];
261
+ if (resource.ready == null) {
262
+ return false;
263
+ }
264
+ }
265
+ this.setConsolidatedModels();
266
+ this.ready = true;
267
+ if (this.success != null) {
268
+ return this.success();
269
+ }
270
+ };
271
+
272
+ SwaggerApi.prototype.fail = function(message) {
273
+ this.failure(message);
274
+ throw message;
275
+ };
276
+
277
+ SwaggerApi.prototype.setConsolidatedModels = function() {
278
+ var model, modelName, resource, resource_name, _i, _len, _ref, _ref1, _results;
279
+ this.modelsArray = [];
280
+ this.models = {};
281
+ _ref = this.apis;
282
+ for (resource_name in _ref) {
283
+ resource = _ref[resource_name];
284
+ for (modelName in resource.models) {
285
+ if (this.models[modelName] == null) {
286
+ this.models[modelName] = resource.models[modelName];
287
+ this.modelsArray.push(resource.models[modelName]);
288
+ }
289
+ }
290
+ }
291
+ _ref1 = this.modelsArray;
292
+ _results = [];
293
+ for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
294
+ model = _ref1[_i];
295
+ _results.push(model.setReferencedModels(this.models));
296
+ }
297
+ return _results;
298
+ };
299
+
300
+ SwaggerApi.prototype.help = function() {
301
+ var operation, operation_name, parameter, resource, resource_name, _i, _len, _ref, _ref1, _ref2;
302
+ _ref = this.apis;
303
+ for (resource_name in _ref) {
304
+ resource = _ref[resource_name];
305
+ log(resource_name);
306
+ _ref1 = resource.operations;
307
+ for (operation_name in _ref1) {
308
+ operation = _ref1[operation_name];
309
+ log(" " + operation.nickname);
310
+ _ref2 = operation.parameters;
311
+ for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
312
+ parameter = _ref2[_i];
313
+ log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description);
314
+ }
315
+ }
316
+ }
317
+ return this;
318
+ };
319
+
320
+ var SwaggerResource = function(resourceObj, api) {
321
+ var _this = this;
322
+ this.api = api;
323
+ this.api = this.api;
324
+ produces = [];
325
+ consumes = [];
326
+ this.path = this.api.resourcePath != null ? this.api.resourcePath : resourceObj.path;
327
+ this.description = resourceObj.description;
328
+
329
+ var parts = this.path.split("/");
330
+ this.name = parts[parts.length - 1].replace('.{format}', '');
331
+ this.basePath = this.api.basePath;
332
+ this.operations = {};
333
+ this.operationsArray = [];
334
+ this.modelsArray = [];
335
+ this.models = {};
336
+ this.rawModels = {};
337
+ this.useJQuery = (typeof api.useJQuery !== 'undefined' ? api.useJQuery : null);
338
+
339
+ if ((resourceObj.apis != null) && (this.api.resourcePath != null)) {
340
+ this.addApiDeclaration(resourceObj);
341
+ } else {
342
+ if (this.path == null) {
343
+ this.api.fail("SwaggerResources must have a path.");
344
+ }
345
+ if (this.path.substring(0, 4) === 'http') {
346
+ this.url = this.path.replace('{format}', 'json');
347
+ } else {
348
+ this.url = this.api.basePath + this.path.replace('{format}', 'json');
349
+ }
350
+ this.api.progress('fetching resource ' + this.name + ': ' + this.url);
351
+ obj = {
352
+ url: this.url,
353
+ method: "get",
354
+ useJQuery: this.useJQuery,
355
+ headers: {
356
+ accept: "application/json"
357
+ },
358
+ on: {
359
+ response: function(resp) {
360
+ var responseObj = resp.obj || JSON.parse(resp.data);
361
+ return _this.addApiDeclaration(responseObj);
362
+ },
363
+ error: function(response) {
364
+ return _this.api.fail("Unable to read api '" +
365
+ _this.name + "' from path " + _this.url + " (server returned " + response.statusText + ")");
366
+ }
367
+ }
368
+ };
369
+ var e = typeof window !== 'undefined' ? window : exports;
370
+ e.authorizations.apply(obj);
371
+ new SwaggerHttp().execute(obj);
372
+ }
373
+ }
374
+
375
+ SwaggerResource.prototype.getAbsoluteBasePath = function (relativeBasePath) {
376
+ var pos, url;
377
+ url = this.api.basePath;
378
+ pos = url.lastIndexOf(relativeBasePath);
379
+ var parts = url.split("/");
380
+ var rootUrl = parts[0] + "//" + parts[2];
381
+ //if the relative path is '/' return the root url
382
+ if (relativeBasePath === '/'){
383
+ return rootUrl
384
+ }
385
+ //if the relative path is not in the base path
386
+ else if (pos === -1 ) {
387
+ if (relativeBasePath.indexOf("/") === 0) {
388
+ return url + relativeBasePath;
389
+ } else {
390
+ return url + "/" + relativeBasePath;
391
+ }
392
+ //If the relative path is in the base path
393
+ } else {
394
+ return url.substring(0, pos) + relativeBasePath;
395
+ }
396
+ };
397
+
398
+ SwaggerResource.prototype.addApiDeclaration = function(response) {
399
+ if (response.produces != null)
400
+ this.produces = response.produces;
401
+ if (response.consumes != null)
402
+ this.consumes = response.consumes;
403
+ if ((response.basePath != null) && response.basePath.replace(/\s/g, '').length > 0)
404
+ this.basePath = response.basePath.indexOf("http") === -1 ? this.getAbsoluteBasePath(response.basePath) : response.basePath;
405
+
406
+ this.addModels(response.models);
407
+ if (response.apis) {
408
+ for (var i = 0 ; i < response.apis.length; i++) {
409
+ var endpoint = response.apis[i];
410
+ this.addOperations(endpoint.path, endpoint.operations, response.consumes, response.produces);
411
+ }
412
+ }
413
+ this.api[this.name] = this;
414
+ this.ready = true;
415
+ return this.api.selfReflect();
416
+ };
417
+
418
+ SwaggerResource.prototype.addModels = function(models) {
419
+ if (models != null) {
420
+ var modelName;
421
+ for (modelName in models) {
422
+ if (this.models[modelName] == null) {
423
+ var swaggerModel = new SwaggerModel(modelName, models[modelName]);
424
+ this.modelsArray.push(swaggerModel);
425
+ this.models[modelName] = swaggerModel;
426
+ this.rawModels[modelName] = models[modelName];
427
+ }
428
+ }
429
+ var output = [];
430
+ for (var i = 0; i < this.modelsArray.length; i++) {
431
+ model = this.modelsArray[i];
432
+ output.push(model.setReferencedModels(this.models));
433
+ }
434
+ return output;
435
+ }
436
+ };
437
+
438
+ SwaggerResource.prototype.addOperations = function(resource_path, ops, consumes, produces) {
439
+ if (ops) {
440
+ output = [];
441
+ for (var i = 0; i < ops.length; i++) {
442
+ o = ops[i];
443
+ consumes = this.consumes;
444
+ produces = this.produces;
445
+ if (o.consumes != null)
446
+ consumes = o.consumes;
447
+ else
448
+ consumes = this.consumes;
449
+
450
+ if (o.produces != null)
451
+ produces = o.produces;
452
+ else
453
+ produces = this.produces;
454
+ type = (o.type||o.responseClass);
455
+
456
+ if (type === "array") {
457
+ ref = null;
458
+ if (o.items)
459
+ ref = o.items["type"] || o.items["$ref"];
460
+ type = "array[" + ref + "]";
461
+ }
462
+ responseMessages = o.responseMessages;
463
+ method = o.method;
464
+ if (o.httpMethod) {
465
+ method = o.httpMethod;
466
+ }
467
+ if (o.supportedContentTypes) {
468
+ consumes = o.supportedContentTypes;
469
+ }
470
+ if (o.errorResponses) {
471
+ responseMessages = o.errorResponses;
472
+ for (var j = 0; j < responseMessages.length; j++) {
473
+ r = responseMessages[j];
474
+ r.message = r.reason;
475
+ r.reason = null;
476
+ }
477
+ }
478
+ o.nickname = this.sanitize(o.nickname);
479
+ op = new SwaggerOperation(o.nickname, resource_path, method, o.parameters, o.summary, o.notes, type, responseMessages, this, consumes, produces, o.authorizations);
480
+ this.operations[op.nickname] = op;
481
+ output.push(this.operationsArray.push(op));
482
+ }
483
+ return output;
484
+ }
485
+ };
486
+
487
+ SwaggerResource.prototype.sanitize = function(nickname) {
488
+ var op;
489
+ op = nickname.replace(/[\s!@#$%^&*()_+=\[{\]};:<>|.\/?,\\'""-]/g, '_');
490
+ op = op.replace(/((_){2,})/g, '_');
491
+ op = op.replace(/^(_)*/g, '');
492
+ op = op.replace(/([_])*$/g, '');
493
+ return op;
494
+ };
495
+
496
+ SwaggerResource.prototype.help = function() {
497
+ var op = this.operations;
498
+ var output = [];
499
+ var operation_name;
500
+ for (operation_name in op) {
501
+ operation = op[operation_name];
502
+ var msg = " " + operation.nickname;
503
+ for (var i = 0; i < operation.parameters; i++) {
504
+ parameter = operation.parameters[i];
505
+ msg.concat(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description);
506
+ }
507
+ output.push(msg);
508
+ }
509
+ return output;
510
+ };
511
+
512
+ var SwaggerModel = function(modelName, obj) {
513
+ this.name = obj.id != null ? obj.id : modelName;
514
+ this.properties = [];
515
+ var propertyName;
516
+ for (propertyName in obj.properties) {
517
+ if (obj.required != null) {
518
+ var value;
519
+ for (value in obj.required) {
520
+ if (propertyName === obj.required[value]) {
521
+ obj.properties[propertyName].required = true;
522
+ }
523
+ }
524
+ }
525
+ prop = new SwaggerModelProperty(propertyName, obj.properties[propertyName]);
526
+ this.properties.push(prop);
527
+ }
528
+ }
529
+
530
+ SwaggerModel.prototype.setReferencedModels = function(allModels) {
531
+ var results = [];
532
+ for (var i = 0; i < this.properties.length; i++) {
533
+ var property = this.properties[i];
534
+ var type = property.type || property.dataType;
535
+ if (allModels[type] != null)
536
+ results.push(property.refModel = allModels[type]);
537
+ else if ((property.refDataType != null) && (allModels[property.refDataType] != null))
538
+ results.push(property.refModel = allModels[property.refDataType]);
539
+ else
540
+ results.push(void 0);
541
+ }
542
+ return results;
543
+ };
544
+
545
+ SwaggerModel.prototype.getMockSignature = function(modelsToIgnore) {
546
+ var propertiesStr = [];
547
+ for (var i = 0; i < this.properties.length; i++) {
548
+ prop = this.properties[i];
549
+ propertiesStr.push(prop.toString());
550
+ }
551
+
552
+ var strong = '<span class="strong">';
553
+ var stronger = '<span class="stronger">';
554
+ var strongClose = '</span>';
555
+ var classOpen = strong + this.name + ' {' + strongClose;
556
+ var classClose = strong + '}' + strongClose;
557
+ var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
558
+ if (!modelsToIgnore)
559
+ modelsToIgnore = [];
560
+ modelsToIgnore.push(this.name);
561
+
562
+ for (var i = 0; i < this.properties.length; i++) {
563
+ prop = this.properties[i];
564
+ if ((prop.refModel != null) && modelsToIgnore.indexOf(prop.refModel.name) === -1) {
565
+ returnVal = returnVal + ('<br>' + prop.refModel.getMockSignature(modelsToIgnore));
566
+ }
567
+ }
568
+ return returnVal;
569
+ };
570
+
571
+ SwaggerModel.prototype.createJSONSample = function(modelsToIgnore) {
572
+ if(sampleModels[this.name]) {
573
+ return sampleModels[this.name];
574
+ }
575
+ else {
576
+ var result = {};
577
+ var modelsToIgnore = (modelsToIgnore||[])
578
+ modelsToIgnore.push(this.name);
579
+ for (var i = 0; i < this.properties.length; i++) {
580
+ prop = this.properties[i];
581
+ result[prop.name] = prop.getSampleValue(modelsToIgnore);
582
+ }
583
+ modelsToIgnore.pop(this.name);
584
+ return result;
585
+ }
586
+ };
587
+
588
+ var SwaggerModelProperty = function(name, obj) {
589
+ this.name = name;
590
+ this.dataType = obj.type || obj.dataType || obj["$ref"];
591
+ this.isCollection = this.dataType && (this.dataType.toLowerCase() === 'array' || this.dataType.toLowerCase() === 'list' || this.dataType.toLowerCase() === 'set');
592
+ this.descr = obj.description;
593
+ this.required = obj.required;
594
+ if (obj.items != null) {
595
+ if (obj.items.type != null) {
596
+ this.refDataType = obj.items.type;
597
+ }
598
+ if (obj.items.$ref != null) {
599
+ this.refDataType = obj.items.$ref;
600
+ }
601
+ }
602
+ this.dataTypeWithRef = this.refDataType != null ? (this.dataType + '[' + this.refDataType + ']') : this.dataType;
603
+ if (obj.allowableValues != null) {
604
+ this.valueType = obj.allowableValues.valueType;
605
+ this.values = obj.allowableValues.values;
606
+ if (this.values != null) {
607
+ this.valuesString = "'" + this.values.join("' or '") + "'";
608
+ }
609
+ }
610
+ if (obj["enum"] != null) {
611
+ this.valueType = "string";
612
+ this.values = obj["enum"];
613
+ if (this.values != null) {
614
+ this.valueString = "'" + this.values.join("' or '") + "'";
615
+ }
616
+ }
617
+ }
618
+
619
+ SwaggerModelProperty.prototype.getSampleValue = function(modelsToIgnore) {
620
+ var result;
621
+ if ((this.refModel != null) && (modelsToIgnore.indexOf(prop.refModel.name) === -1)) {
622
+ result = this.refModel.createJSONSample(modelsToIgnore);
623
+ } else {
624
+ if (this.isCollection) {
625
+ result = this.toSampleValue(this.refDataType);
626
+ } else {
627
+ result = this.toSampleValue(this.dataType);
628
+ }
629
+ }
630
+ if (this.isCollection) {
631
+ return [result];
632
+ } else {
633
+ return result;
634
+ }
635
+ };
636
+
637
+ SwaggerModelProperty.prototype.toSampleValue = function(value) {
638
+ var result;
639
+ if (value === "integer") {
640
+ result = 0;
641
+ } else if (value === "boolean") {
642
+ result = false;
643
+ } else if (value === "double" || value === "number") {
644
+ result = 0.0;
645
+ } else if (value === "string") {
646
+ result = "";
647
+ } else {
648
+ result = value;
649
+ }
650
+ return result;
651
+ };
652
+
653
+ SwaggerModelProperty.prototype.toString = function() {
654
+ var req = this.required ? 'propReq' : 'propOpt';
655
+ var str = '<span class="propName ' + req + '">' + this.name + '</span> (<span class="propType">' + this.dataTypeWithRef + '</span>';
656
+ if (!this.required) {
657
+ str += ', <span class="propOptKey">optional</span>';
658
+ }
659
+ str += ')';
660
+ if (this.values != null) {
661
+ str += " = <span class='propVals'>['" + this.values.join("' or '") + "']</span>";
662
+ }
663
+ if (this.descr != null) {
664
+ str += ': <span class="propDesc">' + this.descr + '</span>';
665
+ }
666
+ return str;
667
+ };
668
+
669
+ var SwaggerOperation = function(nickname, path, method, parameters, summary, notes, type, responseMessages, resource, consumes, produces, authorizations) {
670
+ var _this = this;
671
+
672
+ var errors = [];
673
+ this.nickname = (nickname||errors.push("SwaggerOperations must have a nickname."));
674
+ this.path = (path||errors.push("SwaggerOperation " + nickname + " is missing path."));
675
+ this.method = (method||errors.push("SwaggerOperation " + nickname + " is missing method."));
676
+ this.parameters = parameters != null ? parameters : [];
677
+ this.summary = summary;
678
+ this.notes = notes;
679
+ this.type = type;
680
+ this.responseMessages = (responseMessages||[]);
681
+ this.resource = (resource||errors.push("Resource is required"));
682
+ this.consumes = consumes;
683
+ this.produces = produces;
684
+ this.authorizations = authorizations;
685
+ this["do"] = __bind(this["do"], this);
686
+
687
+ if (errors.length > 0)
688
+ this.resource.api.fail(errors);
689
+
690
+ this.path = this.path.replace('{format}', 'json');
691
+ this.method = this.method.toLowerCase();
692
+ this.isGetMethod = this.method === "get";
693
+
694
+ this.resourceName = this.resource.name;
695
+ if(typeof this.type !== 'undefined' && this.type === 'void')
696
+ this.type = null;
697
+ else {
698
+ this.responseClassSignature = this.getSignature(this.type, this.resource.models);
699
+ this.responseSampleJSON = this.getSampleJSON(this.type, this.resource.models);
700
+ }
701
+
702
+ for(var i = 0; i < this.parameters.length; i ++) {
703
+ var param = this.parameters[i];
704
+ // might take this away
705
+ param.name = param.name || param.type || param.dataType;
706
+
707
+ // for 1.1 compatibility
708
+ var type = param.type || param.dataType;
709
+ if(type === 'array') {
710
+ type = 'array[' + (param.items.$ref ? param.items.$ref : param.items.type) + ']';
711
+ }
712
+ param.type = type;
713
+
714
+ if(type.toLowerCase() === 'boolean') {
715
+ param.allowableValues = {};
716
+ param.allowableValues.values = ["true", "false"];
717
+ }
718
+ param.signature = this.getSignature(type, this.resource.models);
719
+ param.sampleJSON = this.getSampleJSON(type, this.resource.models);
720
+
721
+ var enumValue = param["enum"];
722
+ if(enumValue != null) {
723
+ param.isList = true;
724
+ param.allowableValues = {};
725
+ param.allowableValues.descriptiveValues = [];
726
+
727
+ for(var j = 0; j < enumValue.length; j++) {
728
+ var v = enumValue[j];
729
+ if(param.defaultValue != null) {
730
+ param.allowableValues.descriptiveValues.push ({
731
+ value: String(v),
732
+ isDefault: (v === param.defaultValue)
733
+ });
734
+ }
735
+ else {
736
+ param.allowableValues.descriptiveValues.push ({
737
+ value: String(v),
738
+ isDefault: false
739
+ });
740
+ }
741
+ }
742
+ }
743
+ else if(param.allowableValues != null) {
744
+ if(param.allowableValues.valueType === "RANGE")
745
+ param.isRange = true;
746
+ else
747
+ param.isList = true;
748
+ if(param.allowableValues != null) {
749
+ param.allowableValues.descriptiveValues = [];
750
+ if(param.allowableValues.values) {
751
+ for(var j = 0; j < param.allowableValues.values.length; j++){
752
+ var v = param.allowableValues.values[j];
753
+ if(param.defaultValue != null) {
754
+ param.allowableValues.descriptiveValues.push ({
755
+ value: String(v),
756
+ isDefault: (v === param.defaultValue)
757
+ });
758
+ }
759
+ else {
760
+ param.allowableValues.descriptiveValues.push ({
761
+ value: String(v),
762
+ isDefault: false
763
+ });
764
+ }
765
+ }
766
+ }
767
+ }
768
+ }
769
+ }
770
+ this.resource[this.nickname] = function(args, callback, error) {
771
+ return _this["do"](args, callback, error);
772
+ };
773
+ this.resource[this.nickname].help = function() {
774
+ return _this.help();
775
+ };
776
+ }
777
+
778
+ SwaggerOperation.prototype.isListType = function(type) {
779
+ if (type && type.indexOf('[') >= 0) {
780
+ return type.substring(type.indexOf('[') + 1, type.indexOf(']'));
781
+ } else {
782
+ return void 0;
783
+ }
784
+ };
785
+
786
+ SwaggerOperation.prototype.getSignature = function(type, models) {
787
+ var isPrimitive, listType;
788
+ listType = this.isListType(type);
789
+ isPrimitive = ((listType != null) && models[listType]) || (models[type] != null) ? false : true;
790
+ if (isPrimitive) {
791
+ return type;
792
+ } else {
793
+ if (listType != null) {
794
+ return models[listType].getMockSignature();
795
+ } else {
796
+ return models[type].getMockSignature();
797
+ }
798
+ }
799
+ };
800
+
801
+ SwaggerOperation.prototype.getSampleJSON = function(type, models) {
802
+ var isPrimitive, listType, val;
803
+ listType = this.isListType(type);
804
+ isPrimitive = ((listType != null) && models[listType]) || (models[type] != null) ? false : true;
805
+ val = isPrimitive ? void 0 : (listType != null ? models[listType].createJSONSample() : models[type].createJSONSample());
806
+ if (val) {
807
+ val = listType ? [val] : val;
808
+ if(typeof val == "string")
809
+ return val;
810
+ else if(typeof val === "object") {
811
+ var t = val;
812
+ if(val instanceof Array && val.length > 0) {
813
+ t = val[0];
814
+ }
815
+ if(t.nodeName) {
816
+ var xmlString = new XMLSerializer().serializeToString(t);
817
+ return this.formatXml(xmlString);
818
+ }
819
+ else
820
+ return JSON.stringify(val, null, 2);
821
+ }
822
+ else
823
+ return val;
824
+ }
825
+ };
826
+
827
+ SwaggerOperation.prototype["do"] = function(args, opts, callback, error) {
828
+ var key, param, params, possibleParams, req, requestContentType, responseContentType, value, _i, _len, _ref;
829
+ if (args == null) {
830
+ args = {};
831
+ }
832
+ if (opts == null) {
833
+ opts = {};
834
+ }
835
+ requestContentType = null;
836
+ responseContentType = null;
837
+ if ((typeof args) === "function") {
838
+ error = opts;
839
+ callback = args;
840
+ args = {};
841
+ }
842
+ if ((typeof opts) === "function") {
843
+ error = callback;
844
+ callback = opts;
845
+ }
846
+ if (error == null) {
847
+ error = function(xhr, textStatus, error) {
848
+ return log(xhr, textStatus, error);
849
+ };
850
+ }
851
+ if (callback == null) {
852
+ callback = function(response) {
853
+ var content;
854
+ content = null;
855
+ if (response != null) {
856
+ content = response.data;
857
+ } else {
858
+ content = "no data";
859
+ }
860
+ return log("default callback: " + content);
861
+ };
862
+ }
863
+ params = {};
864
+ params.headers = [];
865
+ if (args.headers != null) {
866
+ params.headers = args.headers;
867
+ delete args.headers;
868
+ }
869
+
870
+ var possibleParams = [];
871
+ for(var i = 0; i < this.parameters.length; i++) {
872
+ var param = this.parameters[i];
873
+ if(param.paramType === 'header') {
874
+ if(args[param.name])
875
+ params.headers[param.name] = args[param.name];
876
+ }
877
+ else if(param.paramType === 'form' || param.paramType.toLowerCase() === 'file')
878
+ possibleParams.push(param);
879
+ }
880
+
881
+ if (args.body != null) {
882
+ params.body = args.body;
883
+ delete args.body;
884
+ }
885
+
886
+ if (possibleParams) {
887
+ var key;
888
+ for (key in possibleParams) {
889
+ value = possibleParams[key];
890
+ if (args[value.name]) {
891
+ params[value.name] = args[value.name];
892
+ }
893
+ }
894
+ }
895
+
896
+ req = new SwaggerRequest(this.method, this.urlify(args), params, opts, callback, error, this);
897
+ if (opts.mock != null) {
898
+ return req;
899
+ } else {
900
+ return true;
901
+ }
902
+ };
903
+
904
+ SwaggerOperation.prototype.pathJson = function() {
905
+ return this.path.replace("{format}", "json");
906
+ };
907
+
908
+ SwaggerOperation.prototype.pathXml = function() {
909
+ return this.path.replace("{format}", "xml");
910
+ };
911
+
912
+ SwaggerOperation.prototype.encodePathParam = function(pathParam) {
913
+ var encParts, part, parts, _i, _len;
914
+ pathParam = pathParam.toString();
915
+ if (pathParam.indexOf("/") === -1) {
916
+ return encodeURIComponent(pathParam);
917
+ } else {
918
+ parts = pathParam.split("/");
919
+ encParts = [];
920
+ for (_i = 0, _len = parts.length; _i < _len; _i++) {
921
+ part = parts[_i];
922
+ encParts.push(encodeURIComponent(part));
923
+ }
924
+ return encParts.join("/");
925
+ }
926
+ };
927
+
928
+ SwaggerOperation.prototype.urlify = function(args) {
929
+ var url = this.resource.basePath + this.pathJson();
930
+ var params = this.parameters;
931
+ for(var i = 0; i < params.length; i ++){
932
+ var param = params[i];
933
+ if (param.paramType === 'path') {
934
+ if(args[param.name]) {
935
+ // apply path params and remove from args
936
+ var reg = new RegExp('\{' + param.name + '[^\}]*\}', 'gi');
937
+ url = url.replace(reg, this.encodePathParam(args[param.name]));
938
+ delete args[param.name];
939
+ }
940
+ else
941
+ throw "" + param.name + " is a required path param.";
942
+ }
943
+ }
944
+
945
+ var queryParams = "";
946
+ for(var i = 0; i < params.length; i ++){
947
+ var param = params[i];
948
+ if(param.paramType === 'query') {
949
+ if (args[param.name] !== undefined) {
950
+ if (queryParams !== '')
951
+ queryParams += "&";
952
+ queryParams += encodeURIComponent(param.name) + '=' + encodeURIComponent(args[param.name]);
953
+ }
954
+ }
955
+ }
956
+ if ((queryParams != null) && queryParams.length > 0)
957
+ url += '?' + queryParams;
958
+ return url;
959
+ };
960
+
961
+ SwaggerOperation.prototype.supportHeaderParams = function() {
962
+ return this.resource.api.supportHeaderParams;
963
+ };
964
+
965
+ SwaggerOperation.prototype.supportedSubmitMethods = function() {
966
+ return this.resource.api.supportedSubmitMethods;
967
+ };
968
+
969
+ SwaggerOperation.prototype.getQueryParams = function(args) {
970
+ return this.getMatchingParams(['query'], args);
971
+ };
972
+
973
+ SwaggerOperation.prototype.getHeaderParams = function(args) {
974
+ return this.getMatchingParams(['header'], args);
975
+ };
976
+
977
+ SwaggerOperation.prototype.getMatchingParams = function(paramTypes, args) {
978
+ var matchingParams = {};
979
+ var params = this.parameters;
980
+ for (var i = 0; i < params.length; i++) {
981
+ param = params[i];
982
+ if (args && args[param.name])
983
+ matchingParams[param.name] = args[param.name];
984
+ }
985
+ var headers = this.resource.api.headers;
986
+ var name;
987
+ for (name in headers) {
988
+ var value = headers[name];
989
+ matchingParams[name] = value;
990
+ }
991
+ return matchingParams;
992
+ };
993
+
994
+ SwaggerOperation.prototype.help = function() {
995
+ var msg = "";
996
+ var params = this.parameters;
997
+ for (var i = 0; i < params.length; i++) {
998
+ var param = params[i];
999
+ if (msg !== "")
1000
+ msg += "\n";
1001
+ msg += "* " + param.name + (param.required ? ' (required)' : '') + " - " + param.description;
1002
+ }
1003
+ return msg;
1004
+ };
1005
+
1006
+
1007
+ SwaggerOperation.prototype.formatXml = function(xml) {
1008
+ var contexp, formatted, indent, lastType, lines, ln, pad, reg, transitions, wsexp, _fn, _i, _len;
1009
+ reg = /(>)(<)(\/*)/g;
1010
+ wsexp = /[ ]*(.*)[ ]+\n/g;
1011
+ contexp = /(<.+>)(.+\n)/g;
1012
+ xml = xml.replace(reg, '$1\n$2$3').replace(wsexp, '$1\n').replace(contexp, '$1\n$2');
1013
+ pad = 0;
1014
+ formatted = '';
1015
+ lines = xml.split('\n');
1016
+ indent = 0;
1017
+ lastType = 'other';
1018
+ transitions = {
1019
+ 'single->single': 0,
1020
+ 'single->closing': -1,
1021
+ 'single->opening': 0,
1022
+ 'single->other': 0,
1023
+ 'closing->single': 0,
1024
+ 'closing->closing': -1,
1025
+ 'closing->opening': 0,
1026
+ 'closing->other': 0,
1027
+ 'opening->single': 1,
1028
+ 'opening->closing': 0,
1029
+ 'opening->opening': 1,
1030
+ 'opening->other': 1,
1031
+ 'other->single': 0,
1032
+ 'other->closing': -1,
1033
+ 'other->opening': 0,
1034
+ 'other->other': 0
1035
+ };
1036
+ _fn = function(ln) {
1037
+ var fromTo, j, key, padding, type, types, value;
1038
+ types = {
1039
+ single: Boolean(ln.match(/<.+\/>/)),
1040
+ closing: Boolean(ln.match(/<\/.+>/)),
1041
+ opening: Boolean(ln.match(/<[^!?].*>/))
1042
+ };
1043
+ type = ((function() {
1044
+ var _results;
1045
+ _results = [];
1046
+ for (key in types) {
1047
+ value = types[key];
1048
+ if (value) {
1049
+ _results.push(key);
1050
+ }
1051
+ }
1052
+ return _results;
1053
+ })())[0];
1054
+ type = type === void 0 ? 'other' : type;
1055
+ fromTo = lastType + '->' + type;
1056
+ lastType = type;
1057
+ padding = '';
1058
+ indent += transitions[fromTo];
1059
+ padding = ((function() {
1060
+ var _j, _ref5, _results;
1061
+ _results = [];
1062
+ for (j = _j = 0, _ref5 = indent; 0 <= _ref5 ? _j < _ref5 : _j > _ref5; j = 0 <= _ref5 ? ++_j : --_j) {
1063
+ _results.push(' ');
1064
+ }
1065
+ return _results;
1066
+ })()).join('');
1067
+ if (fromTo === 'opening->closing') {
1068
+ return formatted = formatted.substr(0, formatted.length - 1) + ln + '\n';
1069
+ } else {
1070
+ return formatted += padding + ln + '\n';
1071
+ }
1072
+ };
1073
+ for (_i = 0, _len = lines.length; _i < _len; _i++) {
1074
+ ln = lines[_i];
1075
+ _fn(ln);
1076
+ }
1077
+ return formatted;
1078
+ };
1079
+
1080
+ var SwaggerRequest = function(type, url, params, opts, successCallback, errorCallback, operation, execution) {
1081
+ var _this = this;
1082
+ var errors = [];
1083
+ this.useJQuery = (typeof operation.resource.useJQuery !== 'undefined' ? operation.resource.useJQuery : null);
1084
+ this.type = (type||errors.push("SwaggerRequest type is required (get/post/put/delete/patch/options)."));
1085
+ this.url = (url||errors.push("SwaggerRequest url is required."));
1086
+ this.params = params;
1087
+ this.opts = opts;
1088
+ this.successCallback = (successCallback||errors.push("SwaggerRequest successCallback is required."));
1089
+ this.errorCallback = (errorCallback||errors.push("SwaggerRequest error callback is required."));
1090
+ this.operation = (operation||errors.push("SwaggerRequest operation is required."));
1091
+ this.execution = execution;
1092
+ this.headers = (params.headers||{});
1093
+
1094
+ if(errors.length > 0) {
1095
+ throw errors;
1096
+ }
1097
+
1098
+ this.type = this.type.toUpperCase();
1099
+
1100
+ var myHeaders = {};
1101
+ var body = params.body;
1102
+ var parent = params["parent"];
1103
+ var requestContentType = "application/json";
1104
+
1105
+ var formParams = [];
1106
+ var fileParams = [];
1107
+ var params = this.operation.parameters;
1108
+
1109
+
1110
+ for(var i = 0; i < params.length; i++) {
1111
+ var param = params[i];
1112
+ if(param.paramType === "form")
1113
+ formParams.push(param);
1114
+ else if(param.paramType === "file")
1115
+ fileParams.push(param);
1116
+ }
1117
+
1118
+
1119
+ if (body && (this.type === "POST" || this.type === "PUT" || this.type === "PATCH")) {
1120
+ if (this.opts.requestContentType) {
1121
+ requestContentType = this.opts.requestContentType;
1122
+ }
1123
+ } else {
1124
+ // if any form params, content type must be set
1125
+ if(formParams.length > 0) {
1126
+ if(fileParams.length > 0)
1127
+ requestContentType = "multipart/form-data";
1128
+ else
1129
+ requestContentType = "application/x-www-form-urlencoded";
1130
+ }
1131
+ else if (this.type == "DELETE")
1132
+ body = "{}";
1133
+ else if (this.type != "DELETE")
1134
+ requestContentType = null;
1135
+ }
1136
+
1137
+ if (requestContentType && this.operation.consumes) {
1138
+ if (this.operation.consumes[requestContentType] === 'undefined') {
1139
+ log("server doesn't consume " + requestContentType + ", try " + JSON.stringify(this.operation.consumes));
1140
+ if (this.requestContentType === null) {
1141
+ requestContentType = this.operation.consumes[0];
1142
+ }
1143
+ }
1144
+ }
1145
+
1146
+ var responseContentType = null;
1147
+ if (this.opts.responseContentType) {
1148
+ responseContentType = this.opts.responseContentType;
1149
+ } else {
1150
+ responseContentType = "application/json";
1151
+ }
1152
+ if (responseContentType && this.operation.produces) {
1153
+ if (this.operation.produces[responseContentType] === 'undefined') {
1154
+ log("server can't produce " + responseContentType);
1155
+ }
1156
+ }
1157
+ if (requestContentType && requestContentType.indexOf("application/x-www-form-urlencoded") === 0) {
1158
+ var fields = {};
1159
+ var possibleParams = {};
1160
+ var values = {};
1161
+ var key;
1162
+ for(key in formParams){
1163
+ var param = formParams[key];
1164
+ values[param.name] = param;
1165
+ }
1166
+
1167
+ var encoded = "";
1168
+ var key;
1169
+ for(key in values) {
1170
+ value = this.params[key];
1171
+ if(typeof value !== 'undefined'){
1172
+ if(encoded !== "")
1173
+ encoded += "&";
1174
+ encoded += encodeURIComponent(key) + '=' + encodeURIComponent(value);
1175
+ }
1176
+ }
1177
+ body = encoded;
1178
+ }
1179
+ var name;
1180
+ for (name in this.headers)
1181
+ myHeaders[name] = this.headers[name];
1182
+ if ((requestContentType && body !== "") || (requestContentType === "application/x-www-form-urlencoded"))
1183
+ myHeaders["Content-Type"] = requestContentType;
1184
+ if (responseContentType)
1185
+ myHeaders["Accept"] = responseContentType;
1186
+
1187
+ if (!((this.headers != null) && (this.headers.mock != null))) {
1188
+ obj = {
1189
+ url: this.url,
1190
+ method: this.type,
1191
+ headers: myHeaders,
1192
+ body: body,
1193
+ useJQuery: this.useJQuery,
1194
+ on: {
1195
+ error: function(response) {
1196
+ return _this.errorCallback(response, _this.opts.parent);
1197
+ },
1198
+ redirect: function(response) {
1199
+ return _this.successCallback(response, _this.opts.parent);
1200
+ },
1201
+ 307: function(response) {
1202
+ return _this.successCallback(response, _this.opts.parent);
1203
+ },
1204
+ response: function(response) {
1205
+ return _this.successCallback(response, _this.opts.parent);
1206
+ }
1207
+ }
1208
+ };
1209
+ var e;
1210
+ if (typeof window !== 'undefined') {
1211
+ e = window;
1212
+ } else {
1213
+ e = exports;
1214
+ }
1215
+ status = e.authorizations.apply(obj, this.operation.authorizations);
1216
+ if (opts.mock == null) {
1217
+ if (status !== false) {
1218
+ new SwaggerHttp().execute(obj);
1219
+ } else {
1220
+ obj.canceled = true;
1221
+ }
1222
+ } else {
1223
+ return obj;
1224
+ }
1225
+ }
1226
+ };
1227
+
1228
+ SwaggerRequest.prototype.asCurl = function() {
1229
+ var results = [];
1230
+ if(this.headers) {
1231
+ var key;
1232
+ for(key in this.headers) {
1233
+ results.push("--header \"" + key + ": " + this.headers[v] + "\"");
1234
+ }
1235
+ }
1236
+ return "curl " + (results.join(" ")) + " " + this.url;
1237
+ };
1238
+
1239
+ /**
1240
+ * SwaggerHttp is a wrapper for executing requests
1241
+ */
1242
+ var SwaggerHttp = function() {};
1243
+
1244
+ SwaggerHttp.prototype.execute = function(obj) {
1245
+ if(obj && (typeof obj.useJQuery === 'boolean'))
1246
+ this.useJQuery = obj.useJQuery;
1247
+ else
1248
+ this.useJQuery = this.isIE8();
1249
+
1250
+ if(this.useJQuery)
1251
+ return new JQueryHttpClient().execute(obj);
1252
+ else
1253
+ return new ShredHttpClient().execute(obj);
1254
+ }
1255
+
1256
+ SwaggerHttp.prototype.isIE8 = function() {
1257
+ var detectedIE = false;
1258
+ if (typeof navigator !== 'undefined' && navigator.userAgent) {
1259
+ nav = navigator.userAgent.toLowerCase();
1260
+ if (nav.indexOf('msie') !== -1) {
1261
+ var version = parseInt(nav.split('msie')[1]);
1262
+ if (version <= 8) {
1263
+ detectedIE = true;
1264
+ }
1265
+ }
1266
+ }
1267
+ return detectedIE;
1268
+ };
1269
+
1270
+ /*
1271
+ * JQueryHttpClient lets a browser take advantage of JQuery's cross-browser magic.
1272
+ * NOTE: when jQuery is available it will export both '$' and 'jQuery' to the global space.
1273
+ * Since we are using closures here we need to alias it for internal use.
1274
+ */
1275
+ var JQueryHttpClient = function(options) {
1276
+ "use strict";
1277
+ if(!jQuery){
1278
+ var jQuery = window.jQuery;
1279
+ }
1280
+ }
1281
+
1282
+ JQueryHttpClient.prototype.execute = function(obj) {
1283
+ var cb = obj.on;
1284
+ var request = obj;
1285
+
1286
+ obj.type = obj.method;
1287
+ obj.cache = false;
1288
+
1289
+ obj.beforeSend = function(xhr) {
1290
+ var key, results;
1291
+ if (obj.headers) {
1292
+ results = [];
1293
+ var key;
1294
+ for (key in obj.headers) {
1295
+ if (key.toLowerCase() === "content-type") {
1296
+ results.push(obj.contentType = obj.headers[key]);
1297
+ } else if (key.toLowerCase() === "accept") {
1298
+ results.push(obj.accepts = obj.headers[key]);
1299
+ } else {
1300
+ results.push(xhr.setRequestHeader(key, obj.headers[key]));
1301
+ }
1302
+ }
1303
+ return results;
1304
+ }
1305
+ };
1306
+
1307
+ obj.data = obj.body;
1308
+ obj.complete = function(response, textStatus, opts) {
1309
+ var headers = {},
1310
+ headerArray = response.getAllResponseHeaders().split("\n");
1311
+
1312
+ for(var i = 0; i < headerArray.length; i++) {
1313
+ var toSplit = headerArray[i].trim();
1314
+ if(toSplit.length === 0)
1315
+ continue;
1316
+ var separator = toSplit.indexOf(":");
1317
+ if(separator === -1) {
1318
+ // Name but no value in the header
1319
+ headers[toSplit] = null;
1320
+ continue;
1321
+ }
1322
+ var name = toSplit.substring(0, separator).trim(),
1323
+ value = toSplit.substring(separator + 1).trim();
1324
+ headers[name] = value;
1325
+ }
1326
+
1327
+ var out = {
1328
+ url: request.url,
1329
+ method: request.method,
1330
+ status: response.status,
1331
+ data: response.responseText,
1332
+ headers: headers
1333
+ };
1334
+
1335
+ var contentType = (headers["content-type"]||headers["Content-Type"]||null)
1336
+
1337
+ if(contentType != null) {
1338
+ if(contentType.indexOf("application/json") == 0 || contentType.indexOf("+json") > 0) {
1339
+ if(response.responseText && response.responseText !== "")
1340
+ out.obj = JSON.parse(response.responseText);
1341
+ else
1342
+ out.obj = {}
1343
+ }
1344
+ }
1345
+
1346
+ if(response.status >= 200 && response.status < 300)
1347
+ cb.response(out);
1348
+ else if(response.status === 0 || (response.status >= 400 && response.status < 599))
1349
+ cb.error(out);
1350
+ else
1351
+ return cb.response(out);
1352
+ };
1353
+
1354
+ jQuery.support.cors = true;
1355
+ return jQuery.ajax(obj);
1356
+ }
1357
+
1358
+ /*
1359
+ * ShredHttpClient is a light-weight, node or browser HTTP client
1360
+ */
1361
+ var ShredHttpClient = function(options) {
1362
+ this.options = (options||{});
1363
+ this.isInitialized = false;
1364
+
1365
+ var identity, toString;
1366
+
1367
+ if (typeof window !== 'undefined') {
1368
+ this.Shred = require("./shred");
1369
+ this.content = require("./shred/content");
1370
+ }
1371
+ else
1372
+ this.Shred = require("shred");
1373
+ this.shred = new this.Shred();
1374
+ }
1375
+
1376
+ ShredHttpClient.prototype.initShred = function () {
1377
+ this.isInitialized = true;
1378
+ this.registerProcessors(this.shred);
1379
+ }
1380
+
1381
+ ShredHttpClient.prototype.registerProcessors = function(shred) {
1382
+ var identity = function(x) {
1383
+ return x;
1384
+ };
1385
+ var toString = function(x) {
1386
+ return x.toString();
1387
+ };
1388
+
1389
+ if (typeof window !== 'undefined') {
1390
+ this.content.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], {
1391
+ parser: identity,
1392
+ stringify: toString
1393
+ });
1394
+ } else {
1395
+ this.Shred.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], {
1396
+ parser: identity,
1397
+ stringify: toString
1398
+ });
1399
+ }
1400
+ }
1401
+
1402
+ ShredHttpClient.prototype.execute = function(obj) {
1403
+ if(!this.isInitialized)
1404
+ this.initShred();
1405
+
1406
+ var cb = obj.on, res;
1407
+
1408
+ var transform = function(response) {
1409
+ var out = {
1410
+ headers: response._headers,
1411
+ url: response.request.url,
1412
+ method: response.request.method,
1413
+ status: response.status,
1414
+ data: response.content.data
1415
+ };
1416
+
1417
+ var contentType = (response._headers["content-type"]||response._headers["Content-Type"]||null)
1418
+
1419
+ if(contentType != null) {
1420
+ if(contentType.indexOf("application/json") == 0 || contentType.indexOf("+json") > 0) {
1421
+ if(response.content.data && response.content.data !== "")
1422
+ out.obj = JSON.parse(response.content.data);
1423
+ else
1424
+ out.obj = {}
1425
+ }
1426
+ }
1427
+ return out;
1428
+ };
1429
+
1430
+ res = {
1431
+ error: function(response) {
1432
+ if (obj)
1433
+ return cb.error(transform(response));
1434
+ },
1435
+ redirect: function(response) {
1436
+ if (obj)
1437
+ return cb.redirect(transform(response));
1438
+ },
1439
+ 307: function(response) {
1440
+ if (obj)
1441
+ return cb.redirect(transform(response));
1442
+ },
1443
+ response: function(response) {
1444
+ if (obj)
1445
+ return cb.response(transform(response));
1446
+ }
1447
+ };
1448
+ if (obj) {
1449
+ obj.on = res;
1450
+ }
1451
+ return this.shred.request(obj);
1452
+ };
1453
+
1454
+ /**
1455
+ * SwaggerAuthorizations applys the correct authorization to an operation being executed
1456
+ */
1457
+ var SwaggerAuthorizations = function() {
1458
+ this.authz = {};
1459
+ };
1460
+
1461
+ SwaggerAuthorizations.prototype.add = function(name, auth) {
1462
+ this.authz[name] = auth;
1463
+ return auth;
1464
+ };
1465
+
1466
+ SwaggerAuthorizations.prototype.remove = function(name) {
1467
+ return delete this.authz[name];
1468
+ };
1469
+
1470
+ SwaggerAuthorizations.prototype.apply = function(obj, authorizations) {
1471
+ var status = null;
1472
+ var key;
1473
+
1474
+ // if the "authorizations" key is undefined, or has an empty array, add all keys
1475
+ if(typeof authorizations === 'undefined' || Object.keys(authorizations).length == 0) {
1476
+ for (key in this.authz) {
1477
+ value = this.authz[key];
1478
+ result = value.apply(obj, authorizations);
1479
+ if (result === true)
1480
+ status = true;
1481
+ }
1482
+ }
1483
+ else {
1484
+ for(name in authorizations) {
1485
+ for (key in this.authz) {
1486
+ if(key == name) {
1487
+ value = this.authz[key];
1488
+ result = value.apply(obj, authorizations);
1489
+ if (result === true)
1490
+ status = true;
1491
+ }
1492
+ }
1493
+ }
1494
+ }
1495
+
1496
+ return status;
1497
+ };
1498
+
1499
+ /**
1500
+ * ApiKeyAuthorization allows a query param or header to be injected
1501
+ */
1502
+ var ApiKeyAuthorization = function(name, value, type) {
1503
+ this.name = name;
1504
+ this.value = value;
1505
+ this.type = type;
1506
+ };
1507
+
1508
+ ApiKeyAuthorization.prototype.apply = function(obj, authorizations) {
1509
+ if (this.type === "query") {
1510
+ if (obj.url.indexOf('?') > 0)
1511
+ obj.url = obj.url + "&" + this.name + "=" + this.value;
1512
+ else
1513
+ obj.url = obj.url + "?" + this.name + "=" + this.value;
1514
+ return true;
1515
+ } else if (this.type === "header") {
1516
+ obj.headers[this.name] = this.value;
1517
+ return true;
1518
+ }
1519
+ };
1520
+
1521
+ var CookieAuthorization = function(cookie) {
1522
+ this.cookie = cookie;
1523
+ }
1524
+
1525
+ CookieAuthorization.prototype.apply = function(obj, authorizations) {
1526
+ obj.cookieJar = obj.cookieJar || CookieJar();
1527
+ obj.cookieJar.setCookie(this.cookie);
1528
+ return true;
1529
+ }
1530
+
1531
+ /**
1532
+ * Password Authorization is a basic auth implementation
1533
+ */
1534
+ var PasswordAuthorization = function(name, username, password) {
1535
+ this.name = name;
1536
+ this.username = username;
1537
+ this.password = password;
1538
+ this._btoa = null;
1539
+ if (typeof window !== 'undefined')
1540
+ this._btoa = btoa;
1541
+ else
1542
+ this._btoa = require("btoa");
1543
+ };
1544
+
1545
+ PasswordAuthorization.prototype.apply = function(obj, authorizations) {
1546
+ var base64encoder = this._btoa;
1547
+ obj.headers["Authorization"] = "Basic " + base64encoder(this.username + ":" + this.password);
1548
+ return true;
1549
+ };
1550
+
1551
+ var e = (typeof window !== 'undefined' ? window : exports);
1552
+
1553
+ var sampleModels = {};
1554
+ var cookies = {};
1555
+
1556
+ e.SampleModels = sampleModels;
1557
+ e.SwaggerHttp = SwaggerHttp;
1558
+ e.SwaggerRequest = SwaggerRequest;
1559
+ e.authorizations = new SwaggerAuthorizations();
1560
+ e.ApiKeyAuthorization = ApiKeyAuthorization;
1561
+ e.PasswordAuthorization = PasswordAuthorization;
1562
+ e.CookieAuthorization = CookieAuthorization;
1563
+ e.JQueryHttpClient = JQueryHttpClient;
1564
+ e.ShredHttpClient = ShredHttpClient;
1565
+ e.SwaggerOperation = SwaggerOperation;
1566
+ e.SwaggerModel = SwaggerModel;
1567
+ e.SwaggerModelProperty = SwaggerModelProperty;
1568
+ e.SwaggerResource = SwaggerResource;
1569
+ e.SwaggerApi = SwaggerApi;