mail/src/js/service/rest.js

157 lines
4.0 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
var ngModule = angular.module('woServices');
// rest dao for use in the public key service
ngModule.factory('publicKeyRestDao', function(appConfig) {
var dao = new RestDAO();
dao.setBaseUri(appConfig.config.cloudUrl);
return dao;
});
// rest dao for use in the private key service
ngModule.factory('privateKeyRestDao', function(appConfig) {
var dao = new RestDAO();
dao.setBaseUri(appConfig.config.privkeyServerUrl);
return dao;
});
// rest dao for use in the invitation service
ngModule.factory('invitationRestDao', function(appConfig) {
var dao = new RestDAO();
dao.setBaseUri(appConfig.config.cloudUrl);
return dao;
});
// rest dao for use in the admin service
ngModule.factory('adminRestDao', function(appConfig) {
var dao = new RestDAO();
dao.setBaseUri(appConfig.config.adminUrl);
return dao;
});
// rest dao for use in the oauth service
ngModule.factory('oauthRestDao', function() {
var dao = new RestDAO();
dao.setBaseUri('https://www.googleapis.com');
return dao;
});
module.exports = RestDAO;
2014-12-11 12:12:37 -05:00
function RestDAO() {}
2014-10-02 16:05:44 -04:00
2014-11-18 14:19:29 -05:00
/**
* Set the REST DAO's base url
* @param {String} baseUri The base url e.g. https://api.example.com
*/
RestDAO.prototype.setBaseUri = function(baseUri) {
this._baseUri = baseUri;
};
2014-10-02 16:05:44 -04:00
/**
* GET (read) request
* @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.
*/
2014-11-29 10:28:09 -05:00
RestDAO.prototype.get = function(options) {
2014-10-02 16:05:44 -04:00
options.method = 'GET';
2014-11-29 10:28:09 -05:00
return this._processRequest(options);
2014-10-02 16:05:44 -04:00
};
/**
* POST (create) request
*/
2014-11-29 10:28:09 -05:00
RestDAO.prototype.post = function(item, uri) {
return this._processRequest({
2014-10-02 16:05:44 -04:00
method: 'POST',
payload: item,
uri: uri
2014-11-29 10:28:09 -05:00
});
2014-10-02 16:05:44 -04:00
};
/**
* PUT (update) request
*/
2014-11-29 10:28:09 -05:00
RestDAO.prototype.put = function(item, uri) {
return this._processRequest({
2014-10-02 16:05:44 -04:00
method: 'PUT',
payload: item,
uri: uri
2014-11-29 10:28:09 -05:00
});
2014-10-02 16:05:44 -04:00
};
/**
* DELETE (remove) request
*/
2014-11-29 10:28:09 -05:00
RestDAO.prototype.remove = function(uri) {
return this._processRequest({
2014-10-02 16:05:44 -04:00
method: 'DELETE',
uri: uri
2014-11-29 10:28:09 -05:00
});
2014-10-02 16:05:44 -04:00
};
//
// helper functions
//
2014-11-29 10:28:09 -05:00
RestDAO.prototype._processRequest = function(options) {
2014-12-10 11:20:55 -05:00
var self = this;
2014-12-11 12:12:37 -05:00
return new Promise(function(resolve, reject) {
2014-11-29 10:28:09 -05:00
var xhr, format;
if (typeof options.uri === 'undefined') {
throw createError(400, 'Bad Request! URI is a mandatory parameter.');
2014-11-29 10:28:09 -05:00
}
2014-11-29 10:28:09 -05:00
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 {
throw createError(400, 'Bad Request! Unhandled data type.');
}
2014-06-06 12:36:23 -04:00
2014-11-29 10:28:09 -05:00
xhr = new XMLHttpRequest();
2014-12-10 11:20:55 -05:00
xhr.open(options.method, self._baseUri + options.uri);
2014-11-29 10:28:09 -05:00
xhr.setRequestHeader('Accept', format);
xhr.setRequestHeader('Content-Type', format);
2014-06-06 12:36:23 -04:00
2014-11-29 10:28:09 -05:00
xhr.onload = function() {
var res;
if (options.type === 'json') {
try {
res = JSON.parse(xhr.responseText);
} catch(e) {
2014-11-29 10:28:09 -05:00
res = xhr.responseText;
}
} else {
res = xhr.responseText;
}
2014-11-29 10:28:09 -05:00
if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 201 || xhr.status === 304)) {
2014-12-10 11:20:55 -05:00
resolve(res);
2014-11-29 10:28:09 -05:00
return;
}
2014-06-06 12:36:23 -04:00
reject(createError(xhr.status, (res && res.error) || xhr.statusText));
2014-11-29 10:28:09 -05:00
};
xhr.onerror = function() {
reject(createError(42, 'Error calling ' + options.method + ' on ' + options.uri));
2014-11-29 10:28:09 -05:00
};
xhr.send(options.payload ? JSON.stringify(options.payload) : undefined);
});
};
function createError(code, message) {
var error = new Error(message);
error.code = code;
return error;
}