static async _processRequest()

in source/microservices/lib/responseManager.js [37:185]


    static async _processRequest(event) {
        let INVALID_PATH_ERR = {
            error: 'InvalidAction',
            message: `Invalid path request ${event.resource}, ${event.httpMethod}`
        };
        let INVALID_OP_ERR;
        if (event.queryStringParameters && event.queryStringParameters.op) {
            INVALID_OP_ERR = {
                error: 'InvalidOperation',
                message: `Invalid operation ${event.queryStringParameters.op} made to path ${event.resource}`
            };
        }
        let INVALID_REQ_ERR = {
            error: 'InvalidRequest',
            message: `Invalid http method: ${event.httpMethod} made to ${event.resource}`
        };
        let _deviceTypeManager = new DeviceTypeManager();
        let _simulationManager = new SimulationManager();
        let _operation = '';
        let _body = {};
        if (event.body) {
            _body = JSON.parse(event.body);
        }
        let data;
        if (event.resource === '/devicetypes') {
            if (event.httpMethod === 'GET') {
                if (event.queryStringParameters.op === 'list') {
                    _operation = 'retrieve device types for user';
                    console.log(['Attempting to', _operation].join(' '));
                    try {
                        data = await _deviceTypeManager.getDeviceTypes();
                        return (this._processResponse(200, data, _operation));
                    } catch (err) {
                        return (this._processResponse(err.code, err, _operation));
                    }
                } else {
                    return this._buildOutput(400, INVALID_OP_ERR);
                }
            } else if (event.httpMethod === 'POST') {
                _operation = 'create device type for user';
                console.log(['Attempting to', _operation].join(' '));
                try {
                    data = await _deviceTypeManager.createDeviceType(_body);
                    return (this._processResponse(200, data, _operation));
                } catch (err) {
                    return (this._processResponse(err.code, err, _operation));
                }
            }
            else {
                return this._buildOutput(400, INVALID_REQ_ERR);
            }
        } else if (event.resource === '/devicetypes/{typeid}') {
            if (event.httpMethod === 'DELETE') {
                _operation = ['delete device type', event.pathParameters.typeid].join(' ');
                console.log(['Attempting to', _operation].join(' '));
                try {
                    data = await _deviceTypeManager.deleteDeviceType(event.pathParameters.typeid);
                    return (this._processResponse(200, data, _operation));
                } catch (err) {
                    return (this._processResponse(err.code, err, _operation));
                }
            } else {
                return this._buildOutput(400, INVALID_REQ_ERR);
            }
        } else if (event.resource === '/simulation') {
            if (event.httpMethod === 'GET') {
                if (event.queryStringParameters.op === 'list') {
                    _operation = 'retrieve simulations';
                    console.log(['Attempting to', _operation].join(' '));
                    try {
                        data = await _simulationManager.getSimulations();
                        return (this._processResponse(200, data, _operation));
                    } catch (err) {
                        return (this._processResponse(err.code, err, _operation));
                    }
                } else if (event.queryStringParameters.op === 'getRunningStat') {
                    _operation = 'retrieve running stats';
                    console.log(['Attempting to', _operation].join(' '));
                    try {
                        data = await _simulationManager.getSimulationStats();
                        return (this._processResponse(200, data, _operation));
                    } catch (err) {
                        return (this._processResponse(err.code, err, _operation));
                    }
                } else {
                    return this._buildOutput(400, INVALID_OP_ERR);
                }
            } else if (event.httpMethod === 'POST') {
                _operation = 'create simulation for user';
                console.log(['Attempting to', _operation].join(' '));
                try {
                    data = await _simulationManager.createSimulation(_body);
                    return (this._processResponse(200, data, _operation));
                } catch (err) {
                    return (this._processResponse(err.code, err, _operation));
                }
            } else if (event.httpMethod === 'PUT') {
                _operation = 'update simulations';
                console.log(['Attempting to', _operation].join(' '));
                try {
                    data = await _simulationManager.updateSimulation(_body.action, _body.simulations);
                    return (this._processResponse(200, data, _operation));
                } catch (err) {
                    return (this._processResponse(err.code, err, _operation));
                }
            } else {
                return this._buildOutput(400, INVALID_REQ_ERR);
            }
        } else if (event.resource === '/simulation/{simid}') {
            if (event.httpMethod === 'GET') {
                _operation = ['retrieve simulation', event.pathParameters.simid].join(' ');
                console.log(['Attempting to', _operation].join(' '));
                try {
                    if (event.queryStringParameters && event.queryStringParameters.op === "list dtype attributes") {
                        const filter = event.queryStringParameters.filter;
                        data = await _simulationManager.getDeviceType(event.pathParameters.simid, filter);
                        return (this._processResponse(200, data, _operation));
                    } else {
                        data = await _simulationManager.getSimulation(event.pathParameters.simid);
                        return (this._processResponse(200, data, _operation));
                    }
                } catch (err) {
                    return (this._processResponse(err.code, err, _operation));
                }
            } else if (event.httpMethod === 'DELETE') {
                _operation = ['delete simulation', event.pathParameters.simid].join(' ');
                console.log(['Attempting to', _operation].join(' '));
                try {
                    data = await _simulationManager.deleteSimulation(event.pathParameters.simid);
                    return (this._processResponse(200, data, _operation));
                } catch (err) {
                    return (this._processResponse(err.code, err, _operation));
                }
            } else if (event.httpMethod === 'PUT') {
                _operation = ['update simulation', event.pathParameters.simid].join(' ');
                console.log(['Attempting to', _operation].join(' '));
                try {
                    data = await _simulationManager.updateSimulation(_body.action, _body.simulations);
                    return (this._processResponse(200, data, _operation));
                } catch (err) {
                    return (this._processResponse(err.code, err, _operation));
                }
            } else {
                return this._buildOutput(400, INVALID_REQ_ERR);
            }
        } else {
            return this._buildOutput(400, INVALID_PATH_ERR);
        }
    }