Skip to content

Commit

Permalink
add replyHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroVega committed Nov 3, 2023
1 parent 7edb906 commit 0005a26
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 50 deletions.
182 changes: 133 additions & 49 deletions lib/server/handlers/sthGetDataHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const encodeRFC5987 = require('rfc5987-value-chars').encode;
* @param {Object} request The request
* @param {Function} reply Hapi's reply function
*/
function getRawData(request, reply) {
function getRawData(request, reply, replyHandler) {
let response;

var query = {
Expand Down Expand Up @@ -71,7 +71,11 @@ function getRawData(request, reply) {
sthLogger.debug(request.sth.context, 'Responding with 500- Internal error');
const error = boom.internal(message);
error.output.payload.message = message;
reply(error);
if (reply) {
reply.response(error);
} else {
replyHandler(error);
}
} else {
// The collection does not exist, reply with en empty response
sthLogger.warn(
Expand All @@ -90,7 +94,11 @@ function getRawData(request, reply) {
request.params.attrName,
emptyResponse
);
response = reply(ngsiPayload);
if (reply) {
response = reply.response(ngsiPayload);
} else {
response = replyHandler(ngsiPayload);
}
sthServerUtils.addFiwareCorrelator(request, response);
if (request.query.count) {
sthServerUtils.addFiwareTotalCount(0, response);
Expand All @@ -116,7 +124,11 @@ function getRawData(request, reply) {
// Error when getting the raw data
sthLogger.error(request.sth.context, 'Error %j when getting raw data with query %j', err, rawQuery);
sthLogger.debug(request.sth.context, 'Responding with 500 - Internal Error');
response = reply(err);
if (reply) {
response = reply.response(err);
} else {
response = replyHandler(err);
}
} else if (!result || !(result.length || result instanceof stream)) {
// No raw data available for the request
sthLogger.info(
Expand All @@ -127,22 +139,38 @@ function getRawData(request, reply) {
);

sthLogger.debug(request.sth.context, 'Responding with no points');
response = reply(
sthServerUtils.getNGSIPayload(
request.params.version,
request.params.entityId,
request.params.entityType,
request.params.attrName,
sthServerUtils.getEmptyResponse()
)
);
if (reply) {
response = reply.response(
sthServerUtils.getNGSIPayload(
request.params.version,
request.params.entityId,
request.params.entityType,
request.params.attrName,
sthServerUtils.getEmptyResponse()
)
);
} else {
response = replyHandler(
sthServerUtils.getNGSIPayload(
request.params.version,
request.params.entityId,
request.params.entityType,
request.params.attrName,
sthServerUtils.getEmptyResponse()
)
);
}
} else if (result instanceof stream) {
sthLogger.debug(request.sth.context, 'Responding with a stream of docs');
response = reply(new stream.Readable().wrap(result));
} else if (typeof result === 'string') {
sthLogger.debug(request.sth.context, "Responding with file '" + result + "'");
fs.readFile(result, function(err, data) {
response = reply(data);
if (reply) {
response = reply.response(data);
} else {
response = replyHandler(data);
}
const fileName = result.substring(result.lastIndexOf(path.sep) + 1);
response.header(
'Content-Disposition',
Expand All @@ -164,15 +192,27 @@ function getRawData(request, reply) {
}); // readfile
} else {
sthLogger.debug(request.sth.context, 'Responding with %s docs', result.length);
response = reply(
sthServerUtils.getNGSIPayload(
request.params.version,
request.params.entityId,
request.params.entityType,
request.params.attrName,
result
)
);
if (reply) {
response = reply.response(
sthServerUtils.getNGSIPayload(
request.params.version,
request.params.entityId,
request.params.entityType,
request.params.attrName,
result
)
);
} else {
response = replyHandler(
sthServerUtils.getNGSIPayload(
request.params.version,
request.params.entityId,
request.params.entityType,
request.params.attrName,
result
)
);
}
}
sthServerUtils.addFiwareCorrelator(request, response);
if (request.query.count) {
Expand Down Expand Up @@ -221,7 +261,11 @@ function getAggregatedData(request, reply) {
sthLogger.debug(request.sth.context, 'Responding with 500- Internal error');
const error = boom.internal(message);
error.output.payload.message = message;
reply(error);
if (reply) {
reply.response(error);
} else {
replyHandler(error);

Check failure on line 267 in lib/server/handlers/sthGetDataHandler.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

'replyHandler' is not defined
}
} else {
// The collection does not exist, reply with en empty response
sthLogger.warn(
Expand All @@ -243,7 +287,11 @@ function getAggregatedData(request, reply) {
request.params.attrName,
emptyResponse
);
response = reply(ngsiPayload);
if (reply) {
response = reply.response(ngsiPayload);
} else {
response = replyHandler(ngsiPayload);

Check failure on line 293 in lib/server/handlers/sthGetDataHandler.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

'replyHandler' is not defined
}
sthServerUtils.addFiwareCorrelator(request, response);
}
} else {
Expand Down Expand Up @@ -274,7 +322,11 @@ function getAggregatedData(request, reply) {
aggregatedQuery
);
sthLogger.debug(request.sth.context, 'Responding with 500 - Internal Error');
response = reply(err);
if (reply) {
response = reply.response(err);
} else {
response = replyHandler(err);

Check failure on line 328 in lib/server/handlers/sthGetDataHandler.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

'replyHandler' is not defined
}
} else if (!result || !result.length) {
// No aggregated data available for the request
sthLogger.debug(
Expand All @@ -285,26 +337,50 @@ function getAggregatedData(request, reply) {
);

sthLogger.debug(request.sth.context, 'Responding with no points');
response = reply(
sthServerUtils.getNGSIPayload(
request.params.version,
request.params.entityId,
request.params.entityType,
request.params.attrName,
sthServerUtils.getEmptyResponse()
)
);
if (reply) {
response = reply.response(
sthServerUtils.getNGSIPayload(
request.params.version,
request.params.entityId,
request.params.entityType,
request.params.attrName,
sthServerUtils.getEmptyResponse()
)
);
} else {
response = replyHandler(

Check failure on line 351 in lib/server/handlers/sthGetDataHandler.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

'replyHandler' is not defined
sthServerUtils.getNGSIPayload(
request.params.version,
request.params.entityId,
request.params.entityType,
request.params.attrName,
sthServerUtils.getEmptyResponse()
)
);
}
} else {
sthLogger.debug(request.sth.context, 'Responding with %s docs', result.length);
response = reply(
sthServerUtils.getNGSIPayload(
request.params.version,
request.params.entityId,
request.params.entityType,
request.params.attrName,
result
)
);
if (reply) {
response = reply.response(
sthServerUtils.getNGSIPayload(
request.params.version,
request.params.entityId,
request.params.entityType,
request.params.attrName,
result
)
);
} else {
response = replyHandler(

Check failure on line 374 in lib/server/handlers/sthGetDataHandler.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

'replyHandler' is not defined
sthServerUtils.getNGSIPayload(
request.params.version,
request.params.entityId,
request.params.entityType,
request.params.attrName,
result
)
);
}
}
sthServerUtils.addFiwareCorrelator(request, response);
});
Expand All @@ -317,7 +393,7 @@ function getAggregatedData(request, reply) {
* @param {Object} request The request
* @param {Function} reply The reply() function of the hapi server
*/
function getDataHandler(request, reply) {
function getDataHandler(request, reply, replyHandler) {
request.sth = request.sth || {};
request.sth.context = sthServerUtils.getContext(request);
let message;
Expand Down Expand Up @@ -349,13 +425,17 @@ function getDataHandler(request, reply) {
source: 'query',
keys: ['lastN', 'hLimit', 'hOffset', 'filetype', 'aggrMethod', 'aggrPeriod', 'count']
};
return reply(error);
if (reply) {
return reply.response(error);
} else {
return replyHandler(error);
}
}
}
getRawData(request, reply);
getRawData(request, reply, replyHandler);
} else if (request.query.aggrMethod && request.query.aggrPeriod) {
// Aggregated data is requested
getAggregatedData(request, reply);
getAggregatedData(request, reply, replyHandler);
} else {
message =
'A combination of the following query params is required: lastN, hLimit and hOffset, ' +
Expand All @@ -369,7 +449,11 @@ function getDataHandler(request, reply) {
source: 'query',
keys: ['lastN', 'hLimit', 'hOffset', 'filetype', 'aggrMethod', 'aggrPeriod', 'count']
};
return reply(error);
if (reply) {
return reply.response(error);
} else {
return replyHandler(error);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/server/handlers/sthGetDataHandlerV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function getDataHandler(request, reply) {
request.params.entityType = request.query.type;
request.params.version = 2;

sthGetDataHandlerV1(request, replyHandler);
sthGetDataHandlerV1(request, null, replyHandler);
}

module.exports = getDataHandler;

0 comments on commit 0005a26

Please sign in to comment.