http_client.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. */
  29. defaultErrorHandler: function (jqXHR, textStatus, errorThrown) {
  30. var json = $.parseJSON(jqXHR.responseText);
  31. if (json) {
  32. Em.assert("HttpClient:", json);
  33. } else {
  34. Em.assert("HttpClient:", errorThrown);
  35. }
  36. },
  37. /**
  38. * @param {string} url
  39. * @param {Object} ajaxOptions
  40. * @param {App.ServerDataMapper} mapper - json processor
  41. * @param {function} errorHandler
  42. * @param {number} interval - frequecy request
  43. */
  44. request: function (url, ajaxOptions, mapper, errorHandler) {
  45. if (!errorHandler) {
  46. errorHandler = this.defaultErrorHandler;
  47. }
  48. var options = {dataType: 'json'}; // default ajax options;
  49. $.extend(options, {
  50. url: url,
  51. success: function (data) {
  52. mapper.map(data);
  53. },
  54. error: errorHandler
  55. });
  56. $.extend(options, ajaxOptions);
  57. $.ajax(options);
  58. },
  59. /**
  60. * @param {string} url
  61. * @param {App.ServerDataMapper} mapper - json processor
  62. * @param {Object} data - ajax data property
  63. * @param {function} errorHandler
  64. * @param {number} interval - frequecy request
  65. */
  66. get: function (url, mapper, data, errorHandler, interval) {
  67. var client = this;
  68. var request = function () {
  69. client.request(url, data, mapper, errorHandler);
  70. }
  71. interval = "" + interval;
  72. if (interval.match(/\d+/)) {
  73. $.periodic({period: interval}, request);
  74. } else {
  75. request();
  76. }
  77. },
  78. /**
  79. * @param {string} url
  80. * @param {Object} data - ajax data property
  81. * @param {App.ServerDataMapper} mapper - json processor
  82. * @param {function} errorHandler
  83. * @param {number} interval - frequecy request
  84. */
  85. post: function (url, data, mapper, errorHandler, interval) {
  86. this.get(url, data, mapper, errorHandler, interval);
  87. }
  88. // not realized yet
  89. // put:function (url, mapper, errorHandler) {
  90. // this.request(url, {}, mapper, errorHandler);
  91. // },
  92. //
  93. // delete:function (url, mapper, errorHandler) {
  94. // this.request(url, {}, mapper, errorHandler);
  95. // }
  96. });
  97. /*App.HttpClient.get(
  98. 'http://nagiosserver/hdp/nagios/nagios_alerts.php?q1=alerts&alert_type=all',
  99. App.alertsMapper,
  100. { dataType: 'jsonp', jsonp: 'jsonp' }
  101. );*/