ajax.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. * Config for each ajax-request
  20. *
  21. * Fields example:
  22. * mock - testMode url
  23. * real - real url (without API prefix)
  24. * type - request type (also may be defined in the format method)
  25. * format - function for processing ajax params after default formatRequest. May be called with one or two parameters (data, opt). Return ajax-params object
  26. * testInProduction - can this request be executed on production tests (used only in tests)
  27. *
  28. * @type {Object}
  29. */
  30. var urls = {
  31. 'mapper.applicationTypes': {
  32. real: 'apptypes?fields=*',
  33. mock: '/data/apptypes/all_fields.json',
  34. headers: {
  35. Accept : "text/plain; charset=utf-8",
  36. "Content-Type": "text/plain; charset=utf-8"
  37. }
  38. },
  39. 'mapper.applicationApps': {
  40. real: 'apps/?fields=*',
  41. mock: '/data/apps/apps.json',
  42. headers: {
  43. Accept : "text/plain; charset=utf-8",
  44. "Content-Type": "text/plain; charset=utf-8"
  45. }
  46. },
  47. 'mapper.applicationStatus': {
  48. real: 'resources/status',
  49. mock: '/data/resource/status_true.json'
  50. },
  51. 'saveInitialValues': {
  52. real: '',
  53. headers: {
  54. "Content-Type": "text/plain; charset=utf-8"
  55. },
  56. format: function(data) {
  57. return {
  58. type: 'PUT',
  59. data: JSON.stringify(data.data)
  60. }
  61. }
  62. },
  63. 'createNewApp': {
  64. real: 'apps',
  65. mock: '',
  66. headers: {
  67. "Content-Type": "text/plain; charset=utf-8"
  68. },
  69. format: function(data) {
  70. return {
  71. type: 'POST',
  72. data: JSON.stringify(data.data)
  73. }
  74. }
  75. },
  76. 'destroyApp': {
  77. real: 'apps/{id}',
  78. mock: '',
  79. format: function() {
  80. return {
  81. method: 'DELETE'
  82. }
  83. }
  84. },
  85. 'changeAppState': {
  86. real: 'apps/{id}',
  87. mock: '',
  88. headers: {
  89. "Content-Type": "text/plain; charset=utf-8"
  90. },
  91. format: function(data) {
  92. return {
  93. method: 'PUT',
  94. data: JSON.stringify(data.data)
  95. }
  96. }
  97. },
  98. 'flexApp': {
  99. real: 'apps/{id}',
  100. mock: '',
  101. headers: {
  102. "Content-Type": "text/plain; charset=utf-8"
  103. },
  104. format: function(data) {
  105. return {
  106. method: 'PUT',
  107. data: JSON.stringify(data.data)
  108. }
  109. }
  110. },
  111. 'service_status': {
  112. real: 'clusters/{clusterName}/services?fields=ServiceInfo/state&minimal_response=true',
  113. mock:'/data/resource/service_status.json',
  114. headers: {
  115. Accept : "text/plain; charset=utf-8",
  116. "Content-Type": "text/plain; charset=utf-8"
  117. }
  118. },
  119. 'components_hosts': {
  120. real: 'clusters/{clusterName}/hosts?host_components/HostRoles/component_name={componentName}&minimal_response=true',
  121. mock:'/data/resource/components_hosts.json',
  122. headers: {
  123. Accept : "text/plain; charset=utf-8",
  124. "Content-Type": "text/plain; charset=utf-8"
  125. }
  126. },
  127. 'service_current_configs': {
  128. real: 'clusters/{clusterName}/configurations/service_config_versions?service_name={serviceName}&is_current=true',
  129. mock: '/data/resource/service_configs.json',
  130. headers: {
  131. Accept : "text/plain; charset=utf-8",
  132. "Content-Type": "text/plain; charset=utf-8"
  133. }
  134. },
  135. 'config.tags': {
  136. 'real': 'clusters/{clusterName}?fields=Clusters/desired_configs',
  137. headers: {
  138. Accept : "text/plain; charset=utf-8",
  139. "Content-Type": "text/plain; charset=utf-8"
  140. }
  141. },
  142. 'get_all_configurations': {
  143. 'real': 'clusters/{clusterName}/configurations?{urlParams}',
  144. headers: {
  145. Accept : "text/plain; charset=utf-8",
  146. "Content-Type": "text/plain; charset=utf-8"
  147. }
  148. },
  149. 'cluster_name': {
  150. real: 'clusters',
  151. mock:'/data/resource/cluster_name.json',
  152. headers: {
  153. Accept : "text/plain; charset=utf-8",
  154. "Content-Type": "text/plain; charset=utf-8"
  155. }
  156. },
  157. 'metrics': {
  158. real: 'apps/{id}/metrics/{metric}',
  159. mock: '/data/metrics/metric.json'
  160. },
  161. 'metrics2': {
  162. real: 'apps/{id}/metrics/{metric}',
  163. mock: '/data/metrics/metric2.json'
  164. },
  165. 'metrics3': {
  166. real: 'apps/{id}/metrics/{metric}',
  167. mock: '/data/metrics/metric3.json'
  168. },
  169. 'metrics4': {
  170. real: 'apps/{id}/metrics/{metric}',
  171. mock: '/data/metrics/metric4.json'
  172. }
  173. };
  174. /**
  175. * Replace data-placeholders to its values
  176. *
  177. * @param {String} url
  178. * @param {Object} data
  179. * @return {String}
  180. */
  181. var formatUrl = function (url, data) {
  182. if (!url) return null;
  183. var keys = url.match(/\{\w+\}/g);
  184. keys = (keys === null) ? [] : keys;
  185. if (keys) {
  186. keys.forEach(function (key) {
  187. var raw_key = key.substr(1, key.length - 2);
  188. var replace;
  189. if (!data || !data[raw_key]) {
  190. replace = '';
  191. }
  192. else {
  193. replace = data[raw_key];
  194. }
  195. url = url.replace(new RegExp(key, 'g'), replace);
  196. });
  197. }
  198. return url;
  199. };
  200. /**
  201. * this = object from config
  202. * @return {Object}
  203. */
  204. var formatRequest = function (data) {
  205. var opt = {
  206. type: this.type || 'GET',
  207. dataType: 'json',
  208. async: true,
  209. headers: this.headers || {Accept: "application/json; charset=utf-8"}
  210. };
  211. if (App.get('testMode')) {
  212. opt.url = formatUrl(this.mock ? this.mock : '', data);
  213. opt.type = 'GET';
  214. }
  215. else {
  216. var prefix = App.get('urlPrefix');
  217. if(Em.get(data, 'urlPrefix')){
  218. var prefix = Em.get(data, 'urlPrefix');
  219. }
  220. opt.url = prefix + (formatUrl(this.real, data) ? formatUrl(this.real, data) : "");
  221. }
  222. if (this.format) {
  223. jQuery.extend(opt, this.format(data, opt));
  224. }
  225. return opt;
  226. };
  227. /**
  228. * Wrapper for all ajax requests
  229. *
  230. * @type {Object}
  231. */
  232. var ajax = Em.Object.extend({
  233. /**
  234. * Send ajax request
  235. *
  236. * @param {Object} config
  237. * @return {$.ajax} jquery ajax object
  238. *
  239. * config fields:
  240. * name - url-key in the urls-object *required*
  241. * sender - object that send request (need for proper callback initialization) *required*
  242. * data - object with data for url-format
  243. * beforeSend - method-name for ajax beforeSend response callback
  244. * success - method-name for ajax success response callback
  245. * error - method-name for ajax error response callback
  246. * callback - callback from <code>App.updater.run</code> library
  247. */
  248. send: function (config) {
  249. Ember.assert('Ajax sender should be defined!', config.sender);
  250. Ember.assert('Invalid config.name provided - ' + config.name, urls[config.name]);
  251. var opt = {};
  252. // default parameters
  253. var params = {
  254. clusterName: App.get('clusterName')
  255. };
  256. if (config.data) {
  257. jQuery.extend(params, config.data);
  258. }
  259. opt = formatRequest.call(urls[config.name], params);
  260. opt.context = this;
  261. // object sender should be provided for processing beforeSend, success, error and complete responses
  262. opt.beforeSend = function (xhr) {
  263. if (config.beforeSend) {
  264. config.sender[config.beforeSend](opt, xhr, params);
  265. }
  266. };
  267. opt.success = function (data) {
  268. console.log("TRACE: The url is: " + opt.url);
  269. if (config.success) {
  270. config.sender[config.success](data, opt, params);
  271. }
  272. };
  273. opt.error = function (request, ajaxOptions, error) {
  274. if (config.error) {
  275. config.sender[config.error](request, ajaxOptions, error, opt, params);
  276. }
  277. };
  278. opt.complete = function (xhr, status) {
  279. if (config.complete) {
  280. config.sender[config.complete](xhr, status);
  281. }
  282. };
  283. return $.ajax(opt);
  284. }
  285. });
  286. App.ajax = ajax.create({});