Refactor rest dao to use promises

This commit is contained in:
Tankred Hase 2014-11-29 16:28:09 +01:00
parent ce07f30252
commit 99c2f24d7d
1 changed files with 70 additions and 66 deletions

View File

@ -39,7 +39,9 @@ ngModule.factory('oauthRestDao', function() {
module.exports = RestDAO; module.exports = RestDAO;
function RestDAO() {} function RestDAO($q) {
this._q = $q;
}
/** /**
* Set the REST DAO's base url * 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.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. * @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'; options.method = 'GET';
this._processRequest(options, callback); return this._processRequest(options);
}; };
/** /**
* POST (create) request * POST (create) request
*/ */
RestDAO.prototype.post = function(item, uri, callback) { RestDAO.prototype.post = function(item, uri) {
this._processRequest({ return this._processRequest({
method: 'POST', method: 'POST',
payload: item, payload: item,
uri: uri uri: uri
}, callback); });
}; };
/** /**
* PUT (update) request * PUT (update) request
*/ */
RestDAO.prototype.put = function(item, uri, callback) { RestDAO.prototype.put = function(item, uri) {
this._processRequest({ return this._processRequest({
method: 'PUT', method: 'PUT',
payload: item, payload: item,
uri: uri uri: uri
}, callback); });
}; };
/** /**
* DELETE (remove) request * DELETE (remove) request
*/ */
RestDAO.prototype.remove = function(uri, callback) { RestDAO.prototype.remove = function(uri) {
this._processRequest({ return this._processRequest({
method: 'DELETE', method: 'DELETE',
uri: uri uri: uri
}, callback); });
}; };
// //
// helper functions // helper functions
// //
RestDAO.prototype._processRequest = function(options, callback) { RestDAO.prototype._processRequest = function(options) {
var xhr, format; return this._q(function(resolve, reject) {
var xhr, format;
if (typeof options.uri === 'undefined') { if (typeof options.uri === 'undefined') {
callback({ reject({
code: 400, code: 400,
errMsg: 'Bad Request! URI is a mandatory parameter.' message: '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);
return; return;
} }
callback({ options.type = options.type || 'json';
code: xhr.status,
errMsg: xhr.statusText
});
};
xhr.onerror = function() { if (options.type === 'json') {
callback({ format = 'application/json';
code: 42, } else if (options.type === 'xml') {
errMsg: 'Error calling ' + options.method + ' on ' + options.uri 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);
});
}; };