return api_server + ':' + CONFD_PORT;
};
+var projectContextUrl = function(req, url) {
+ if (req.session && req.session.projectId) {
+ return url.replace(/(\/api\/operational\/|\/api\/config\/|\/api\/operations\/)(.*)/, '$1project/' + req.session.projectId + '/$2');
+
+ }
+ return url;
+}
+
var validateResponse = function(callerName, error, response, body, resolve, reject) {
var res = {};
};
reject(res);
return false;
- } else if (response.statusCode >= 400) {
+ } else if (response.statusCode >= CONSTANTS.HTTP_RESPONSE_CODES.ERROR.BAD_REQUEST) {
console.log('Problem with "', callerName, '": ', response.statusCode, ':', body);
res.statusCode = response.statusCode;
// auth specific
- if (response.statusCode == 401) {
+ if (response.statusCode == CONSTANTS.HTTP_RESPONSE_CODES.ERROR.UNAUTHORIZED) {
res.errorMessage = {
error: 'Authentication needed' + body
};
reject(res);
return false;
- } else if (response.statusCode == 204) {
+ } else if (response.statusCode == CONSTANTS.HTTP_RESPONSE_CODES.SUCCESS.NO_CONTENT) {
resolve({
statusCode: response.statusCode,
data: {}
var checkAuthorizationHeader = function(req) {
return new Promise(function(resolve, reject) {
- if (req.get('Authorization') == null) {
+ if (req.session && req.session.authorization == null) {
reject();
} else {
resolve();
reject(res);
fs.appendFileSync(logFile, 'Request API: ' + response.request.uri.href + ' ; ' + 'Error: ' + error);
return false;
- } else if (response.statusCode >= 400) {
+ } else if (response.statusCode >= CONSTANTS.HTTP_RESPONSE_CODES.ERROR.BAD_REQUEST) {
console.log('Problem with "', callerName, '": ', response.statusCode, ':', body);
res.statusCode = response.statusCode;
// auth specific
- if (response.statusCode == 401) {
+ if (response.statusCode == CONSTANTS.HTTP_RESPONSE_CODES.ERROR.UNAUTHORIZED) {
res.errorMessage = {
error: 'Authentication needed' + body
};
reject(res);
fs.appendFileSync(logFile, 'Request API: ' + response.request.uri.href + ' ; ' + 'Error Body: ' + body);
return false;
- } else if (response.statusCode == 204) {
+ } else if (response.statusCode == CONSTANTS.HTTP_RESPONSE_CODES.SUCCESS.NO_CONTENT) {
resolve();
fs.appendFileSync(logFile, 'Request API: ' + response.request.uri.href + ' ; ' + 'Response Body: ' + body);
return false;
}
new Promise(function(resolve, reject) {
request({
- uri: uri,
+ uri: projectContextUrl(req, uri),
method: 'GET',
headers: _.extend({}, CONSTANTS.HTTP_HEADERS.accept[type], {
- 'Authorization': req.get('Authorization'),
+ 'Authorization': req.session && req.session.authorization,
forever: CONSTANTS.FOREVER_ON,
rejectUnauthorized: false,
})
});
}
+var getPortForProtocol = function(protocol) {
+ switch (protocol) {
+ case 'http':
+ return 8000;
+ case 'https':
+ return 8443;
+ }
+}
+
module.exports = {
/**
* Ensure confd port is on api_server variable.
sendSuccessResponse: sendSuccessResponse,
- passThroughConstructor: passThroughConstructor
+ passThroughConstructor: passThroughConstructor,
+
+ getPortForProtocol: getPortForProtocol,
+
+ projectContextUrl: projectContextUrl
};