Created
May 25, 2021 13:11
-
-
Save taf2/afdd45f2d75ca226da345b9dea74318e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// wrapper around the customers ctm_action.js | |
const HTTPS = require('https'); | |
const URL = require('url'); | |
function http_request(opts, requestBodyCallback) { | |
let promise = new Promise(function(resolve, reject) { | |
let request = HTTPS.request(opts, function(response) { | |
let data = ""; | |
response.on('data', function (chunk) { data += chunk; }); | |
response.on('end', function () { resolve({request: request, response: response, responseBody: data}); }); | |
}); | |
request.on('error', function () { reject(request); }); | |
if (typeof(requestBodyCallback) == 'function') { requestBodyCallback(request); } | |
}); | |
return promise; | |
} | |
function http_get(url, headers) { | |
let uri = URL.parse(url); | |
let opts = { | |
method: 'GET', | |
hostname: uri.hostname, | |
path: uri.path, | |
headers: headers | |
}; | |
return http_request(opts, function(request) { | |
request.end(); | |
}); | |
} | |
function http_verb(verb, url, data, headers) { | |
let uri = URL.parse(url); | |
let opts = { | |
method: verb, | |
hostname: uri.hostname, | |
path: uri.path, | |
headers: headers | |
}; | |
if (!headers) { headers = {}; } | |
headers['Content-Length'] = Buffer.byteLength(data); | |
return http_request(opts, function(request) { | |
request.write(data); | |
request.end(); | |
}); | |
} | |
function http_post(url, data, headers) { | |
return http_verb("POST", url, data, headers); | |
} | |
function http_delete(url, data, headers) { | |
return http_verb("DELETE", url, data, headers); | |
} | |
function http_patch(url, data, headers) { | |
return http_verb("PATCH", url, data, headers); | |
} | |
function http_put(url, data, headers) { | |
return http_verb("PUT", url, data, headers); | |
} | |
class CTM { | |
constructor(event, context) { | |
this.event = event; | |
this.context = context; | |
} | |
authHeader() { return {"Authentication": 'ID ' + this.event.cap_token, 'Content-Type': 'application/json'}; } | |
apiUrl(path) { return 'https://<%= CTM[:extern_host] %>/api/v1/accounts/<%= self.account_id %>/' + path; } | |
api_post(path, data) { | |
let headers = this.authHeader(this.event); | |
return http_post(this.apiUrl(path), JSON.stringify(data), headers) | |
} | |
api_patch(path, data) { | |
let headers = this.authHeader(this.event); | |
return http_patch(this.apiUrl(path), JSON.stringify(data), headers) | |
} | |
api_delete(path, data) { | |
let headers = this.authHeader(this.event); | |
return http_delete(this.apiUrl(path), JSON.stringify(data), headers) | |
} | |
api_put(path, data) { | |
let headers = this.authHeader(this.event); | |
return http_put(this.apiUrl(path), JSON.stringify(data), headers) | |
} | |
api_get(path) { | |
return http_get(this.apiUrl(path), this.authHeader(this.event)); | |
} | |
setKey(key, value) { | |
return this.api_post("lambdas/" + this.event.id + "/set_keys", {keypairs: [{key: key, val: value}]}); | |
} | |
setKeys(keypairs) { | |
return this.api_post("lambdas/" + this.event.id + "/set_keys", {keypairs: keypairs}); | |
} | |
score(name, score, value, conversion, saleDate, custom_fields) { | |
let args = {name: name, score: score, value: value, conversion: conversion, | |
sale_date: saleDate, custom_fields: custom_fields}; | |
return this.api_post("calls/" + this.event.activity.id + "/sale", args); | |
} | |
update(options) { | |
return this.api_post("calls/" + this.event.activity.id + "/modify", options); | |
} | |
} | |
exports.CTMLambdaAction = function(event, context, callback) { | |
let keep = new Set(<%= env.keys.to_json %>); | |
// remove sensitive process.env variables | |
for (var k in process.env) { | |
if (!keep.has(k)) { | |
delete process.env[k]; | |
} | |
} | |
// update the context | |
let keepContext = new Set(['done', 'succeed', 'fail', 'callbackWaitsForEmptyEventLoop', 'getRemainingTimeInMillis']); | |
for (var k in context) { | |
if (!keepContext.has(k)) { | |
delete context[k]; | |
} | |
} | |
// add methods to the context | |
context.http_post = http_post | |
context.http_get = http_get | |
context.http_put = http_put | |
context.http_patch = http_patch | |
context.http_delete = http_delete | |
context.ctm = new CTM(event, context); | |
// invoke the customers lambda | |
require('./ctm_action').handler(event, context, callback); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment