UNPKG

@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

87 lines (86 loc) 4.82 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const _ = require("lodash"); const index_1 = require("../../../index"); class ActivityAnalyzerPlugin extends index_1.BasePlugin { // Helper to fetch the activity analyzer resource with caching fetchActivityAnalyzer(activityAnalyzerId) { const _super = name => super[name]; return __awaiter(this, void 0, void 0, function* () { const activityAnalyzerResponse = yield _super("requestGatewayHelper").call(this, "GET", `${this.outboundPlatformUrl}/v1/activity_analyzers/${activityAnalyzerId}`); this.logger.debug(`Fetched Activity Analyzer: ${activityAnalyzerId} - ${JSON.stringify(activityAnalyzerResponse.data)}`); return activityAnalyzerResponse.data; }); } // Helper to fetch the activity analyzer resource with caching fetchActivityAnalyzerProperties(activityAnalyzerId) { const _super = name => super[name]; return __awaiter(this, void 0, void 0, function* () { const activityAnalyzerPropertyResponse = yield _super("requestGatewayHelper").call(this, "GET", `${this.outboundPlatformUrl}/v1/activity_analyzers/${activityAnalyzerId}/properties`); this.logger.debug(`Fetched Creative Properties: ${activityAnalyzerId} - ${JSON.stringify(activityAnalyzerPropertyResponse.data)}`); return activityAnalyzerPropertyResponse.data; }); } // Method to build an instance context // To be overriden to get a cutom behavior // This is a default provided implementation instanceContextBuilder(activityAnalyzerId) { return __awaiter(this, void 0, void 0, function* () { const activityAnalyzerP = this.fetchActivityAnalyzer(activityAnalyzerId); const activityAnalyzerPropsP = this.fetchActivityAnalyzerProperties(activityAnalyzerId); const results = yield Promise.all([ activityAnalyzerP, activityAnalyzerPropsP ]); const activityAnalyzer = results[0]; const activityAnalyzerProps = results[1]; const context = { activityAnalyzer: activityAnalyzer, activityAnalyzerProperties: activityAnalyzerProps }; return context; }); } initActivityAnalysis() { this.app.post("/v1/activity_analysis", this.asyncMiddleware((req, res) => __awaiter(this, void 0, void 0, function* () { if (!req.body || _.isEmpty(req.body)) { const msg = { error: "Missing request body" }; this.logger.error("POST /v1/activity_analysis : %s", JSON.stringify(msg)); return res.status(500).json(msg); } else { this.logger.debug(`POST /v1/activity_analysis ${JSON.stringify(req.body)}`); const activityAnalyzerRequest = req.body; if (!this.onActivityAnalysis) { const errMsg = "No Activity Analyzer listener registered!"; this.logger.error(errMsg); return res.status(500).json({ error: errMsg }); } if (!this.pluginCache.get(activityAnalyzerRequest.activity_analyzer_id)) { this.pluginCache.put(activityAnalyzerRequest.activity_analyzer_id, this.instanceContextBuilder(activityAnalyzerRequest.activity_analyzer_id), this.INSTANCE_CONTEXT_CACHE_EXPIRATION); } const instanceContext = yield this.pluginCache.get(activityAnalyzerRequest.activity_analyzer_id); const pluginResponse = yield this.onActivityAnalysis(activityAnalyzerRequest, instanceContext); this.logger.debug(`Returning: ${JSON.stringify(pluginResponse)}`); return res.status(200).send(JSON.stringify(pluginResponse)); } }))); } constructor() { super(); // We init the specific route to listen for activity analysis requests this.initActivityAnalysis(); this.setErrorHandler(); } } exports.ActivityAnalyzerPlugin = ActivityAnalyzerPlugin;