From 99c2f24d7d1c1444f09120f0e3b8c75de037dfb8 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Sat, 29 Nov 2014 16:28:09 +0100 Subject: [PATCH] Refactor rest dao to use promises --- src/js/service/rest.js | 136 +++++++++++++++++++++-------------------- 1 file changed, 70 insertions(+), 66 deletions(-) diff --git a/src/js/service/rest.js b/src/js/service/rest.js index 82e8be6..393a1f2 100644 --- a/src/js/service/rest.js +++ b/src/js/service/rest.js @@ -39,7 +39,9 @@ ngModule.factory('oauthRestDao', function() { module.exports = RestDAO; -function RestDAO() {} +function RestDAO($q) { + this._q = $q; +} /** * Set the REST DAO's base url @@ -54,105 +56,107 @@ RestDAO.prototype.setBaseUri = function(baseUri) { * @param {String} options.uri URI relative to the base uri to perform the GET request with. * @param {String} options.type (optional) The type of data that you're expecting back from the server: json, xml, text. Default: json. */ -RestDAO.prototype.get = function(options, callback) { +RestDAO.prototype.get = function(options) { options.method = 'GET'; - this._processRequest(options, callback); + return this._processRequest(options); }; /** * POST (create) request */ -RestDAO.prototype.post = function(item, uri, callback) { - this._processRequest({ +RestDAO.prototype.post = function(item, uri) { + return this._processRequest({ method: 'POST', payload: item, uri: uri - }, callback); + }); }; /** * PUT (update) request */ -RestDAO.prototype.put = function(item, uri, callback) { - this._processRequest({ +RestDAO.prototype.put = function(item, uri) { + return this._processRequest({ method: 'PUT', payload: item, uri: uri - }, callback); + }); }; /** * DELETE (remove) request */ -RestDAO.prototype.remove = function(uri, callback) { - this._processRequest({ +RestDAO.prototype.remove = function(uri) { + return this._processRequest({ method: 'DELETE', uri: uri - }, callback); + }); }; // // helper functions // -RestDAO.prototype._processRequest = function(options, callback) { - var xhr, format; +RestDAO.prototype._processRequest = function(options) { + return this._q(function(resolve, reject) { + var xhr, format; - if (typeof options.uri === 'undefined') { - callback({ - code: 400, - errMsg: 'Bad Request! URI is a mandatory parameter.' - }); - return; - } - - options.type = options.type || 'json'; - - if (options.type === 'json') { - format = 'application/json'; - } else if (options.type === 'xml') { - format = 'application/xml'; - } else if (options.type === 'text') { - format = 'text/plain'; - } else { - callback({ - code: 400, - errMsg: 'Bad Request! Unhandled data type.' - }); - return; - } - - xhr = new XMLHttpRequest(); - xhr.open(options.method, this._baseUri + options.uri); - xhr.setRequestHeader('Accept', format); - xhr.setRequestHeader('Content-Type', format); - - xhr.onload = function() { - var res; - - if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 201 || xhr.status === 304)) { - if (options.type === 'json') { - res = xhr.responseText ? JSON.parse(xhr.responseText) : xhr.responseText; - } else { - res = xhr.responseText; - } - - callback(null, res, xhr.status); + if (typeof options.uri === 'undefined') { + reject({ + code: 400, + message: 'Bad Request! URI is a mandatory parameter.' + }); return; } - callback({ - code: xhr.status, - errMsg: xhr.statusText - }); - }; + options.type = options.type || 'json'; - xhr.onerror = function() { - callback({ - code: 42, - errMsg: 'Error calling ' + options.method + ' on ' + options.uri - }); - }; + if (options.type === 'json') { + format = 'application/json'; + } else if (options.type === 'xml') { + format = 'application/xml'; + } else if (options.type === 'text') { + format = 'text/plain'; + } else { + reject({ + code: 400, + message: 'Bad Request! Unhandled data type.' + }); + return; + } - xhr.send(options.payload ? JSON.stringify(options.payload) : undefined); + xhr = new XMLHttpRequest(); + xhr.open(options.method, this._baseUri + options.uri); + xhr.setRequestHeader('Accept', format); + xhr.setRequestHeader('Content-Type', format); + + xhr.onload = function() { + var res; + + if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 201 || xhr.status === 304)) { + if (options.type === 'json') { + res = xhr.responseText ? JSON.parse(xhr.responseText) : xhr.responseText; + } else { + res = xhr.responseText; + } + + resolve(res, xhr.status); + return; + } + + reject({ + code: xhr.status, + message: xhr.statusText + }); + }; + + xhr.onerror = function() { + reject({ + code: 42, + message: 'Error calling ' + options.method + ' on ' + options.uri + }); + }; + + xhr.send(options.payload ? JSON.stringify(options.payload) : undefined); + }); }; \ No newline at end of file