ajax.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. var App = require('app');
  19. /**
  20. * Config for each ajax-request
  21. *
  22. * Fields example:
  23. * mock - testMode url
  24. * real - real url (without API prefix)
  25. * type - request type (also may be defined in the format method)
  26. * format - function for processing ajax params after default formatRequest. Return ajax-params object
  27. * testInProduction - can this request be executed on production tests (used only in tests)
  28. *
  29. * @type {Object}
  30. */
  31. var urls = {
  32. 'background_operations': {
  33. 'mock': '/data/background_operations/list_on_start.json',
  34. 'real': '/clusters/{clusterName}/requests/?fields=tasks/*',
  35. 'testInProduction': true
  36. },
  37. 'background_operations.update_task': {
  38. 'mock': '/data/background_operations/one_task.json',
  39. 'real': '/clusters/{clusterName}/requests/{requestId}/tasks/{taskId}',
  40. 'testInProduction': true
  41. },
  42. 'service.item.start_stop': {
  43. 'mock': '/data/wizard/deploy/poll_1.json',
  44. 'real': '/clusters/{clusterName}/services/{serviceName}',
  45. 'format': function (data, opt) {
  46. return {
  47. type: 'PUT',
  48. data: JSON.stringify({
  49. ServiceInfo: {
  50. state: data.state
  51. }
  52. })
  53. };
  54. }
  55. },
  56. 'service.item.smoke': {
  57. 'mock': '/data/wizard/deploy/poll_1.json',
  58. 'real': '/clusters/{clusterName}/services/{serviceName}/actions/{serviceName}_SERVICE_CHECK',
  59. 'type': 'POST'
  60. }
  61. };
  62. /**
  63. * Replace data-placeholders to its values
  64. *
  65. * @param {String} url
  66. * @param {Object} data
  67. * @return {String}
  68. */
  69. var formatUrl = function(url, data) {
  70. var keys = url.match(/\{\w+\}/g);
  71. keys.forEach(function(key){
  72. var raw_key = key.substr(1, key.length - 2);
  73. var replace;
  74. if (!data[raw_key]) {
  75. replace = '';
  76. }
  77. else {
  78. replace = data[raw_key];
  79. }
  80. url = url.replace(new RegExp(key, 'g'), replace);
  81. });
  82. return url;
  83. };
  84. /**
  85. * this = object from config
  86. * @return {Object}
  87. */
  88. var formatRequest = function(data) {
  89. var opt = {
  90. type : this.type || 'GET',
  91. timeout : App.timeout,
  92. dataType: 'json',
  93. statusCode: require('data/statusCodes')
  94. };
  95. if(App.testMode) {
  96. opt.url = this.mock;
  97. }
  98. else {
  99. opt.url = App.apiPrefix + formatUrl(this.real, data);
  100. }
  101. if(this.format) {
  102. jQuery.extend(opt, this.format(data, opt));
  103. }
  104. return opt;
  105. };
  106. /**
  107. * Wrapper for all ajax requests
  108. *
  109. * @type {Object}
  110. */
  111. App.ajax = {
  112. /**
  113. * Send ajax request
  114. *
  115. * @param {Object} config
  116. * @return Object jquery ajax object
  117. *
  118. * config fields:
  119. * name - url-key in the urls-object *required*
  120. * sender - object that send request (need for proper callback initialization) *required*
  121. * data - object with data for url-format
  122. * success - method-name for ajax success response callback
  123. * error - method-name for ajax error response callback
  124. * callback - callback from <code>App.updater.run</code> library
  125. */
  126. send: function(config) {
  127. if (!config.sender) {
  128. console.warn('Ajax sender should be defined!');
  129. return null;
  130. }
  131. // default parameters
  132. var params = {
  133. clusterName: App.get('clusterName')
  134. };
  135. // extend default parameters with provided
  136. if (config.data) {
  137. jQuery.extend(params, config.data);
  138. }
  139. var opt = {};
  140. opt = formatRequest.call(urls[config.name], params);
  141. // object sender should be provided for processing success and error responses
  142. opt.success = function(data) {
  143. if(config.success) {
  144. config.sender[config.success](data, opt);
  145. }
  146. };
  147. opt.error = function(request, ajaxOptions, error) {
  148. if (config.error) {
  149. config.sender[config.error](request, ajaxOptions, error, opt);
  150. }
  151. };
  152. opt.complete = function(){
  153. if(config.callback){
  154. config.callback();
  155. }
  156. };
  157. if($.mocho){
  158. opt.url = 'http://' + $.hostName + opt.url;
  159. }
  160. return $.ajax(opt);
  161. }
  162. }