Skip to content
Snippets Groups Projects
Commit 5c63aceb authored by Piotr Gawron's avatar Piotr Gawron
Browse files

sending request code simplified (all types of requests have common interface)

parent 8295f11c
No related branches found
No related tags found
2 merge requests!115Resolve "admin panel should use API",!114Resolve "admin panel should use API"
......@@ -141,9 +141,9 @@ ServerConnector._sendRequest = function(params) {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(new NetworkError(url + " rejected with status code: " + response.statusCode, {
reject(new NetworkError(params.url + " rejected with status code: " + response.statusCode, {
content : body,
url : url,
url : params.url,
statusCode : response.statusCode
}));
} else {
......@@ -154,114 +154,42 @@ ServerConnector._sendRequest = function(params) {
};
ServerConnector.sendPostRequest = function(url, params) {
var self = this;
if (self.getSessionData().getToken() === undefined) {
self.getSessionData().setLogin(undefined);
window.location.reload(false);
}
return new Promise(function(resolve, reject) {
request({
method : "POST",
url : url,
form : params
}, function(error, response, body) {
if (error) {
reject(new NetworkError(error.message, {
content: body,
url: url
}));
} else if (response.statusCode !== 200) {
reject(new NetworkError(url + " rejected with status code: " + response.statusCode, {
content : body,
url : url,
statusCode : response.statusCode
}));
} else {
resolve(body);
}
});
return this.sendRequest({
method : "POST",
url : url,
form : params
});
};
ServerConnector.sendPutRequest = function(url, params) {
var self = this;
if (self.getSessionData().getToken() === undefined) {
self.getSessionData().setLogin(undefined);
window.location.reload(false);
}
return new Promise(function(resolve, reject) {
request({
method : "PUT",
url : url,
form : params
}, function(error, response, body) {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(new Error(url + " rejected with status code: " + response.statusCode));
} else {
resolve(body);
}
});
return this.sendRequest({
method : "PUT",
url : url,
form : params
});
};
ServerConnector.sendDeleteRequest = function(url, json) {
var self = this;
if (self.getSessionData().getToken() === undefined) {
self.getSessionData().setLogin(undefined);
window.location.reload(false);
}
return new Promise(function(resolve, reject) {
request({
method : "DELETE",
url : url,
json : json,
}, function(error, response, body) {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(new Error(url + " rejected with status code: " + response.statusCode));
} else {
resolve(body);
}
});
return this.sendRequest({
method : "DELETE",
url : url,
json : json,
});
};
ServerConnector.sendPatchRequest = function(url, params) {
var self = this;
if (self.getSessionData().getToken() === undefined) {
self.getSessionData().setLogin(undefined);
window.location.reload(false);
}
return new Promise(function(resolve, reject) {
request({
method : "PATCH",
url : url,
json : params,
}, function(error, response, body) {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(new Error(url + " rejected with status code: " + response.statusCode));
} else {
resolve(body);
}
});
ServerConnector.sendPatchRequest = function(url, json) {
return this.sendRequest({
method : "PATCH",
url : url,
json : json,
});
};
ServerConnector.getToken = function(token) {
if (token !== undefined) {
return Promise.resolve(token);
}
ServerConnector.getToken = function() {
var self = this;
token = self.getSessionData(null).getToken();
var login = self.getSessionData(null).getLogin()
if (token === undefined || login === undefined) {
var token = self.getSessionData(null).getToken();
if (token === undefined) {
return self.login();
} else {
// if the project is not initialized then check if we can download data
......@@ -271,6 +199,8 @@ ServerConnector.getToken = function(token) {
token : token
}).then(function() {
return token;
// if there was an error accessing configuration it means our token is
// invalid
}, function() {
return self.login();
});
......@@ -280,11 +210,11 @@ ServerConnector.getToken = function(token) {
}
};
ServerConnector.getApiBaseUrl = function () {
ServerConnector.getApiBaseUrl = function() {
return this.getServerBaseUrl() + "/api/";
};
ServerConnector.getServerBaseUrl = function () {
ServerConnector.getServerBaseUrl = function() {
if (this._serverBaseUrl === undefined) {
var url = "" + window.location.href;
if (url.indexOf("?")>=0) {
......@@ -366,20 +296,20 @@ ServerConnector.getProjectUrl = function(queryParams, filterParams) {
});
};
ServerConnector.getProjectStatisticsUrl = function (queryParams, filterParams) {
ServerConnector.getProjectStatisticsUrl = function(queryParams, filterParams) {
return this.getApiUrl({
url : this.getProjectUrl(queryParams) + "statistics/",
params : filterParams,
});
};
ServerConnector.getPublicationsUrl = function (queryParams, filterParams) {
ServerConnector.getPublicationsUrl = function(queryParams, filterParams) {
filterParams.start = filterParams.start || 0;
filterParams.length = filterParams.length || 10;
return this.getApiUrl({
url: this.getModelsUrl(queryParams) + "publications/",
params: filterParams,
url : this.getModelsUrl(queryParams) + "publications/",
params : filterParams,
});
};
......@@ -407,32 +337,32 @@ ServerConnector.logoutUrl = function () {
ServerConnector.getSuggestedQueryListUrl = function (queryParams, filterParams) {
return this.getApiUrl({
url: this.getBioEntitiesUrl(queryParams) + "suggestedQueryList/",
params: filterParams,
url : this.getBioEntitiesUrl(queryParams) + "suggestedQueryList/",
params : filterParams,
});
};
ServerConnector.addCommentUrl = function (queryParams) {
ServerConnector.addCommentUrl = function(queryParams) {
return this.getApiUrl({
url: this.getCommentsUrl(queryParams)
url : this.getCommentsUrl(queryParams)
});
};
ServerConnector.addOverlayUrl = function (queryParams) {
ServerConnector.addOverlayUrl = function(queryParams) {
return this.getApiUrl({
url: this.getOverlaysUrl(queryParams)
url : this.getOverlaysUrl(queryParams)
});
};
ServerConnector.updateOverlayUrl = function (queryParams) {
ServerConnector.updateOverlayUrl = function(queryParams) {
return this.getApiUrl({
url: this.getOverlayByIdUrl(queryParams)
url : this.getOverlayByIdUrl(queryParams)
});
};
ServerConnector.deleteOverlayUrl = function (queryParams) {
ServerConnector.deleteOverlayUrl = function(queryParams) {
return this.getApiUrl({
url: this.getOverlayByIdUrl(queryParams),
url : this.getOverlayByIdUrl(queryParams),
});
};
......@@ -449,7 +379,7 @@ ServerConnector.getOverlaysUrl = function(queryParams, filterParams) {
});
};
ServerConnector.getCommentsUrl = function (queryParams, filterParams) {
ServerConnector.getCommentsUrl = function(queryParams, filterParams) {
var modelId = this.getIdOrAsterisk(queryParams.modelId);
var url = this.getProjectUrl(queryParams) + "comments/models/" + modelId + "/";
if (queryParams.elementType !== undefined) {
......@@ -464,41 +394,41 @@ ServerConnector.getCommentsUrl = function (queryParams, filterParams) {
}
}
return this.getApiUrl({
url: url,
params: filterParams,
url : url,
params : filterParams,
});
};
ServerConnector.getOverlayByIdUrl = function (queryParams, filterParams) {
ServerConnector.getOverlayByIdUrl = function(queryParams, filterParams) {
return this.getApiUrl({
url: this.getOverlaysUrl(queryParams) + queryParams.overlayId + "/",
params: filterParams,
url : this.getOverlaysUrl(queryParams) + queryParams.overlayId + "/",
params : filterParams,
});
};
ServerConnector.getOverlayElementsUrl = function (queryParams, filterParams) {
ServerConnector.getOverlayElementsUrl = function(queryParams, filterParams) {
return this.getApiUrl({
url: this.getModelsUrl(queryParams) + "bioEntities/",
params: filterParams,
url : this.getModelsUrl(queryParams) + "bioEntities/",
params : filterParams,
});
};
ServerConnector.getFullOverlayElementUrl = function (queryParams, filterParams) {
ServerConnector.getFullOverlayElementUrl = function(queryParams, filterParams) {
return this.getApiUrl({
url: this.getAliasesUrl(queryParams) + queryParams.id + "/",
params: filterParams,
url : this.getAliasesUrl(queryParams) + queryParams.id + "/",
params : filterParams,
});
};
ServerConnector.idsToString = function (ids) {
ServerConnector.idsToString = function(ids) {
var result = "";
if (ids !== undefined) {
ids.sort(function (a, b) {
ids.sort(function(a, b) {
return a - b;
});
for (var i = 0; i < ids.length; i++) {
......@@ -514,18 +444,18 @@ ServerConnector.idsToString = function (ids) {
return result;
};
ServerConnector.pointToString = function (point) {
ServerConnector.pointToString = function(point) {
return point.x.toFixed(2) + "," + point.y.toFixed(2);
};
ServerConnector.columnsToString = function (columns) {
ServerConnector.columnsToString = function(columns) {
if (columns === undefined) {
return "";
}
return columns;
};
ServerConnector.getModelsUrl = function (queryParams) {
ServerConnector.getModelsUrl = function(queryParams) {
var modelId = this.getIdOrAsterisk(queryParams.modelId);
var overlayId = queryParams.overlayId;
var url = this.getProjectUrl(queryParams);
......@@ -534,17 +464,17 @@ ServerConnector.getModelsUrl = function (queryParams) {
}
return this.getApiUrl({
url: url + "models/" + modelId + "/",
url : url + "models/" + modelId + "/",
});
};
ServerConnector.getBioEntitiesUrl = function (queryParams) {
ServerConnector.getBioEntitiesUrl = function(queryParams) {
return this.getApiUrl({
url: this.getModelsUrl(queryParams) + "bioEntities/",
url : this.getModelsUrl(queryParams) + "bioEntities/",
});
};
ServerConnector.getIdOrAsterisk = function (id) {
ServerConnector.getIdOrAsterisk = function(id) {
if (id === undefined || id === "" || id === null) {
return "*";
} else {
......@@ -552,64 +482,64 @@ ServerConnector.getIdOrAsterisk = function (id) {
}
};
ServerConnector.getReactionsUrl = function (queryParams, filterParams) {
ServerConnector.getReactionsUrl = function(queryParams, filterParams) {
return this.getApiUrl({
url: this.getBioEntitiesUrl(queryParams) + "reactions/",
params: filterParams,
url : this.getBioEntitiesUrl(queryParams) + "reactions/",
params : filterParams,
});
};
ServerConnector.getAliasesUrl = function (queryParams, filterParams) {
ServerConnector.getAliasesUrl = function(queryParams, filterParams) {
return this.getApiUrl({
url: this.getBioEntitiesUrl(queryParams) + "elements/",
params: filterParams,
url : this.getBioEntitiesUrl(queryParams) + "elements/",
params : filterParams,
});
};
ServerConnector.getConfigurationUrl = function (queryParams, filterParams) {
ServerConnector.getConfigurationUrl = function(queryParams, filterParams) {
var result = this.getApiUrl({
type: "configuration/",
params: filterParams
type : "configuration/",
params : filterParams
});
return result;
};
ServerConnector.getSearchUrl = function (queryParams, filterParams) {
ServerConnector.getSearchUrl = function(queryParams, filterParams) {
return this.getApiUrl({
url: this.getModelsUrl(queryParams) + "bioEntities:search",
params: filterParams,
url : this.getModelsUrl(queryParams) + "bioEntities:search",
params : filterParams,
});
};
ServerConnector.getSearchDrugsUrl = function (queryParams, filterParams) {
ServerConnector.getSearchDrugsUrl = function(queryParams, filterParams) {
return this.getApiUrl({
url : this.getProjectUrl(queryParams) + "drugs:search",
params : filterParams,
});
};
ServerConnector.getSearchMiRnasUrl = function (queryParams, filterParams) {
ServerConnector.getSearchMiRnasUrl = function(queryParams, filterParams) {
return this.getApiUrl({
url : this.getProjectUrl(queryParams) + "miRnas:search",
params : filterParams,
});
};
ServerConnector.getSearchChemicalsUrl = function (queryParams, filterParams) {
ServerConnector.getSearchChemicalsUrl = function(queryParams, filterParams) {
return this.getApiUrl({
url : this.getProjectUrl(queryParams) + "chemicals:search",
params : filterParams,
});
};
ServerConnector.getOverlaySourceUrl = function (queryParams, filterParams) {
ServerConnector.getOverlaySourceUrl = function(queryParams, filterParams) {
return this.getApiUrl({
url: this.getOverlaysUrl(queryParams) + queryParams.overlayId + ":downloadSource",
params: filterParams
url : this.getOverlaysUrl(queryParams) + queryParams.overlayId + ":downloadSource",
params : filterParams
});
};
ServerConnector.getImageUrl = function (queryParams, filterParams) {
ServerConnector.getImageUrl = function(queryParams, filterParams) {
return this.getApiUrl({
url : this.getProjectUrl(queryParams) + "models/" + queryParams.modelId + ":downloadImage",
params : filterParams,
......@@ -943,21 +873,21 @@ ServerConnector.getAliases = function (params) {
params.excludedCompartmentIds = [];
}
var filterParams = {
id: params.ids,
columns: params.columns,
type: params.type,
excludedCompartmentIds: params.excludedCompartmentIds,
includedCompartmentIds: params.includedCompartmentIds,
id : params.ids,
columns : params.columns,
type : params.type,
excludedCompartmentIds : params.excludedCompartmentIds,
includedCompartmentIds : params.includedCompartmentIds,
};
return self.getProjectId(params.projectId).then(function (result) {
return self.getProjectId(params.projectId).then(function(result) {
queryParams.projectId = result;
if (filterParams.id.length > 100) {
return self.sendPostRequest(self.getAliasesUrl(queryParams), filterParams);
} else {
return self.sendGetRequest(self.getAliasesUrl(queryParams, filterParams));
}
}).then(function (content) {
}).then(function(content) {
var array = JSON.parse(content);
var result = [];
for (var i = 0; i < array.length; i++) {
......@@ -967,22 +897,22 @@ ServerConnector.getAliases = function (params) {
});
};
ServerConnector.getLightComments = function (params) {
params.columns = ["id", "elementId", "modelId", "type", "icon", "removed", "pinned"];
ServerConnector.getLightComments = function(params) {
params.columns = [ "id", "elementId", "modelId", "type", "icon", "removed", "pinned" ];
return this.getComments(params);
};
ServerConnector.getComments = function (params) {
ServerConnector.getComments = function(params) {
var self = this;
var queryParams = {
elementId: params.elementId,
elementType: params.elementType,
coordinates: params.coordinates,
elementId : params.elementId,
elementType : params.elementType,
coordinates : params.coordinates,
};
var filterParams = {
columns: params.columns
columns : params.columns
};
return self.getProjectId(params.projectId).then(function (result) {
return self.getProjectId(params.projectId).then(function(result) {
queryParams.projectId = result;
return self.sendGetRequest(self.getCommentsUrl(queryParams, filterParams));
}).then(function(content) {
......@@ -995,7 +925,7 @@ ServerConnector.getComments = function (params) {
});
};
ServerConnector.getSessionData = function (project) {
ServerConnector.getSessionData = function(project) {
if (this._sessionData === undefined) {
this._sessionData = new SessionData(project);
}
......@@ -1005,16 +935,16 @@ ServerConnector.getSessionData = function (project) {
return this._sessionData;
};
ServerConnector.getClosestElementsByCoordinates = function (params) {
ServerConnector.getClosestElementsByCoordinates = function(params) {
var self = this;
var queryParams = {
modelId: params.modelId
modelId : params.modelId
};
var filterParams = {
coordinates: params.coordinates,
count: params.count,
coordinates : params.coordinates,
count : params.count,
};
return self.getProjectId(params.projectId).then(function (result) {
return self.getProjectId(params.projectId).then(function(result) {
queryParams.projectId = result;
return self.sendGetRequest(self.getSearchUrl(queryParams, filterParams));
}).then(function(content) {
......@@ -1027,7 +957,7 @@ ServerConnector.getClosestElementsByCoordinates = function (params) {
});
};
ServerConnector.login = function (login, password) {
ServerConnector.login = function(login, password) {
var self = this;
var params = {};
if (login !== undefined && login !== "") {
......@@ -1037,10 +967,10 @@ ServerConnector.login = function (login, password) {
params.login = "anonymous";
}
self.getSessionData().setToken("");
return self.sendPostRequest(self.loginUrl(), params).then(function () {
return self.sendPostRequest(self.loginUrl(), params).then(function() {
self.getSessionData().setLogin(params.login);
return Promise.resolve(self.getSessionData().getToken());
}, function (error) {
}, function(error) {
if (error instanceof NetworkError && error.statusCode === HttpStatus.FORBIDDEN) {
throw new InvalidCredentialsError("Invalid credentials");
} else {
......@@ -1049,24 +979,24 @@ ServerConnector.login = function (login, password) {
});
};
ServerConnector.logout = function () {
ServerConnector.logout = function() {
var self = this;
self.getSessionData().setToken(undefined);
self.getSessionData().setLogin(undefined);
return self.sendGetRequest(self.logoutUrl());
};
ServerConnector.getElementsByQuery = function (params) {
ServerConnector.getElementsByQuery = function(params) {
var self = this;
var queryParams = {
modelId: params.modelId
modelId : params.modelId
};
var filterParams = {
query: params.query,
perfectMatch: params.perfectMatch,
query : params.query,
perfectMatch : params.perfectMatch,
};
return self.getProjectId(params.projectId).then(function (result) {
return self.getProjectId(params.projectId).then(function(result) {
queryParams.projectId = result;
return self.sendGetRequest(self.getSearchUrl(queryParams, filterParams));
}).then(function(content) {
......
......@@ -15,28 +15,47 @@ var request = require('request');
var ServerConnectorMock = OriginalServerConnector;
function replaceAsterisk(str) {
return str.replace(/\*/g,"all").replace(/\:/g,".");
return str.replace(/\*/g, "all").replace(/\:/g, ".");
}
function urlToFileName(url) {
var result = url;
var token = OriginalServerConnector.getSessionData().getToken();
if (token!==undefined && token!=="" && url.startsWith("./testFiles/apiCalls")) {
if (!result.endsWith("&") &&!result.endsWith("_") ) {
result +="/";
if (token !== undefined && token !== "" && url.startsWith("./testFiles/apiCalls")) {
if (!result.endsWith("&") && !result.endsWith("_")) {
result += "/";
}
result+="token=" +token+"&";
result += "token=" + token + "&";
}
return replaceAsterisk(result);
}
function encodeParams(params) {
var result = {};
for ( var key in params) {
if (params.hasOwnProperty(key)) {
result[key] = encodeURIComponent(params[key]);
}
}
return result;
}
ServerConnectorMock._sendRequest = function(params) {
var self = this;
var url = params.url;
var prefix = params.method;
if (prefix==="GET") {
if (prefix === "GET") {
prefix = "";
} else {
prefix +="_";
prefix = "/" + prefix + "_";
}
var sufix = "";
if (params.form !== undefined) {
sufix += self.createGetParams(encodeParams(params.form));
}
if (params.json !== undefined) {
sufix += self.createGetParams(params.json);
}
return new Promise(function(resolve, reject) {
if (url.indexOf("http") === 0) {
......@@ -50,7 +69,7 @@ ServerConnectorMock._sendRequest = function(params) {
}
});
} else {
var fileName = urlToFileName(url);
var fileName = urlToFileName(url + prefix + sufix);
fs.readFile(fileName, 'utf8', function(err, content) {
if (err) {
reject(new NetworkError(err.message, {
......@@ -66,136 +85,6 @@ ServerConnectorMock._sendRequest = function(params) {
});
};
function encodeParams(params) {
var result = {};
for ( var key in params) {
if (params.hasOwnProperty(key)) {
result[key] = encodeURIComponent(params[key]);
}
}
return result;
}
ServerConnectorMock.sendPostRequest = function(url, params) {
var self = this;
return new Promise(function(resolve, reject) {
if (url.indexOf("http") === 0) {
request.post({
url : url,
form : params
}, function(error, response, body) {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(response);
} else {
resolve(body);
}
});
} else {
var mockUrl = urlToFileName(url + "/POST_" + self.createGetParams(encodeParams(params)));
fs.readFile(mockUrl, 'utf8', function(err, content) {
if (err) {
reject(err);
} else {
resolve(content);
}
});
}
});
};
ServerConnectorMock.sendPutRequest = function(url, params) {
var self = this;
return new Promise(function(resolve, reject) {
if (url.indexOf("http") === 0) {
request.put({
url : url,
form : params
}, function(error, response, body) {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(response);
} else {
resolve(body);
}
});
} else {
var mockUrl = urlToFileName(url + "/PUT_" + self.createGetParams(encodeParams(params)));
fs.readFile(mockUrl, 'utf8', function(err, content) {
if (err) {
reject(err);
} else {
resolve(content);
}
});
}
});
};
ServerConnectorMock.sendDeleteRequest = function(url, params) {
var self = this;
return new Promise(function(resolve, reject) {
if (url.indexOf("http") === 0) {
request.delete({
url : url,
form : params
}, function(error, response, body) {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(response);
} else {
resolve(body);
}
});
} else {
var mockUrl = urlToFileName(url + "/DELETE_" + self.createGetParams(encodeParams(params)));
fs.readFile(mockUrl, 'utf8', function(err, content) {
if (err) {
reject(err);
} else {
resolve(JSON.parse(content));
}
});
}
});
};
ServerConnectorMock.sendPatchRequest = function(url, params) {
var self = this;
return new Promise(function(resolve, reject) {
if (url.indexOf("http") === 0) {
request.delete({
url : url,
form : params
}, function(error, response, body) {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(response);
} else {
resolve(body);
}
});
} else {
var mockUrl = urlToFileName(url + "/PATCH_" + self.createGetParams(params));
fs.readFile(mockUrl, 'utf8', function(err, content) {
if (err) {
reject(err);
} else {
resolve(JSON.parse(content));
}
});
}
});
};
ServerConnectorMock.getApiBaseUrl = function() {
return "./testFiles/apiCalls/";
};
......
......@@ -285,9 +285,9 @@ describe('ServerConnector', function() {
describe('readFile', function() {
it('check session expired', function() {
ServerConnector.getSessionData().setToken(undefined);
assert.ok(ServerConnector.getSessionData().getLogin())
assert.ok(ServerConnector.getSessionData().getLogin());
return ServerConnector.sendGetRequest("package.json", "Downloading projects").then(function() {
assert.notOk(ServerConnector.getSessionData().getLogin())
assert.notOk(ServerConnector.getSessionData().getLogin());
});
});
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment