Last active
May 19, 2021 19:37
-
-
Save loldenburg/6a7c1674486ec83355e94a9258d060e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const claimRequest = require('claimRequest'); | |
const getRequestBody = require('getRequestBody'); | |
const getRequestPath = require('getRequestPath'); | |
const getRequestQueryParameters = require('getRequestQueryParameters'); | |
const getTimestamp = require('getTimestampMillis'); | |
const returnResponse = require('returnResponse'); | |
const setResponseBody = require('setResponseBody'); | |
const setResponseStatus = require('setResponseStatus'); | |
const getRequestMethod = require('getRequestMethod'); | |
const getContainerVersion = require('getContainerVersion'); | |
const JSON = require('JSON'); | |
const log = require('logToConsole'); | |
const setResponseHeader = require('setResponseHeader'); | |
const sendHttpRequest = require('sendHttpRequest'); | |
const templateDataStorage = require('templateDataStorage'); | |
if (getRequestPath() === '/stitch') { | |
claimRequest(); | |
// returns a failure message and status code | |
const fail = msg => { | |
setResponseStatus(500); | |
setResponseBody(msg); | |
returnResponse(); | |
}; | |
var params; | |
const reqMethod = getRequestMethod(); | |
if (reqMethod === "GET") { // expects a request like https://<your domain>/stitch?id=the_id_to_stitch&async_response=0 | |
params = getRequestQueryParameters(); // gets query params into object like { id: "12345", async_response: "0" } | |
} else if (reqMethod === "POST") { | |
params = JSON.parse(getRequestBody()); // in case we get the data in a POST body, we simply take the JSON payload | |
} | |
log("Got the following parameters from the " + reqMethod + " request:"); | |
log(params); | |
const uid = params.id; // get the user ID | |
if (!uid) { | |
fail("no user ID provided!"); | |
} | |
const containerVersion = getContainerVersion(); | |
const containerId = containerVersion.containerId; | |
// to make transport to another pubsub message frictionless, we provide all attributes as strings instead of Booleans | |
var isDebug = "0"; | |
if (containerVersion.debugMode) { | |
isDebug = "1"; | |
} | |
// Cloud Function Endpoint URL | |
const API_PATH = 'https://<YOUR GCP DOMAIN>.cloudfunctions.net/<CLOUD-FUNCTION-PATH>'; | |
const data = { | |
url: API_PATH, | |
body: { | |
"eventPayload": params, | |
"token": "XXXXXXXXX", // secret token without which the cloud function won't do anything | |
"attrs": { | |
"origin": "gtm", | |
"gtmContainerId": containerId, | |
"gtmDebugMode": isDebug | |
} | |
} | |
}; | |
const postHeaders = {'Content-Type': 'application/json'}; | |
let postBodyData = data.body; | |
const postBody = JSON.stringify(postBodyData); | |
const now = getTimestamp(); | |
const twoHoursAgo = now - 1000 * 60 * 60 * 2; | |
const stored_uid_ts = uid + "_timestamp"; // name of the file that tells us when this user ID's data was last requested | |
// if tpldatastorage does not have a cached file or if this file is older than 2 hours, we initiate the request to pubsub+cloud functions | |
if (!templateDataStorage.getItemCopy(uid) || templateDataStorage.getItemCopy(stored_uid_ts) < twoHoursAgo) { | |
// fire pubsub request | |
sendHttpRequest(data.url, (statusCode, headers, body) => { | |
if (statusCode >= 200 && statusCode < 300) { | |
log(body); | |
setResponseStatus(200); | |
setResponseHeader('Content-Type', 'application/json'); | |
setResponseBody(JSON.stringify(body)); | |
log('Got Response from Google Cloud Function. Storing in Template Storage Cache and returning to requestor.'); | |
templateDataStorage.setItemCopy(uid, JSON.stringify(body)); | |
templateDataStorage.setItemCopy(stored_uid_ts, now); | |
returnResponse(); | |
} else { | |
fail("request failed!"); | |
} | |
}, {headers: postHeaders, method: 'POST', timeout: 5000}, postBody); // wait max 5 sec for the response | |
} else { | |
const cached_data = templateDataStorage.getItemCopy(uid); | |
setResponseHeader('Content-Type', 'application/json'); | |
setResponseBody(cached_data); | |
log('Data for User ID ' + uid + ' found in template data cache, returning cached version.'); | |
log(cached_data); | |
returnResponse(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment