http_client.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * App.HttpClient perform an ajax request
  21. */
  22. App.HttpClient = Em.Object.create({
  23. /**
  24. *
  25. * @param jqXHR
  26. * @param textStatus
  27. * @param errorThrown
  28. * @param url api that invoked this callback function
  29. */
  30. defaultErrorHandler: function (jqXHR, textStatus, errorThrown, url) {
  31. try {
  32. var json = $.parseJSON(jqXHR.responseText);
  33. } catch (err) { }
  34. App.ajax.defaultErrorHandler(jqXHR, url);
  35. if (json) {
  36. Em.assert("HttpClient:", json);
  37. }
  38. else {
  39. if (!$.mocho) { // don't use this assert on tests
  40. Em.assert("HttpClient:", errorThrown);
  41. }
  42. }
  43. },
  44. /**
  45. * @param {string} url
  46. * @param {Object} ajaxOptions
  47. * @param {App.ServerDataMapper} mapper - json processor
  48. * @param {callback} errorHandler
  49. */
  50. request: function (url, ajaxOptions, mapper, errorHandler) {
  51. if (!errorHandler) {
  52. errorHandler = this.defaultErrorHandler;
  53. }
  54. var xhr = new XMLHttpRequest();
  55. var curTime = App.dateTime();
  56. xhr.open('GET', url + (url.indexOf('?') >= 0 ? '&_=' : '?_=') + curTime, true);
  57. xhr.send(null);
  58. this.onReady(xhr, "", ajaxOptions, mapper, errorHandler, url);
  59. },
  60. /**
  61. * Do POST-request equal to GET-request but with some params put to body
  62. * @param {string} url
  63. * @param {{params: string, success: callback, error: callback}} ajaxOptions
  64. * @param {App.QuickDataMapper} mapper
  65. * @param errorHandler
  66. * @method getAsPostRequest
  67. */
  68. getAsPostRequest: function (url, ajaxOptions, mapper, errorHandler) {
  69. if (!errorHandler) {
  70. errorHandler = this.defaultErrorHandler;
  71. }
  72. var xhr = new XMLHttpRequest(),
  73. curTime = App.dateTime(),
  74. params = JSON.stringify({
  75. "RequestInfo": {"query" : ajaxOptions.params }
  76. });
  77. xhr.open('POST', url + (url.indexOf('?') >= 0 ? '&_=' : '?_=') + curTime, true);
  78. xhr.setRequestHeader("X-Http-Method-Override", "GET");
  79. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  80. xhr.send(params);
  81. this.onReady(xhr, "", ajaxOptions, mapper, errorHandler, url);
  82. },
  83. /*
  84. This function checks if we get response from server
  85. Not using onreadystatechange cuz of possible closure
  86. */
  87. onReady: function (xhr, tm, tmp_val, mapper, errorHandler, url) {
  88. var self = this;
  89. clearTimeout(tm);
  90. var timeout = setTimeout(function () {
  91. if (xhr.readyState == 4) {
  92. if (xhr.status == 200) {
  93. try {
  94. App.store.commit();
  95. } catch (err) {
  96. console.warn('App.store.commit error:', err);
  97. }
  98. mapper.map($.parseJSON(xhr.responseText));
  99. tmp_val.complete.call(self);
  100. xhr.abort();
  101. } else {
  102. errorHandler(xhr, "error", xhr.statusText, url);
  103. }
  104. tmp_val = null;
  105. xhr = null;
  106. clearTimeout(timeout);
  107. timeout = null;
  108. }
  109. else {
  110. self.onReady(xhr, timeout, tmp_val, mapper, errorHandler, url);
  111. }
  112. }, 10);
  113. },
  114. /**
  115. * @param {string} url
  116. * @param {App.ServerDataMapper} mapper - json processor
  117. * @param {Object} data - ajax data property
  118. * @param {callback} errorHandler
  119. * @param {number} interval - frequency request
  120. */
  121. get: function (url, mapper, data, errorHandler, interval) {
  122. if (!errorHandler && data.error) {
  123. errorHandler = data.error;
  124. }
  125. var client = this,
  126. request = function () {
  127. if (data.doGetAsPost && !App.get('testMode')) {
  128. client.getAsPostRequest(url, data, mapper, errorHandler);
  129. }
  130. else {
  131. client.request(url, data, mapper, errorHandler);
  132. }
  133. url = null;
  134. data = null;
  135. mapper = null;
  136. errorHandler = null;
  137. };
  138. interval = "" + interval;
  139. if (interval.match(/\d+/)) {
  140. $.periodic({period: interval}, request);
  141. }
  142. else {
  143. request();
  144. }
  145. },
  146. /**
  147. * @param {string} url
  148. * @param {Object} data - ajax data property
  149. * @param {App.ServerDataMapper} mapper - json processor
  150. * @param {callback} errorHandler
  151. * @param {number} interval - frequecy request
  152. */
  153. post: function (url, data, mapper, errorHandler, interval) {
  154. this.get(url, data, mapper, errorHandler, interval);
  155. }
  156. });