Skip to content

Commit

Permalink
connection: Better handling of requests
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud committed Nov 25, 2016
1 parent 3dda99c commit 20aae5b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 53 deletions.
21 changes: 7 additions & 14 deletions src/client/javascript/core/authenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,13 @@
* @param {CallbackHandler} callback Callback function
*/
Authenticator.prototype.login = function(data, callback) {
var conn = OSjs.Core.getConnection();

conn.request('login', data, function(response) {
if ( response.result ) {
callback(false, response.result);
API.call('login', data, function(error, result) {
if ( result ) {
callback(false, result);
} else {
var error = response.error || API._('ERR_LOGIN_INVALID');
var error = error || API._('ERR_LOGIN_INVALID');
callback(API._('ERR_LOGIN_FMT', error), false);
}
}, function(error) {
callback(API._('ERR_LOGIN_FMT', error), false);
});
};

Expand All @@ -152,16 +148,13 @@
*/
Authenticator.prototype.logout = function(callback) {
var opts = {};
var conn = OSjs.Core.getConnection();

conn.request('logout', opts, function(response) {
if ( response.result ) {
API.call('logout', opts, function(error, result) {
if ( result ) {
callback(false, true);
} else {
callback('An error occured: ' + (response.error || 'Unknown error'));
callback('An error occured: ' + (error || 'Unknown error'));
}
}, function(error) {
callback('Logout error: ' + error);
});
};

Expand Down
35 changes: 10 additions & 25 deletions src/client/javascript/core/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,13 @@
cbError('You are currently off-line and cannot perform this operation!');
} else if ( (API.getConfig('Connection.Type') === 'standalone') ) {
cbError('You are currently running locally and cannot perform this operation!');
} else {
if ( method.match(/^FS:/) ) {
return this.requestVFS(method.replace(/^FS:/, ''), args, options, cbSuccess, cbError);
}
return this.requestAPI(method, args, options, cbSuccess, cbError);
}

return false;
if ( method.match(/^FS:/) ) {
return this.requestVFS(method.replace(/^FS:/, ''), args, options, cbSuccess, cbError);
}

return this.requestAPI(method, args, options, cbSuccess, cbError);
};

/**
Expand All @@ -280,7 +279,7 @@
* @return {Boolean}
*/
Connection.prototype.requestAPI = function(method, args, options, cbSuccess, cbError) {
return this._request(false, method, args, options, cbSuccess, cbError);
return false;
};

/**
Expand All @@ -293,24 +292,10 @@
* @return {Boolean}
*/
Connection.prototype.requestVFS = function(method, args, options, cbSuccess, cbError) {
return this._request(true, method, args, options, cbSuccess, cbError);
};

/**
* Wrapper for OS.js API calls
*
* @function _request
* @memberof OSjs.Core.Connection#
*
* @return {Boolean}
*/
Connection.prototype._request = function(isVfs, method, args, options, onsuccess, onerror) {
if ( isVfs ) {
if ( method === 'get' ) {
return this._requestGET(args, options, onsuccess, onerror);
} else if ( method === 'upload' ) {
return this._requestPOST(args, options, onsuccess, onerror);
}
if ( method === 'get' ) {
return this._requestGET(args, options, cbSuccess, cbError);
} else if ( method === 'upload' ) {
return this._requestPOST(args, options, cbSuccess, cbError);
}

return false;
Expand Down
8 changes: 5 additions & 3 deletions src/client/javascript/core/connections/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,20 @@
HttpConnection.prototype = Object.create(Connection.prototype);
HttpConnection.constructor = Connection;

HttpConnection.prototype._request = function(isVfs, method, args, options, onsuccess, onerror) {
var res = Connection.prototype._request.apply(this, arguments);
HttpConnection.prototype.request = function(method, args, onsuccess, onerror, options) {
var res = Connection.prototype.request.apply(this, arguments);

if ( res === false ) {
var url = (function() {
if ( isVfs ) {
if ( method.match(/^FS:/) ) {
return API.getConfig('Connection.FSURI') + '/' + method.replace(/^FS\:/, '');
}
return API.getConfig('Connection.APIURI') + '/' + method;
})();

return this._requestXHR(url, args, options, onsuccess, onerror);
}

return res;
};

Expand Down
8 changes: 4 additions & 4 deletions src/client/javascript/core/connections/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,24 @@
}
};

WSConnection.prototype._request = function(isVfs, method, args, options, onsuccess, onerror) {
WSConnection.prototype.request = function(method, args, onsuccess, onerror, options) {
onerror = onerror || function() {
console.warn('Connection::callWS()', 'error', arguments);
};

var res = Connection.prototype._request.apply(this, arguments);
var res = Connection.prototype.request.apply(this, arguments);
if ( res !== false ) {
return res;
}

var idx = this.index++;
var base = isVfs ? '/FS/' : '/API/';
var base = method.match(/^FS:/) ? '/FS/' : '/API/';

try {
this.ws.send(JSON.stringify({
_index: idx,
sid: Utils.getCookie('session'),
path: base + method,
path: base + method.replace(/^FS:/, ''),
args: args
}));

Expand Down
8 changes: 1 addition & 7 deletions src/client/javascript/core/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,7 @@
* @param {CallbackHandler} callback Callback function
*/
Storage.prototype._settings = function(pool, storage, callback) {
var conn = OSjs.Core.getConnection();

conn.request('settings', {pool: pool, settings: Utils.cloneObject(storage)}, function(response) {
callback(false, response.result);
}, function(error) {
callback(error);
});
API.call('settings', {pool: pool, settings: Utils.cloneObject(storage)}, callback);
};

/**
Expand Down

0 comments on commit 20aae5b

Please sign in to comment.