2014-08-31 23:22:56 +02:00
|
|
|
var App = App || {};
|
|
|
|
|
2014-10-10 18:13:46 +02:00
|
|
|
App.API = function(_, jQuery, promise, appState) {
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
var baseUrl = '/api/';
|
2014-10-18 10:31:01 +02:00
|
|
|
var AJAX_UNSENT = 0;
|
|
|
|
var AJAX_OPENED = 1;
|
|
|
|
var AJAX_HEADERS_RECEIVED = 2;
|
|
|
|
var AJAX_LOADING = 3;
|
|
|
|
var AJAX_DONE = 4;
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
function get(url, data) {
|
|
|
|
return request('GET', url, data);
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
function post(url, data) {
|
|
|
|
return request('POST', url, data);
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
function put(url, data) {
|
|
|
|
return request('PUT', url, data);
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
function _delete(url, data) {
|
|
|
|
return request('DELETE', url, data);
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
function request(method, url, data) {
|
|
|
|
var fullUrl = baseUrl + '/' + url;
|
|
|
|
fullUrl = fullUrl.replace(/\/{2,}/, '/');
|
|
|
|
|
2014-10-18 10:31:01 +02:00
|
|
|
var xhr = null;
|
|
|
|
var apiPromise = promise.make(function(resolve, reject) {
|
|
|
|
xhr = jQuery.ajax({
|
2014-09-04 19:21:18 +02:00
|
|
|
headers: {
|
|
|
|
'X-Authorization-Token': appState.get('loginToken') || '',
|
|
|
|
},
|
2014-08-31 23:22:56 +02:00
|
|
|
success: function(data, textStatus, xhr) {
|
|
|
|
resolve({
|
|
|
|
status: xhr.status,
|
2014-10-10 18:13:46 +02:00
|
|
|
json: stripMeta(data)});
|
2014-08-31 23:22:56 +02:00
|
|
|
},
|
|
|
|
error: function(xhr, textStatus, errorThrown) {
|
|
|
|
reject({
|
|
|
|
status: xhr.status,
|
2014-09-08 22:02:28 +02:00
|
|
|
json: xhr.responseJSON ?
|
2014-10-10 18:13:46 +02:00
|
|
|
stripMeta(xhr.responseJSON) :
|
2014-09-08 22:02:28 +02:00
|
|
|
{error: errorThrown}});
|
2014-08-31 23:22:56 +02:00
|
|
|
},
|
|
|
|
type: method,
|
|
|
|
url: fullUrl,
|
|
|
|
data: data,
|
|
|
|
});
|
|
|
|
});
|
2014-10-18 10:31:01 +02:00
|
|
|
apiPromise.xhr = xhr;
|
|
|
|
return apiPromise;
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
2014-10-10 18:13:46 +02:00
|
|
|
function stripMeta(data) {
|
|
|
|
var result = {};
|
|
|
|
_.each(data, function(v, k) {
|
|
|
|
if (!k.match(/^__/)) {
|
|
|
|
result[k] = v;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-08-31 23:22:56 +02:00
|
|
|
return {
|
|
|
|
get: get,
|
|
|
|
post: post,
|
|
|
|
put: put,
|
2014-10-18 10:31:01 +02:00
|
|
|
delete: _delete,
|
|
|
|
|
|
|
|
AJAX_UNSENT: AJAX_UNSENT,
|
|
|
|
AJAX_OPENED: AJAX_OPENED,
|
|
|
|
AJAX_HEADERS_RECEIVED: AJAX_HEADERS_RECEIVED,
|
|
|
|
AJAX_LOADING: AJAX_LOADING,
|
|
|
|
AJAX_DONE: AJAX_DONE,
|
2014-08-31 23:22:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-10-10 18:13:46 +02:00
|
|
|
App.DI.registerSingleton('api', ['_', 'jQuery', 'promise', 'appState'], App.API);
|