@mediarithmics/plugins-nodejs-sdk
Version:
This is the mediarithmics nodejs to help plugin developers bootstrapping their plugin without having to deal with most of the plugin boilerplate
82 lines • 4.21 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RecommenderPlugin = void 0;
const lodash_1 = __importDefault(require("lodash"));
const BasePlugin_1 = require("../common/BasePlugin");
class RecommenderPlugin extends BasePlugin_1.BasePlugin {
constructor() {
super();
// We init the specific route to listen for activity analysis requests
this.initRecommendationRequest();
this.setErrorHandler();
}
// Helper to fetch the activity analyzer resource with caching
async fetchRecommenderCatalogs(recommenderId) {
const recommenderCatalogsResponse = await super.requestGatewayHelper('GET', `${this.outboundPlatformUrl}/v1/recommenders/${recommenderId}/catalogs`);
this.logger.debug(`Fetched recommender catalogs: ${recommenderId} - ${JSON.stringify(recommenderCatalogsResponse.data)}`);
return recommenderCatalogsResponse.data;
}
// Method to build an instance context
// To be overriden to get a cutom behavior
// Helper to fetch the activity analyzer resource with caching
async fetchRecommenderProperties(recommenderId) {
const recommenderPropertyResponse = await super.requestGatewayHelper('GET', `${this.outboundPlatformUrl}/v1/recommenders/${recommenderId}/properties`);
this.logger.debug(`Fetched recommender Properties: ${recommenderId} - ${JSON.stringify(recommenderPropertyResponse.data)}`);
return recommenderPropertyResponse.data;
}
// Method to process an Activity Analysis
// This is a default provided implementation
async instanceContextBuilder(recommenderId) {
const recommenderProps = await this.fetchRecommenderProperties(recommenderId);
const context = {
properties: new BasePlugin_1.PropertiesWrapper(recommenderProps),
};
return context;
}
async getInstanceContext(recommenderId) {
if (!this.pluginCache.get(recommenderId)) {
void this.pluginCache.put(recommenderId, this.instanceContextBuilder(recommenderId).catch((err) => {
this.logger.error(`Error while caching instance context: ${err.message}`);
this.pluginCache.del(recommenderId);
throw err;
}), this.getInstanceContextCacheExpiration());
}
return this.pluginCache.get(recommenderId);
}
initRecommendationRequest() {
this.app.post('/v1/recommendations', this.asyncMiddleware(async (req, res) => {
if (!this.httpIsReady()) {
const msg = {
error: 'Plugin not initialized',
};
this.logger.error('POST /v1/recommendations : %s', JSON.stringify(msg));
return res.status(500).json(msg);
}
else if (!req.body || lodash_1.default.isEmpty(req.body)) {
const msg = {
error: 'Missing request body',
};
this.logger.error('POST /v1/recommendations : %s', JSON.stringify(msg));
return res.status(500).json(msg);
}
else {
this.logger.debug(`POST /v1/recommendations ${JSON.stringify(req.body)}`);
const recommenderRequest = req.body;
if (!this.onRecommendationRequest) {
const errMsg = 'No Recommendation request listener registered!';
this.logger.error(errMsg);
return res.status(500).json({ error: errMsg });
}
const instanceContext = await this.getInstanceContext(recommenderRequest.recommender_id);
const pluginResponse = await this.onRecommendationRequest(recommenderRequest, instanceContext);
this.logger.debug(`Returning: ${JSON.stringify(pluginResponse)}`);
return res.status(200).send(JSON.stringify(pluginResponse));
}
}));
}
}
exports.RecommenderPlugin = RecommenderPlugin;
//# sourceMappingURL=RecommenderBasePlugin.js.map