http_client.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 {boolean} isGetAsPost - if true, do POST-request equal to GET-request but with some params put to body
  49. * @param {callback} errorHandler
  50. */
  51. request: function (url, ajaxOptions, mapper, errorHandler, isGetAsPost) {
  52. if (!errorHandler) {
  53. errorHandler = this.defaultErrorHandler;
  54. }
  55. var xhr = new XMLHttpRequest(),
  56. curTime = App.dateTime(),
  57. method = isGetAsPost ? 'POST': 'GET',
  58. params = isGetAsPost ? JSON.stringify({
  59. "RequestInfo": {"query" : ajaxOptions.params}
  60. }) : null;
  61. xhr.open(method, url + (url.indexOf('?') >= 0 ? '&_=' : '?_=') + curTime, true);
  62. if (isGetAsPost) {
  63. xhr.setRequestHeader("X-Http-Method-Override", "GET");
  64. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  65. }
  66. xhr.send(params);
  67. this.onReady(xhr, "", ajaxOptions, mapper, errorHandler, url);
  68. },
  69. /**
  70. This function checks if we get response from server
  71. Not using onreadystatechange cuz of possible closure
  72. */
  73. onReady: function (xhr, tm, tmp_val, mapper, errorHandler, url) {
  74. var self = this;
  75. clearTimeout(tm);
  76. var timeout = setTimeout(function () {
  77. if (xhr.readyState == 4) {
  78. if (xhr.status == 200) {
  79. try {
  80. App.store.commit();
  81. } catch (err) {
  82. console.warn('App.store.commit error:', err);
  83. }
  84. mapper.map($.parseJSON(xhr.responseText));
  85. tmp_val.complete.call(self);
  86. xhr.abort();
  87. } else {
  88. errorHandler(xhr, "error", xhr.statusText, url);
  89. }
  90. tmp_val = null;
  91. xhr = null;
  92. clearTimeout(timeout);
  93. timeout = null;
  94. }
  95. else {
  96. self.onReady(xhr, timeout, tmp_val, mapper, errorHandler, url);
  97. }
  98. }, 10);
  99. },
  100. /**
  101. * @param {string} url
  102. * @param {App.ServerDataMapper} mapper - json processor
  103. * @param {Object} data - ajax data property
  104. * @param {callback} errorHandler
  105. * @param {number} interval - frequency request
  106. */
  107. get: function (url, mapper, data, errorHandler, interval) {
  108. if (!errorHandler && data.error) {
  109. errorHandler = data.error;
  110. }
  111. var client = this,
  112. request = function () {
  113. var isGetAsPost = Boolean(data.doGetAsPost && !App.get('testMode'));
  114. client.request(url, data, mapper, errorHandler, isGetAsPost);
  115. url = null;
  116. data = null;
  117. mapper = null;
  118. errorHandler = null;
  119. };
  120. interval = "" + interval;
  121. if (interval.match(/\d+/)) {
  122. $.periodic({period: interval}, request);
  123. }
  124. else {
  125. request();
  126. }
  127. },
  128. /**
  129. * @param {string} url
  130. * @param {Object} data - ajax data property
  131. * @param {App.ServerDataMapper} mapper - json processor
  132. * @param {callback} errorHandler
  133. * @param {number} interval - frequency request
  134. */
  135. post: function (url, data, mapper, errorHandler, interval) {
  136. this.get.apply(this, arguments);
  137. }
  138. });