Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

swagger.js 33 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. // Generated by CoffeeScript 1.4.0
  2. (function() {
  3. var ApiKeyAuthorization, SwaggerApi, SwaggerAuthorizations, SwaggerHttp, SwaggerModel, SwaggerModelProperty, SwaggerOperation, SwaggerRequest, SwaggerResource,
  4. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  5. SwaggerApi = (function() {
  6. SwaggerApi.prototype.discoveryUrl = "http://api.wordnik.com/v4/resources.json";
  7. SwaggerApi.prototype.debug = false;
  8. SwaggerApi.prototype.basePath = null;
  9. SwaggerApi.prototype.authorizations = null;
  10. SwaggerApi.prototype.authorizationScheme = null;
  11. function SwaggerApi(options) {
  12. if (options == null) {
  13. options = {};
  14. }
  15. if (options.discoveryUrl != null) {
  16. this.discoveryUrl = options.discoveryUrl;
  17. }
  18. this.supportedSubmitMethods = options.supportedSubmitMethods != null ? options.supportedSubmitMethods : ['get'];
  19. if (options.success != null) {
  20. this.success = options.success;
  21. }
  22. this.failure = options.failure != null ? options.failure : function() {};
  23. this.progress = options.progress != null ? options.progress : function() {};
  24. this.defaultHeaders = options.headers != null ? options.headers : {};
  25. if (options.success != null) {
  26. this.build();
  27. }
  28. }
  29. SwaggerApi.prototype.build = function() {
  30. var obj,
  31. _this = this;
  32. this.progress('fetching resource list: ' + this.discoveryUrl);
  33. obj = {
  34. url: this.discoveryUrl,
  35. method: "get",
  36. on: {
  37. error: function(response) {
  38. if (_this.discoveryUrl.substring(0, 4) !== 'http') {
  39. return _this.fail('Please specify the protocol for ' + _this.discoveryUrl);
  40. } else if (error.status === 0) {
  41. return _this.fail('Can\'t read from server. It may not have the appropriate access-control-origin settings.');
  42. } else if (error.status === 404) {
  43. return _this.fail('Can\'t read swagger JSON from ' + _this.discoveryUrl);
  44. } else {
  45. return _this.fail(error.status + ' : ' + error.statusText + ' ' + _this.discoveryUrl);
  46. }
  47. },
  48. response: function(rawResponse) {
  49. var res, resource, response, _i, _len, _ref;
  50. response = JSON.parse(rawResponse.content.data);
  51. if (response.apiVersion != null) {
  52. _this.apiVersion = response.apiVersion;
  53. }
  54. _this.apis = {};
  55. _this.apisArray = [];
  56. if (response.basePath) {
  57. _this.basePath = response.basePath;
  58. } else if (_this.discoveryUrl.indexOf('?') > 0) {
  59. _this.basePath = _this.discoveryUrl.substring(0, _this.discoveryUrl.lastIndexOf('?'));
  60. } else {
  61. _this.basePath = _this.discoveryUrl;
  62. }
  63. _ref = response.apis;
  64. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  65. resource = _ref[_i];
  66. res = new SwaggerResource(resource, _this);
  67. _this.apis[res.name] = res;
  68. _this.apisArray.push(res);
  69. }
  70. return _this;
  71. }
  72. }
  73. };
  74. return new SwaggerHttp().execute(obj);
  75. };
  76. SwaggerApi.prototype.selfReflect = function() {
  77. var resource, resource_name, _ref;
  78. if (this.apis == null) {
  79. return false;
  80. }
  81. _ref = this.apis;
  82. for (resource_name in _ref) {
  83. resource = _ref[resource_name];
  84. if (resource.ready == null) {
  85. return false;
  86. }
  87. }
  88. this.setConsolidatedModels();
  89. this.ready = true;
  90. if (this.success != null) {
  91. return this.success();
  92. }
  93. };
  94. SwaggerApi.prototype.fail = function(message) {
  95. this.failure(message);
  96. throw message;
  97. };
  98. SwaggerApi.prototype.setConsolidatedModels = function() {
  99. var model, modelName, resource, resource_name, _i, _len, _ref, _ref1, _results;
  100. this.modelsArray = [];
  101. this.models = {};
  102. _ref = this.apis;
  103. for (resource_name in _ref) {
  104. resource = _ref[resource_name];
  105. for (modelName in resource.models) {
  106. if (!(this.models[modelName] != null)) {
  107. this.models[modelName] = resource.models[modelName];
  108. this.modelsArray.push(resource.models[modelName]);
  109. }
  110. }
  111. }
  112. _ref1 = this.modelsArray;
  113. _results = [];
  114. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  115. model = _ref1[_i];
  116. _results.push(model.setReferencedModels(this.models));
  117. }
  118. return _results;
  119. };
  120. SwaggerApi.prototype.help = function() {
  121. var operation, operation_name, parameter, resource, resource_name, _i, _len, _ref, _ref1, _ref2;
  122. _ref = this.apis;
  123. for (resource_name in _ref) {
  124. resource = _ref[resource_name];
  125. console.log(resource_name);
  126. _ref1 = resource.operations;
  127. for (operation_name in _ref1) {
  128. operation = _ref1[operation_name];
  129. console.log(" " + operation.nickname);
  130. _ref2 = operation.parameters;
  131. for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  132. parameter = _ref2[_i];
  133. console.log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description);
  134. }
  135. }
  136. }
  137. return this;
  138. };
  139. return SwaggerApi;
  140. })();
  141. SwaggerResource = (function() {
  142. SwaggerResource.prototype.api = null;
  143. SwaggerResource.prototype.produces = null;
  144. SwaggerResource.prototype.consumes = null;
  145. function SwaggerResource(resourceObj, api) {
  146. var consumes, obj, parts, produces,
  147. _this = this;
  148. this.api = api;
  149. this.api = this.api;
  150. produces = [];
  151. consumes = [];
  152. this.path = this.api.resourcePath != null ? this.api.resourcePath : resourceObj.path;
  153. this.description = resourceObj.description;
  154. parts = this.path.split("/");
  155. this.name = parts[parts.length - 1].replace('.{format}', '');
  156. this.basePath = this.api.basePath;
  157. this.operations = {};
  158. this.operationsArray = [];
  159. this.modelsArray = [];
  160. this.models = {};
  161. if ((resourceObj.operations != null) && (this.api.resourcePath != null)) {
  162. this.api.progress('reading resource ' + this.name + ' models and operations');
  163. this.addModels(resourceObj.models);
  164. this.addOperations(resourceObj.path, resourceObj.operations);
  165. this.api[this.name] = this;
  166. } else {
  167. if (this.path == null) {
  168. this.api.fail("SwaggerResources must have a path.");
  169. }
  170. this.url = this.api.basePath + this.path.replace('{format}', 'json');
  171. this.api.progress('fetching resource ' + this.name + ': ' + this.url);
  172. obj = {
  173. url: this.url,
  174. method: "get",
  175. on: {
  176. error: function(response) {
  177. return _this.api.fail("Unable to read api '" + _this.name + "' from path " + _this.url + " (server returned " + error.statusText + ")");
  178. },
  179. response: function(rawResponse) {
  180. var endpoint, response, _i, _len, _ref;
  181. response = JSON.parse(rawResponse.content._body);
  182. if (response.produces != null) {
  183. _this.produces = response.produces;
  184. }
  185. if (response.consumes != null) {
  186. _this.consumes = response.consumes;
  187. }
  188. if ((response.basePath != null) && response.basePath.replace(/\s/g, '').length > 0) {
  189. _this.basePath = response.basePath;
  190. }
  191. _this.addModels(response.models);
  192. if (response.apis) {
  193. _ref = response.apis;
  194. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  195. endpoint = _ref[_i];
  196. _this.addOperations(endpoint.path, endpoint.operations);
  197. }
  198. }
  199. _this.api[_this.name] = _this;
  200. _this.ready = true;
  201. return _this.api.selfReflect();
  202. }
  203. }
  204. };
  205. new SwaggerHttp().execute(obj);
  206. }
  207. }
  208. SwaggerResource.prototype.addModels = function(models) {
  209. var model, modelName, swaggerModel, _i, _len, _ref, _results;
  210. if (models != null) {
  211. for (modelName in models) {
  212. if (!(this.models[modelName] != null)) {
  213. swaggerModel = new SwaggerModel(modelName, models[modelName]);
  214. this.modelsArray.push(swaggerModel);
  215. this.models[modelName] = swaggerModel;
  216. }
  217. }
  218. _ref = this.modelsArray;
  219. _results = [];
  220. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  221. model = _ref[_i];
  222. _results.push(model.setReferencedModels(this.models));
  223. }
  224. return _results;
  225. }
  226. };
  227. SwaggerResource.prototype.addOperations = function(resource_path, ops) {
  228. var consumes, method, o, op, produces, responseMessages, _i, _len, _results;
  229. if (ops) {
  230. _results = [];
  231. for (_i = 0, _len = ops.length; _i < _len; _i++) {
  232. o = ops[_i];
  233. consumes = null;
  234. produces = null;
  235. if (o.consumes != null) {
  236. consumes = o.consumes;
  237. } else {
  238. consumes = this.consumes;
  239. }
  240. if (o.produces != null) {
  241. produces = o.produces;
  242. } else {
  243. produces = this.produces;
  244. }
  245. responseMessages = o.responseMessages;
  246. method = o.method;
  247. if (o.httpMethod) {
  248. method = o.httpMethod;
  249. }
  250. if (o.supportedContentTypes) {
  251. consumes = o.supportedContentTypes;
  252. }
  253. if (o.errorResponses) {
  254. responseMessages = o.errorResponses;
  255. }
  256. op = new SwaggerOperation(o.nickname, resource_path, method, o.parameters, o.summary, o.notes, o.responseClass, responseMessages, this, consumes, produces);
  257. this.operations[op.nickname] = op;
  258. _results.push(this.operationsArray.push(op));
  259. }
  260. return _results;
  261. }
  262. };
  263. SwaggerResource.prototype.help = function() {
  264. var msg, operation, operation_name, parameter, _i, _len, _ref, _ref1, _results;
  265. _ref = this.operations;
  266. _results = [];
  267. for (operation_name in _ref) {
  268. operation = _ref[operation_name];
  269. msg = " " + operation.nickname;
  270. _ref1 = operation.parameters;
  271. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  272. parameter = _ref1[_i];
  273. msg.concat(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description);
  274. }
  275. _results.push(msg);
  276. }
  277. return _results;
  278. };
  279. return SwaggerResource;
  280. })();
  281. SwaggerModel = (function() {
  282. function SwaggerModel(modelName, obj) {
  283. var propertyName;
  284. this.name = obj.id != null ? obj.id : modelName;
  285. this.properties = [];
  286. for (propertyName in obj.properties) {
  287. this.properties.push(new SwaggerModelProperty(propertyName, obj.properties[propertyName]));
  288. }
  289. }
  290. SwaggerModel.prototype.setReferencedModels = function(allModels) {
  291. var prop, _i, _len, _ref, _results;
  292. _ref = this.properties;
  293. _results = [];
  294. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  295. prop = _ref[_i];
  296. if (allModels[prop.dataType] != null) {
  297. _results.push(prop.refModel = allModels[prop.dataType]);
  298. } else if ((prop.refDataType != null) && (allModels[prop.refDataType] != null)) {
  299. _results.push(prop.refModel = allModels[prop.refDataType]);
  300. } else {
  301. _results.push(void 0);
  302. }
  303. }
  304. return _results;
  305. };
  306. SwaggerModel.prototype.getMockSignature = function(modelsToIgnore) {
  307. var classClose, classOpen, prop, propertiesStr, returnVal, strong, strongClose, stronger, _i, _j, _len, _len1, _ref, _ref1;
  308. propertiesStr = [];
  309. _ref = this.properties;
  310. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  311. prop = _ref[_i];
  312. propertiesStr.push(prop.toString());
  313. }
  314. strong = '<span class="strong">';
  315. stronger = '<span class="stronger">';
  316. strongClose = '</span>';
  317. classOpen = strong + this.name + ' {' + strongClose;
  318. classClose = strong + '}' + strongClose;
  319. returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
  320. if (!modelsToIgnore) {
  321. modelsToIgnore = [];
  322. }
  323. modelsToIgnore.push(this);
  324. _ref1 = this.properties;
  325. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  326. prop = _ref1[_j];
  327. if ((prop.refModel != null) && (modelsToIgnore.indexOf(prop.refModel)) === -1) {
  328. returnVal = returnVal + ('<br>' + prop.refModel.getMockSignature(modelsToIgnore));
  329. }
  330. }
  331. return returnVal;
  332. };
  333. SwaggerModel.prototype.createJSONSample = function(modelsToIgnore) {
  334. var prop, result, _i, _len, _ref;
  335. result = {};
  336. modelsToIgnore = modelsToIgnore || [];
  337. modelsToIgnore.push(this.name);
  338. _ref = this.properties;
  339. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  340. prop = _ref[_i];
  341. result[prop.name] = prop.getSampleValue(modelsToIgnore);
  342. }
  343. return result;
  344. };
  345. return SwaggerModel;
  346. })();
  347. SwaggerModelProperty = (function() {
  348. function SwaggerModelProperty(name, obj) {
  349. this.name = name;
  350. this.dataType = obj.type;
  351. this.isCollection = this.dataType && (this.dataType.toLowerCase() === 'array' || this.dataType.toLowerCase() === 'list' || this.dataType.toLowerCase() === 'set');
  352. this.descr = obj.description;
  353. this.required = obj.required;
  354. if (obj.items != null) {
  355. if (obj.items.type != null) {
  356. this.refDataType = obj.items.type;
  357. }
  358. if (obj.items.$ref != null) {
  359. this.refDataType = obj.items.$ref;
  360. }
  361. }
  362. this.dataTypeWithRef = this.refDataType != null ? this.dataType + '[' + this.refDataType + ']' : this.dataType;
  363. if (obj.allowableValues != null) {
  364. this.valueType = obj.allowableValues.valueType;
  365. this.values = obj.allowableValues.values;
  366. if (this.values != null) {
  367. this.valuesString = "'" + this.values.join("' or '") + "'";
  368. }
  369. }
  370. }
  371. SwaggerModelProperty.prototype.getSampleValue = function(modelsToIgnore) {
  372. var result;
  373. if ((this.refModel != null) && (modelsToIgnore.indexOf(this.refModel.name) === -1)) {
  374. result = this.refModel.createJSONSample(modelsToIgnore);
  375. } else {
  376. if (this.isCollection) {
  377. result = this.refDataType;
  378. } else {
  379. result = this.dataType;
  380. }
  381. }
  382. if (this.isCollection) {
  383. return [result];
  384. } else {
  385. return result;
  386. }
  387. };
  388. SwaggerModelProperty.prototype.toString = function() {
  389. var req, str;
  390. req = this.required ? 'propReq' : 'propOpt';
  391. str = '<span class="propName ' + req + '">' + this.name + '</span> (<span class="propType">' + this.dataTypeWithRef + '</span>';
  392. if (!this.required) {
  393. str += ', <span class="propOptKey">optional</span>';
  394. }
  395. str += ')';
  396. if (this.values != null) {
  397. str += " = <span class='propVals'>['" + this.values.join("' or '") + "']</span>";
  398. }
  399. if (this.descr != null) {
  400. str += ': <span class="propDesc">' + this.descr + '</span>';
  401. }
  402. return str;
  403. };
  404. return SwaggerModelProperty;
  405. })();
  406. SwaggerOperation = (function() {
  407. function SwaggerOperation(nickname, path, method, parameters, summary, notes, responseClass, responseMessages, resource, consumes, produces) {
  408. var parameter, v, _i, _j, _len, _len1, _ref, _ref1, _ref2,
  409. _this = this;
  410. this.nickname = nickname;
  411. this.path = path;
  412. this.method = method;
  413. this.parameters = parameters != null ? parameters : [];
  414. this.summary = summary;
  415. this.notes = notes;
  416. this.responseClass = responseClass;
  417. this.responseMessages = responseMessages;
  418. this.resource = resource;
  419. this.consumes = consumes;
  420. this.produces = produces;
  421. this["do"] = __bind(this["do"], this);
  422. if (this.nickname == null) {
  423. this.resource.api.fail("SwaggerOperations must have a nickname.");
  424. }
  425. if (this.path == null) {
  426. this.resource.api.fail("SwaggerOperation " + nickname + " is missing path.");
  427. }
  428. if (this.method == null) {
  429. this.resource.api.fail("SwaggerOperation " + nickname + " is missing method.");
  430. }
  431. this.path = this.path.replace('{format}', 'json');
  432. this.method = this.method.toLowerCase();
  433. this.isGetMethod = this.method === "get";
  434. this.resourceName = this.resource.name;
  435. if (((_ref = this.responseClass) != null ? _ref.toLowerCase() : void 0) === 'void') {
  436. this.responseClass = void 0;
  437. }
  438. if (this.responseClass != null) {
  439. this.responseClassSignature = this.getSignature(this.responseClass, this.resource.models);
  440. this.responseSampleJSON = this.getSampleJSON(this.responseClass, this.resource.models);
  441. }
  442. this.responseMessages = this.responseMessages || [];
  443. _ref1 = this.parameters;
  444. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  445. parameter = _ref1[_i];
  446. parameter.name = parameter.name || parameter.dataType;
  447. if (parameter.dataType.toLowerCase() === 'boolean') {
  448. parameter.allowableValues = {};
  449. parameter.allowableValues.values = this.resource.api.booleanValues;
  450. }
  451. parameter.signature = this.getSignature(parameter.dataType, this.resource.models);
  452. parameter.sampleJSON = this.getSampleJSON(parameter.dataType, this.resource.models);
  453. if (parameter.allowableValues != null) {
  454. if (parameter.allowableValues.valueType === "RANGE") {
  455. parameter.isRange = true;
  456. } else {
  457. parameter.isList = true;
  458. }
  459. if (parameter.allowableValues.values != null) {
  460. parameter.allowableValues.descriptiveValues = [];
  461. _ref2 = parameter.allowableValues.values;
  462. for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
  463. v = _ref2[_j];
  464. if ((parameter.defaultValue != null) && parameter.defaultValue === v) {
  465. parameter.allowableValues.descriptiveValues.push({
  466. value: v,
  467. isDefault: true
  468. });
  469. } else {
  470. parameter.allowableValues.descriptiveValues.push({
  471. value: v,
  472. isDefault: false
  473. });
  474. }
  475. }
  476. }
  477. }
  478. }
  479. this.resource[this.nickname] = function(args, callback, error) {
  480. return _this["do"](args, callback, error);
  481. };
  482. }
  483. SwaggerOperation.prototype.isListType = function(dataType) {
  484. if (dataType.indexOf('[') >= 0) {
  485. return dataType.substring(dataType.indexOf('[') + 1, dataType.indexOf(']'));
  486. } else {
  487. return void 0;
  488. }
  489. };
  490. SwaggerOperation.prototype.getSignature = function(dataType, models) {
  491. var isPrimitive, listType;
  492. listType = this.isListType(dataType);
  493. isPrimitive = ((listType != null) && models[listType]) || (models[dataType] != null) ? false : true;
  494. if (isPrimitive) {
  495. return dataType;
  496. } else {
  497. if (listType != null) {
  498. return models[listType].getMockSignature();
  499. } else {
  500. return models[dataType].getMockSignature();
  501. }
  502. }
  503. };
  504. SwaggerOperation.prototype.getSampleJSON = function(dataType, models) {
  505. var isPrimitive, listType, val;
  506. listType = this.isListType(dataType);
  507. isPrimitive = ((listType != null) && models[listType]) || (models[dataType] != null) ? false : true;
  508. val = isPrimitive ? void 0 : (listType != null ? models[listType].createJSONSample() : models[dataType].createJSONSample());
  509. if (val) {
  510. val = listType ? [val] : val;
  511. return JSON.stringify(val, null, 2);
  512. }
  513. };
  514. SwaggerOperation.prototype["do"] = function(args, callback, error) {
  515. var key, param, params, possibleParams, requestContentType, responseContentType, value;
  516. if (args == null) {
  517. args = {};
  518. }
  519. requestContentType = null;
  520. responseContentType = null;
  521. if ((typeof args) === "function") {
  522. error = callback;
  523. callback = args;
  524. args = {};
  525. }
  526. if (error == null) {
  527. error = function(xhr, textStatus, error) {
  528. return console.log(xhr, textStatus, error);
  529. };
  530. }
  531. if (callback == null) {
  532. callback = function(data) {
  533. return console.log("default callback: " + data);
  534. };
  535. }
  536. params = {};
  537. if (args.requestContentType) {
  538. requestContentType = args.requestContentType;
  539. }
  540. if (args.responseContentType) {
  541. responseContentType = args.responseContentType;
  542. }
  543. if (args.headers != null) {
  544. params.headers = args.headers;
  545. delete args.headers;
  546. }
  547. if (args.body != null) {
  548. params.body = args.body;
  549. delete args.body;
  550. }
  551. possibleParams = (function() {
  552. var _i, _len, _ref, _results;
  553. _ref = this.parameters;
  554. _results = [];
  555. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  556. param = _ref[_i];
  557. if (param.paramType === "form") {
  558. _results.push(param);
  559. }
  560. }
  561. return _results;
  562. }).call(this);
  563. if (possibleParams) {
  564. for (key in possibleParams) {
  565. value = possibleParams[key];
  566. if (args[value.name]) {
  567. params[value.name] = args[value.name];
  568. }
  569. }
  570. }
  571. if (args.mock != null) {
  572. params.mock = args["mock"];
  573. }
  574. params["parent"] = args["parent"];
  575. return new SwaggerRequest(this.method, this.urlify(args), params, requestContentType, responseContentType, callback, error, this);
  576. };
  577. SwaggerOperation.prototype.pathJson = function() {
  578. return this.path.replace("{format}", "json");
  579. };
  580. SwaggerOperation.prototype.pathXml = function() {
  581. return this.path.replace("{format}", "xml");
  582. };
  583. SwaggerOperation.prototype.urlify = function(args) {
  584. var param, queryParams, reg, url, _i, _j, _len, _len1, _ref, _ref1;
  585. url = this.resource.basePath + this.pathJson();
  586. _ref = this.parameters;
  587. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  588. param = _ref[_i];
  589. if (param.paramType === 'path') {
  590. if (args[param.name]) {
  591. reg = new RegExp('\{' + param.name + '[^\}]*\}', 'gi');
  592. url = url.replace(reg, encodeURIComponent(args[param.name]));
  593. delete args[param.name];
  594. } else {
  595. throw "" + param.name + " is a required path param.";
  596. }
  597. }
  598. }
  599. queryParams = "";
  600. _ref1 = this.parameters;
  601. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  602. param = _ref1[_j];
  603. if (param.paramType === 'query') {
  604. if (args[param.name]) {
  605. if (queryParams !== "") {
  606. queryParams += "&";
  607. }
  608. queryParams += encodeURIComponent(param.name) + '=' + encodeURIComponent(args[param.name]);
  609. }
  610. }
  611. }
  612. if ((queryParams != null) && queryParams.length > 0) {
  613. url += "?" + queryParams;
  614. }
  615. return url;
  616. };
  617. SwaggerOperation.prototype.supportHeaderParams = function() {
  618. return this.resource.api.supportHeaderParams;
  619. };
  620. SwaggerOperation.prototype.supportedSubmitMethods = function() {
  621. return this.resource.api.supportedSubmitMethods;
  622. };
  623. SwaggerOperation.prototype.getQueryParams = function(args) {
  624. return this.getMatchingParams(['query'], args);
  625. };
  626. SwaggerOperation.prototype.getHeaderParams = function(args) {
  627. return this.getMatchingParams(['header'], args);
  628. };
  629. SwaggerOperation.prototype.getMatchingParams = function(paramTypes, args) {
  630. var matchingParams, name, param, value, _i, _len, _ref, _ref1;
  631. matchingParams = {};
  632. _ref = this.parameters;
  633. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  634. param = _ref[_i];
  635. if (args && args[param.name]) {
  636. matchingParams[param.name] = args[param.name];
  637. }
  638. }
  639. _ref1 = this.resource.api.headers;
  640. for (name in _ref1) {
  641. value = _ref1[name];
  642. matchingParams[name] = value;
  643. }
  644. return matchingParams;
  645. };
  646. SwaggerOperation.prototype.help = function() {
  647. var msg, parameter, _i, _len, _ref;
  648. msg = "";
  649. _ref = this.parameters;
  650. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  651. parameter = _ref[_i];
  652. if (msg !== "") {
  653. msg += "\n";
  654. }
  655. msg += "* " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description;
  656. }
  657. return msg;
  658. };
  659. return SwaggerOperation;
  660. })();
  661. SwaggerRequest = (function() {
  662. function SwaggerRequest(type, url, params, requestContentType, responseContentType, successCallback, errorCallback, operation, execution) {
  663. var body, e, fields, headers, key, myHeaders, obj, param, parent, possibleParams, urlEncoded, value, values,
  664. _this = this;
  665. this.type = type;
  666. this.url = url;
  667. this.params = params;
  668. this.requestContentType = requestContentType;
  669. this.responseContentType = responseContentType;
  670. this.successCallback = successCallback;
  671. this.errorCallback = errorCallback;
  672. this.operation = operation;
  673. this.execution = execution;
  674. if (this.type == null) {
  675. throw "SwaggerRequest type is required (get/post/put/delete).";
  676. }
  677. if (this.url == null) {
  678. throw "SwaggerRequest url is required.";
  679. }
  680. if (this.successCallback == null) {
  681. throw "SwaggerRequest successCallback is required.";
  682. }
  683. if (this.errorCallback == null) {
  684. throw "SwaggerRequest error callback is required.";
  685. }
  686. if (this.operation == null) {
  687. throw "SwaggerRequest operation is required.";
  688. }
  689. this.type = this.type.toUpperCase();
  690. headers = params.headers;
  691. myHeaders = {};
  692. body = params.body;
  693. parent = params["parent"];
  694. requestContentType = "application/json";
  695. if (body && (this.type === "POST" || this.type === "PUT" || this.type === "PATCH")) {
  696. if (this.requestContentType) {
  697. requestContentType = this.requestContentType;
  698. }
  699. } else {
  700. if (((function() {
  701. var _i, _len, _ref, _results;
  702. _ref = this.operation.parameters;
  703. _results = [];
  704. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  705. param = _ref[_i];
  706. if (param.paramType === "form") {
  707. _results.push(param);
  708. }
  709. }
  710. return _results;
  711. }).call(this)).length > 0) {
  712. requestContentType = "application/x-www-form-urlencoded";
  713. } else if (this.type !== "DELETE") {
  714. requestContentType = null;
  715. }
  716. }
  717. if (requestContentType && this.operation.consumes) {
  718. if (this.operation.consumes.indexOf(requestContentType) === -1) {
  719. console.log("server doesn't consume " + requestContentType + ", try " + JSON.stringify(this.operation.consumes));
  720. if (this.requestContentType === null) {
  721. requestContentType = this.operation.consumes[0];
  722. }
  723. } else {
  724. console.log("it's ok to send " + requestContentType);
  725. }
  726. }
  727. responseContentType = null;
  728. if (this.type === "POST" || this.type === "GET") {
  729. if (this.responseContentType) {
  730. responseContentType = this.responseContentType;
  731. } else {
  732. responseContentType = "application/json";
  733. }
  734. } else {
  735. responseContentType = null;
  736. }
  737. if (responseContentType && this.operation.produces) {
  738. if (this.operation.produces.indexOf(responseContentType) === -1) {
  739. console.log("server can't produce " + responseContentType);
  740. } else {
  741. console.log("get ready for " + responseContentType);
  742. }
  743. }
  744. if (requestContentType && requestContentType.indexOf("application/x-www-form-urlencoded") === 0) {
  745. console.log("pulling fields");
  746. fields = {};
  747. possibleParams = (function() {
  748. var _i, _len, _ref, _results;
  749. _ref = this.operation.parameters;
  750. _results = [];
  751. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  752. param = _ref[_i];
  753. if (param.paramType === "form") {
  754. _results.push(param);
  755. }
  756. }
  757. return _results;
  758. }).call(this);
  759. console.log(possibleParams);
  760. values = {};
  761. for (key in possibleParams) {
  762. value = possibleParams[key];
  763. if (this.params[value.name]) {
  764. values[value.name] = this.params[value.name];
  765. }
  766. }
  767. urlEncoded = "";
  768. for (key in values) {
  769. value = values[key];
  770. if (urlEncoded !== "") {
  771. urlEncoded += "&";
  772. }
  773. urlEncoded += encodeURIComponent(key) + '=' + encodeURIComponent(value);
  774. }
  775. body = urlEncoded;
  776. }
  777. if (requestContentType) {
  778. myHeaders["Content-Type"] = requestContentType;
  779. }
  780. if (responseContentType) {
  781. myHeaders["Accept"] = responseContentType;
  782. }
  783. if (!((headers != null) && (headers.mock != null))) {
  784. obj = {
  785. url: this.url,
  786. method: this.type,
  787. headers: myHeaders,
  788. body: body,
  789. on: {
  790. error: function(response) {
  791. return _this.errorCallback(response, _this.params["parent"]);
  792. },
  793. redirect: function(response) {
  794. return _this.successCallback(response, _this.params["parent"]);
  795. },
  796. 307: function(response) {
  797. return _this.successCallback(response, _this.params["parent"]);
  798. },
  799. response: function(response) {
  800. return _this.successCallback(response, _this.params["parent"]);
  801. }
  802. }
  803. };
  804. e = {};
  805. if (typeof window !== 'undefined') {
  806. e = window;
  807. } else {
  808. e = exports;
  809. }
  810. e.authorizations.apply(obj);
  811. if (params.mock == null) {
  812. new SwaggerHttp().execute(obj);
  813. } else {
  814. return obj;
  815. }
  816. }
  817. }
  818. SwaggerRequest.prototype.asCurl = function() {
  819. var header_args, k, v;
  820. header_args = (function() {
  821. var _ref, _results;
  822. _ref = this.headers;
  823. _results = [];
  824. for (k in _ref) {
  825. v = _ref[k];
  826. _results.push("--header \"" + k + ": " + v + "\"");
  827. }
  828. return _results;
  829. }).call(this);
  830. return "curl " + (header_args.join(" ")) + " " + this.url;
  831. };
  832. return SwaggerRequest;
  833. })();
  834. SwaggerHttp = (function() {
  835. SwaggerHttp.prototype.shred = null;
  836. function SwaggerHttp() {
  837. var Shred;
  838. Shred = null;
  839. if (typeof window !== 'undefined') {
  840. Shred = require("./shred");
  841. } else {
  842. Shred = require("shred");
  843. }
  844. this.shred = new Shred();
  845. }
  846. SwaggerHttp.prototype.execute = function(obj) {
  847. var Content, identity, toString,
  848. _this = this;
  849. Content = require("./shred/content");
  850. identity = function(x) {
  851. return x;
  852. };
  853. toString = function(x) {
  854. return x.toString;
  855. };
  856. Content.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], {
  857. parser: identity,
  858. stringify: toString
  859. });
  860. return this.shred.request(obj);
  861. };
  862. return SwaggerHttp;
  863. })();
  864. SwaggerAuthorizations = (function() {
  865. SwaggerAuthorizations.prototype.authz = null;
  866. function SwaggerAuthorizations() {
  867. this.authz = {};
  868. }
  869. SwaggerAuthorizations.prototype.add = function(name, auth) {
  870. this.authz[name] = auth;
  871. return auth;
  872. };
  873. SwaggerAuthorizations.prototype.apply = function(obj) {
  874. var key, value, _ref, _results;
  875. _ref = this.authz;
  876. _results = [];
  877. for (key in _ref) {
  878. value = _ref[key];
  879. _results.push(value.apply(obj));
  880. }
  881. return _results;
  882. };
  883. return SwaggerAuthorizations;
  884. })();
  885. ApiKeyAuthorization = (function() {
  886. ApiKeyAuthorization.prototype.type = null;
  887. ApiKeyAuthorization.prototype.name = null;
  888. ApiKeyAuthorization.prototype.value = null;
  889. function ApiKeyAuthorization(name, value, type) {
  890. this.name = name;
  891. this.value = value;
  892. this.type = type;
  893. }
  894. ApiKeyAuthorization.prototype.apply = function(obj) {
  895. if (this.type === "query") {
  896. if (obj.url.indexOf('?') > 0) {
  897. obj.url = obj.url + "&" + this.name + "=" + this.value;
  898. } else {
  899. obj.url = obj.url + "?" + this.name + "=" + this.value;
  900. }
  901. return true;
  902. } else if (this.type === "header") {
  903. return obj.headers[this.name] = this.value;
  904. }
  905. };
  906. return ApiKeyAuthorization;
  907. })();
  908. this.SwaggerApi = SwaggerApi;
  909. this.SwaggerResource = SwaggerResource;
  910. this.SwaggerOperation = SwaggerOperation;
  911. this.SwaggerRequest = SwaggerRequest;
  912. this.SwaggerModelProperty = SwaggerModelProperty;
  913. this.SwaggerAuthorizations = new SwaggerAuthorizations();
  914. this.ApiKeyAuthorization = ApiKeyAuthorization;
  915. this.authorizations = new SwaggerAuthorizations();
  916. }).call(this);