|
| 1 | +/* |
| 2 | + * Copyright 2014 Samit Badle |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * HTTP service for Firefox addons |
| 19 | + * Do not instantiate |
| 20 | + */ |
| 21 | +function HTTP() { |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Make a generic http request |
| 26 | + * |
| 27 | + * @param method GET, POST, DELETE, PUT or any other http request |
| 28 | + * @param url |
| 29 | + * @param {string|object} [data] string or object, object is converted to json and sets the Content-Type header |
| 30 | + * @param {Object.<string, string>} [headers] hash with keys containing header names and values containing its value |
| 31 | + * @param {function(string, string, string)} [callback] a callback function that takes response, success, status. If callback is not given, |
| 32 | + * a deferred object is created. If deferred.js is not loaded, an exception will occur. |
| 33 | + * @returns {Deferred} if a deferred has been created |
| 34 | + */ |
| 35 | +HTTP.request = function(method, url, data, headers, callback) { |
| 36 | + var deferred; |
| 37 | + if (!callback) { |
| 38 | + deferred = new Deferred(); |
| 39 | + callback = function(response, success, status) { |
| 40 | + if (success) { |
| 41 | + deferred.resolve(response, success, status); |
| 42 | + } else { |
| 43 | + deferred.reject(response, success, status); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + } |
| 48 | + var httpRequest = new XMLHttpRequest(); |
| 49 | + //LOG.debug('Executing: ' + method + " " + url); |
| 50 | + httpRequest.open(method, url); |
| 51 | + httpRequest.onreadystatechange = function() { |
| 52 | + try { |
| 53 | + if (httpRequest.readyState === 4) { |
| 54 | + if (httpRequest.status === 200 || (httpRequest.status > 200 && httpRequest.status < 300)) { |
| 55 | + callback(httpRequest.responseText, true, httpRequest.status); |
| 56 | + } else if (httpRequest.status === 500 ) { |
| 57 | + callback(httpRequest.responseText, false, httpRequest.status); |
| 58 | + } else { |
| 59 | + //TODO eliminate alert and signal the failure |
| 60 | +// alert('There was a problem with the request.\nUrl: ' + url + '\nHttp Status: ' + httpRequest.status + "\nResponse: " + httpRequest.responseText); |
| 61 | + LOG.debug('Error: There was a problem with the request.\nUrl: ' + url + '\nHttp Status: ' + httpRequest.status + "\nResponse: " + httpRequest.responseText); |
| 62 | + callback(null, false, httpRequest.status); |
| 63 | + } |
| 64 | + } |
| 65 | + } catch(e) { |
| 66 | + //TODO eliminate alert and signal the failure, typically when callback is not given and Deferred is not loaded |
| 67 | + alert('Caught Exception in HTTP.request: ' + e); |
| 68 | + throw e; |
| 69 | + } |
| 70 | + }; |
| 71 | + //httpRequest.channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE; |
| 72 | + if (data && typeof data !== 'string') { |
| 73 | + data = JSON.stringify(data); |
| 74 | + //do this before you set custom headers, so that user supplied headers will overwrite this |
| 75 | + httpRequest.setRequestHeader('Content-Type', 'application/json; charset=utf-8'); |
| 76 | + } |
| 77 | + if (headers) { |
| 78 | + for (var header in headers) { |
| 79 | + httpRequest.setRequestHeader(header, headers[header] + ''); |
| 80 | + } |
| 81 | + } |
| 82 | + if (data) { |
| 83 | + httpRequest.send(data); |
| 84 | + } else { |
| 85 | + httpRequest.send(); |
| 86 | + } |
| 87 | + return deferred; |
| 88 | +}; |
| 89 | + |
| 90 | +/** |
| 91 | + * Shortcut method to create HTTP POST requests. See HTTP.request() for more details. |
| 92 | + * |
| 93 | + * @param url |
| 94 | + * @param {string|object} [data] string or object, object is converted to json and sets the Content-Type header |
| 95 | + * @param {Object.<string, string>} [headers] hash with keys containing header names and values containing its value |
| 96 | + * @param {function(string, string, string)} [callback] a callback function that takes response, success, status. If callback is not given, |
| 97 | + * a deferred object is created. If deferred.js is not loaded, an exception occurs. |
| 98 | + * @returns {Deferred} if a deferred has been created |
| 99 | + */ |
| 100 | +HTTP.post = function(url, data, headers, callback) { |
| 101 | + return this.request('POST', url, data, headers, callback); |
| 102 | +}; |
| 103 | + |
| 104 | +/** |
| 105 | + * Shortcut method to create HTTP GET requests. See HTTP.request() for more details. |
| 106 | + * |
| 107 | + * @param url |
| 108 | + * @param {Object.<string, string>} [headers] hash with keys containing header names and values containing its value |
| 109 | + * @param {function(string, string, string)} [callback] a callback function that takes response, success, status. If callback is not given, |
| 110 | + * a deferred object is created. If deferred.js is not loaded, an exception occurs. |
| 111 | + * @returns {Deferred} if a deferred has been created |
| 112 | + */ |
| 113 | +HTTP.get = function (url, headers, callback) { |
| 114 | + return this.request('GET', url, null, headers, callback); |
| 115 | +}; |
| 116 | + |
0 commit comments