UNPKG

@swrve/smarttv-sdk

Version:

Swrve marketing engagement platform SDK for SmartTV OTT devices

171 lines (170 loc) 6.83 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SwrveRestClient = void 0; const SwrveLogger_1 = __importDefault(require("../utils/SwrveLogger")); class SwrveRestClient { constructor(config, profileManager, platform) { this.config = config; this.profileManager = profileManager; this.platform = platform; this.version = 3; this.apiVersion = "8"; this.inAppVersion = "10"; this.embeddedVersion = "1"; } postEvents(events) { const appId = this.config.appId; const stack = this.config.stack === "us" ? "api" : "eu-api"; const url = this.config.contentUrl == null ? `https://${appId}.${stack}.swrve.com/1/batch` : this.config.contentUrl; const body = { user: this.profileManager.currentUser.userId, app_version: this.config.appVersion, session_token: this.profileManager.getSessionToken(), version: this.version, unique_device_id: this.platform.deviceID, data: events, }; return this.fetch(url, { body: JSON.stringify(body), cache: "no-cache", method: "POST", headers: { "content-type": "application/json", }, }); } getCampaignsAndResources() { const appId = this.config.appId; const stack = this.config.stack === "us" ? "content" : "eu-content"; const url = this.config.apiUrl == null ? `https://${appId}.${stack}.swrve.com/api/1/user_content?` : this.config.apiUrl; const query = this.getQueryString(this.profileManager.currentUser.etag); SwrveLogger_1.default.debug("campaignsAndResourcesUrl: " + url + query); return this.fetch(url + query) .then((response) => { if (response && response.ok) { return response.json().then((json) => ({ json, etag: response.headers.get("ETag"), })); } return response .json() .then((body) => { const error = new Error((body === null || body === void 0 ? void 0 : body.message) || "Request failed"); error.status = response ? response.status : null; error.body = body !== null && body !== void 0 ? body : null; throw error; }) .catch((error) => { throw error; }); }) .catch((error) => { throw error; }); } identify(externalUserId, swrveId) { const appId = this.config.appId; const stack = this.config.stack === "us" ? "identity" : "eu-identity"; const url = this.config.identityUrl == null ? `https://${appId}.${stack}.swrve.com/identify` : this.config.identityUrl; const body = { api_key: this.config.apiKey, swrve_id: swrveId, external_user_id: externalUserId, unique_device_id: this.platform.deviceID, }; return this.fetch(url, { body: JSON.stringify(body), cache: "no-cache", method: "POST", headers: { "Content-Type": "application/json", }, }) .then((response) => { if (response && response.ok) { return response.json(); } else { const theError = new Error(response.status.toString()); theError.status = response ? response.status : null; throw theError; } }) .catch((error) => { SwrveLogger_1.default.debug(String(error)); throw error; }); } getUserResourcesDiff() { const appId = this.config.appId; const stack = this.config.stack === "us" ? "content" : "eu-content"; const url = this.config.apiUrl == null ? `https://${appId}.${stack}.swrve.com/api/1/user_resources_diff?` : this.config.apiUrl; const query = this.getQueryString(); SwrveLogger_1.default.info("url+query " + url + query); return this.fetch(url + query).then((response) => response.json().then((json) => ({ json, etag: response.headers.get("ETag"), }))); } getQueryString(etag) { const esc = encodeURIComponent; const params = this.getContentRequestParams(); const query = Object.keys(params) .filter((key) => params[key] != null) .map((k) => esc(k) + "=" + esc(params[k])) .join("&"); const etagParam = etag ? "&etag=" + etag : ""; const sessionToken = this.profileManager.getSessionToken(); const sessionTokenParam = sessionToken ? "&session_token=" + sessionToken : ""; return query + etagParam + sessionTokenParam; } getContentRequestParams() { const params = { api_key: this.config.apiKey, user: this.profileManager.currentUser.userId, app_version: this.config.appVersion, joined: this.profileManager.currentUser.firstUse.toString(), version: this.apiVersion, in_app_version: this.inAppVersion, embedded_campaign_version: this.embeddedVersion, language: this.platform.language, app_store: this.platform.appStore, device_width: this.platform.screenWidth.toString(), device_height: this.platform.screenHeight.toString(), device_dpi: this.platform.screenDPI.toString(), device_name: this.platform.deviceID, device_type: "tv", os: this.platform.os, os_version: this.platform.osVersion, orientation: "landscape", }; return params; } fetch(input, init) { const timeout = this.config.httpsTimeoutSeconds; let controller; let signal; if ("AbortController" in window) { controller = new AbortController(); signal = controller.signal; } return Promise.race([ fetch(input, Object.assign(Object.assign({}, init), { signal })), new Promise((_, reject) => { setTimeout(() => { reject(new Error("Request timeout after " + timeout + " seconds")); if (controller) { controller.abort(); } }, timeout * 1000); }), ]); } } exports.SwrveRestClient = SwrveRestClient;