You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

1570 lines
45 KiB

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