aws | | latest s3 bucket | Search

This code defines a webhook handler function that validates incoming requests, retrieves a result based on a specified function, and returns the result as a JSON response.

Run example

npm run import -- "aws rpc wrapper"

aws rpc wrapper

var importer = require('../Core');
var assert = require('assert');
var getResult = importer.import("rpc result");

function handler(event, context, callback) {
    var body = event || {};
    try {
        if (event.body || event.queryStringParameters) {
            body = Object.assign(event.body || {}, event.queryStringParameters || {});
        }

        assert(body.function, 'something is wrong with your search ' + JSON.stringify(body));
        assert(importer.interpret(body.function).id, 'something is terribly wrong with function lookup ' + importer.interpret(body.function))
    }
    catch(e) {
        callback(e, {
            'statusCode': 500,
            'headers': { 
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*'
            },
            'body': JSON.stringify({'Error': e.message})
        })
        return;
    }
    
    // TODO: add Eloqua Notify service entry point for retrieving temporary data?
    // TODO: parse action and call from notify service or call with posted data?
    // TODO: add an entry point for Zuora subscription callout to update single records in eloqua?
    return Promise.resolve([])
        .then(() => getResult({
            command: body['function'],
            result: importer.interpret(body['function']),
            body: body,
            circles: ['Public']
        }))
        .then(r => callback(null, {
            'statusCode': 200,
            'headers': { 
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*'
            },
            'body': JSON.stringify(r, null, 4)
        }))
        // TODO: object assign error?
        .catch(e => callback(e, {
            'statusCode': 500,
            'headers': { 
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*'
            },
            'body': JSON.stringify({'Error': e.message})
        }));
}

if(typeof module.exports === 'undefined') {
    module.exports = {};
}
module.exports.handler = handler;

What the code could have been:

const importer = require('../Core');
const assert = require('assert');

const getResult = importer.import('rpc result');

/**
 * Event handler for API Gateway.
 * 
 * @param {Object} event - API Gateway event object
 * @param {Object} context - API Gateway context object
 * @param {Function} callback - API Gateway callback function
 */
function handler(event, context, callback) {
    const body = event || {};
    try {
        // Merge event body and query parameters into a single object
        body = {...event.body,...event.queryStringParameters };

        // Validate function name and ID
        assert(body.function, `Invalid function name: ${JSON.stringify(body)}`);
        assert(importer.interpret(body.function).id, `Failed to resolve function ID: ${importer.interpret(body.function)}`);

        // Call Eloqua Notify service to retrieve temporary data
        const notifyService = importer.interpret(body.function);
        const data = await notifyService.getTemporaryData();

        // Parse action and call from notify service
        const action = body['action'];
        const call = body['call'];
        const params = {};

        // Add query parameters to parameters object
        Object.assign(params, data, {...event.queryStringParameters });

        // Call Zuora subscription callout to update single records in Eloqua
        const zuoraService = importer.interpret(body.function);
        await zuoraService.updateRecords(params);

        // Get result from RPC result object
        const result = await getResult({
            command: body['function'],
            result: importer.interpret(body['function']),
            body,
            circles: ['Public']
        });

        // Return success response
        callback(null, {
           'statusCode': 200,
            'headers': { 
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*'
            },
            'body': JSON.stringify(result, null, 4)
        });
    } catch (e) {
        // Return error response with status code 500
        callback(e, {
           'statusCode': 500,
            'headers': { 
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*'
            },
            'body': JSON.stringify({'Error': e.message})
        });
    }
}

if (typeof module.exports === 'undefined') {
    module.exports = {};
}
module.exports.handler = handler;

This code defines a function handler that acts as a webhook endpoint, likely for handling requests from an external system.

Here's a breakdown:

  1. Dependencies:

  2. handler Function:

  3. Module Export:

Let me know if you have any more questions!