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.
 
 
 
 

1075 rivejä
35 KiB

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