<commit_before>(function() {
    var soap = require('soap');

    var clang = {};

    var logger;

    clang.init = function (first, second) {
        if (arguments.length === 1) {
            clang.callbackMain = first;
            logger            = console;
        } else if (arguments.length === 2) {
            clang.callbackMain = second;
            logger             = first;
        } else {
            throw new Error('No callback supplied when calling clang.init');
        }
        clang.roundTrips=0;
        clang.maxAttempts=10;

        var url = 'https://secure.myclang.com/app/api/soap/public/index.php?wsdl';
        logger.log('Fetching WSDL and preparing SOAP client...', ['SOAP']);

        soap.createClient(url, clang.initCallback);
    };

    clang.uuid = function (uuid) {
        clang.uuid = uuid;
    };

    clang.initCallback = function(err, client) {
        logger.log('SOAP client created', ['SOAP']);

        clang.client  = client;
        clang.soap    = client.describe();
        clang.objects = {};

        var resourceMethods=[]; //TODO not used

        function capitalize(string) {
            return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
        }

        //iterate through all methods supplied in the Clang API
        for (var key in clang.soap.clangAPI.clangPort) {
            var methodNameParts = key.split('_');
            var objectName      = methodNameParts[0];
            var methodName      = methodNameParts[1];

            var method = clang.soap.clangAPI.clangPort[key];
            if (!clang.objects[objectName]) {
                clang.objects[objectName] = {};
            }

            clang.objects[objectName][methodName] = function(clangMethodName) {
                //build new function
                return function (args, parentCallback) {
                    logger.log('Invoking SOAP method: ' + clangMethodName + '('+JSON.stringify(args)+')', ['SOAP']);
                    clang.roundTrips=0;
                    clang.maxAttempts=10;

                    args = clang.prepareInput(clangMethodName, args);

                    client[clangMethodName](args, function(err, result) {
                        //process results of SOAP call
                        logger.log({ msg: 'lastRequest', lastRequest: client.lastRequest}, ['SOAP']);
                        clang.clientCallback(err, result, clangMethodName, args, undefined, undefined, parentCallback);
                    });
                    logger.log('Awaiting SOAP response...', ['SOAP']);
                }
            }(key);
        
            //some methods do not output records directly, instead they return a reference to a resource set
            //this bit within if captures these methods and adds a property to indicate
            //that the output has to be fetched through a resourceSet
            if (method.output.code == "xsd:integer" &&
                method.output.msg  == "xsd:long" && 
                [
                    'customer_getTotalNumberOfCustomers',
                    'magentoEmail_insert',
                    'magento_executeAbandonedCart'
                ].indexOf(key) === -1) { //the output of these methods is not referring to a resource but have similar output as the ones that do

                method.useResource = true;
                resourceMatch = methodName.match(/get(\w+)Set/);

                if (resourceMatch) {
                    method.resourceMethod = resourceMatch[1].toLowerCase()+'Set_get'+resourceMatch[1]+'s';
                } else if (methodName == 'getMembers') {
                    method.resourceMethod = 'customerSet_getCustomers';
                } else {
                    method.resourceMethod = objectName+'Set_get'+capitalize(objectName)+'s';
                }
                method.resourceMethod = method.resourceMethod.replace(/ys$/, 'ies');

                if (resourceMethods.indexOf(method.resourceMethod) === -1) {
                    resourceMethods.push(method.resourceMethod);
                    //resourceMethods.push(clang.soap.clangAPI.clangPort[method.resourceMethod]) ;
                }
            }

        }

        clang.callbackMain(null, clang);
    };

    clang.prepareInput = function(clangMethodName, args) {
        var inputDefinition = clang.soap.clangAPI.clangPort[clangMethodName].input;

        //To understand this code better, check how the entire interface definition
        // looks like in JSON created by createClient
        // a copy of this definition (wsdl version 1.18) has been placed here ????

        var filterParams = function (inputObject, args, optionsIdentifier, optionIdentifier) {
            var baseFields = [];
            //iterate over the input object's fields to get the baseFields like emailAddress, firstname etc
            for (var fieldName in inputObject) {
                if (inputObject.hasOwnProperty(fieldName) &&
                    typeof inputObject[fieldName] === 'string' //for example "xsd:string" and not another object like accounts
                ) {
                    baseFields.push(fieldName);
                }

            }

            //Move all none base fields to the options collection with name-value paired objects
            var filteredParams = {}, options = {};
            options[optionIdentifier] = [];
            for (var fieldName in args) {
                if (args.hasOwnProperty(fieldName) && fieldName !== 'uuid') {
                    if (baseFields.indexOf(fieldName) > -1) {
                        filteredParams[fieldName] = args[fieldName];
                    } else {
                        options[optionIdentifier].push({
                            name: fieldName,
                            value: args[fieldName]
                        });
                    }
                }
            }

            if (options[optionIdentifier].length > 0) {
                filteredParams[optionsIdentifier] = options;
            }

            return filteredParams;
        };

        var tempArgs;
        if (clangMethodName.match('^email_sendTo')) {
            tempArgs = filterParams(inputDefinition, args, 'manualOptions', 'Option');
            tempArgs.uuid = args.uuid || clang.uuid;

            return tempArgs;
        }

        for (var key in inputDefinition) {
            if (inputDefinition.hasOwnProperty(key) &&
                ['customer', 'group', 'sms', 'email'].indexOf(key) > -1 &&
                Object.prototype.toString.call(inputDefinition[key])=== '[object Object]'
            ) {
                if (key === 'customer') {
                    var inputObject = inputDefinition[key]; //either the customer, group, sms or email object

                    //create the new/rearranged input object with all properties moved to a nested object
                    //  that nested object itself got al its none base fields moved to an options collection
                    tempArgs = {
                        uuid: args.uuid || clang.uuid,
                    };
                    tempArgs[key] = filterParams(inputObject, args, 'options', 'CustomerOption');
                } else { //key => group, sms or email
                    tempArgs = {
                        uuid: args.uuid || clang.uuid,
                    };
                    tempArgs[key] = args;

                    delete tempArgs[key].uuid;
                }

                return tempArgs;
            }
        }

        //if we're here then no rearranging occured
        args.uuid = args.uuid || clang.uuid;

        return args;
    };

    clang.clientCallback = function(err, result, clangMethodName, args, resourceId, resourceMethod, parentCallback) {
        logger.log('SOAP response arrived for method' + clangMethodName, ['SOAP']);
        
        //Check for actual SOAP Fault generated by callback in soap/lib/client line 152
        if (err) {
            return parentCallback(err);
        }
        //Check for No SOAP Fault, but result code not 0
        if (result.code != 0) {
            return parentCallback(new Error('No SOAP Fault, but result code not 0: ' + JSON.stringify(result.msg)));
        }
        //Check for No SOAP Fault, but result.msg = false (probably delete that didn't work out)
        if (result.msg === false) {
            return parentCallback(new Error('No SOAP Fault, but result msg is false (delete failed?): ' + JSON.stringify(result.msg)));
        }
        //checks maximum amount of roundtrips (due to nested call)
        if (clang.roundTrips > clang.maxAttempts) {
            return parentCallback(new Error('Exceeded the maximum number of attempts'));
        }
        //When resource is READY fetch the data using the resourceMethod
        if (clangMethodName === 'resource_getById' && result.msg.status === 'ERROR') {
            return parentCallback(new Error('Error getting resource (probably incorrect x_getByObject request parameters)'));
        }

        var method = clang.soap.clangAPI.clangPort[clangMethodName];

        //Check for result that should be fetched using new SOAP call
        if (method && method.useResource || //first attempt trying if resource is READY
            clangMethodName === 'resource_getById' && result.msg.status === 'PROCESSING'    //next attempts trying if resource is READY
        ) {

            resourceId     = resourceId     || result.msg ;//    = (typeof result.msg === "object" ? result.msg.id : result.msg );
            resourceMethod = resourceMethod || method.resourceMethod;
            var resourceArgs   = {
                uuid: args.uuid,
                resourceId: resourceId
            };

            logger.log('Invoking SOAP method to see if resourceId is READY: ' + resourceId, ['SOAP']);
            clang.roundTrips++;
            clang.client['resource_getById'](resourceArgs, function(err, result) {
                /* result looks like: {
                      "code": 0,
                      "msg": {
                        "id": "147082",
                        "type": "CUSTOMER_SET",
                        "status": "READY",
                        "size": "1"
                      }
                    }
                */
                clang.clientCallback(err, result, 'resource_getById', args, resourceId, resourceMethod, parentCallback);
            });
            clang.roundTrips--;
            logger.log('Awaiting SOAP response for resourceId: '+resourceId+'...', ['SOAP']);

            return;
        }
        if (clangMethodName === 'resource_getById' && result.msg.status === 'READY') {
            logger.log('Resource of resourceId: '+resourceId+' is READY ('+result.msg.size+ ' records)', ['SOAP']);
            if (result.msg.size > 0) {
                var resourceArgs   = {
                    uuid: args.uuid,
                    resourceId: resourceId,
                    offset: 0,
                    size: 50
                };
                clang.client[resourceMethod](resourceArgs, function(err, result) {
                    clang.clientCallback(err, result, 'dummy', null, null, null, parentCallback)
                });
                logger.log('Awaiting SOAP response for resource records of resourceId: ' + resourceId + '...', ['SOAP']);
            } else {
                logger.log('No records found in SOAP response', ['SOAP']);
                return parentCallback(null, []);
            }

            return;
        }

        //we have 0 or more results. Normalize it to just the array to output
        if (result.msg === true) {
            logger.log('SOAP success', ['SOAP']);
            return parentCallback(null, [{msg: true}]);
        }

        var msgKeys = Object.keys(result.msg);
        if (msgKeys.length === 0) {
            logger.log('No records found in SOAP response', ['SOAP']);
            return parentCallback(null, []);
        }
        if (msgKeys.length === 1 && result.msg[msgKeys[0]] instanceof Array) {
            logger.log('SOAP msg array returned', ['SOAP']); //{ code: 0, msg: { Group: [Object] } }
            return parentCallback(null, result.msg[msgKeys[0]]);
        }
        if (result.msg) { // 1 record from x_getById or x_create
            logger.log('SOAP msg object returned', ['SOAP']);
            return parentCallback(null, [result.msg]);
        }
      
    };

    exports.init = clang.init;
    exports.uuid = clang.uuid;
})();<commit_msg>Output definition for resourced changed long->int<commit_after>(function() {
    var soap = require('soap');

    var clang = {};

    var logger;

    clang.init = function (first, second) {
        if (arguments.length === 1) {
            clang.callbackMain = first;
            logger            = console;
        } else if (arguments.length === 2) {
            clang.callbackMain = second;
            logger             = first;
        } else {
            throw new Error('No callback supplied when calling clang.init');
        }
        clang.roundTrips=0;
        clang.maxAttempts=10;

        var url = 'https://secure.myclang.com/app/api/soap/public/index.php?wsdl';
        logger.log('Fetching WSDL and preparing SOAP client...', ['SOAP']);

        soap.createClient(url, clang.initCallback);
    };

    clang.uuid = function (uuid) {
        clang.uuid = uuid;
    };

    clang.initCallback = function(err, client) {
        logger.log('SOAP client created', ['SOAP']);

        clang.client  = client;
        clang.soap    = client.describe();
        clang.objects = {};

        var resourceMethods=[]; //TODO not used

        function capitalize(string) {
            return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
        }

        //iterate through all methods supplied in the Clang API
        for (var key in clang.soap.clangAPI.clangPort) {
            var methodNameParts = key.split('_');
            var objectName      = methodNameParts[0];
            var methodName      = methodNameParts[1];

            var method = clang.soap.clangAPI.clangPort[key];
            if (!clang.objects[objectName]) {
                clang.objects[objectName] = {};
            }

            clang.objects[objectName][methodName] = function(clangMethodName) {
                //build new function
                return function (args, parentCallback) {
                    logger.log('Invoking SOAP method: ' + clangMethodName + '('+JSON.stringify(args)+')', ['SOAP']);
                    clang.roundTrips=0;
                    clang.maxAttempts=10;

                    args = clang.prepareInput(clangMethodName, args);

                    client[clangMethodName](args, function(err, result) {
                        //process results of SOAP call
                        logger.log({ msg: 'lastRequest', lastRequest: client.lastRequest}, ['SOAP']);
                        clang.clientCallback(err, result, clangMethodName, args, undefined, undefined, parentCallback);
                    });
                    logger.log('Awaiting SOAP response...', ['SOAP']);
                }
            }(key);
        
            //some methods do not output records directly, instead they return a reference to a resource set
            //this bit within if captures these methods and adds a property to indicate
            //that the output has to be fetched through a resourceSet
            if (typeof method.output.code == 'string' && method.output.code.match(/^xsd:[int|long]/) &&
                typeof method.output.msg  == 'string' && method.output.msg.match(/^xsd:[int|long]/) && 
                [
                    'customer_getTotalNumberOfCustomers',
                    'magentoEmail_insert',
                    'magento_executeAbandonedCart'
                ].indexOf(key) === -1) { //the output of these methods is not referring to a resource but have similar output as the ones that do

                method.useResource = true;
                resourceMatch = methodName.match(/get(\w+)Set/);

                if (resourceMatch) {
                    method.resourceMethod = resourceMatch[1].toLowerCase()+'Set_get'+resourceMatch[1]+'s';
                } else if (methodName == 'getMembers') {
                    method.resourceMethod = 'customerSet_getCustomers';
                } else {
                    method.resourceMethod = objectName+'Set_get'+capitalize(objectName)+'s';
                }
                method.resourceMethod = method.resourceMethod.replace(/ys$/, 'ies');

                if (resourceMethods.indexOf(method.resourceMethod) === -1) {
                    resourceMethods.push(method.resourceMethod);
                    //resourceMethods.push(clang.soap.clangAPI.clangPort[method.resourceMethod]) ;
                }
            }

        }

        clang.callbackMain(null, clang);
    };

    clang.prepareInput = function(clangMethodName, args) {
        var inputDefinition = clang.soap.clangAPI.clangPort[clangMethodName].input;

        //To understand this code better, check how the entire interface definition
        // looks like in JSON created by createClient
        // a copy of this definition (wsdl version 1.18) has been placed here ????

        var filterParams = function (inputObject, args, optionsIdentifier, optionIdentifier) {
            var baseFields = [];
            //iterate over the input object's fields to get the baseFields like emailAddress, firstname etc
            for (var fieldName in inputObject) {
                if (inputObject.hasOwnProperty(fieldName) &&
                    typeof inputObject[fieldName] === 'string' //for example "xsd:string" and not another object like accounts
                ) {
                    baseFields.push(fieldName);
                }

            }

            //Move all none base fields to the options collection with name-value paired objects
            var filteredParams = {}, options = {};
            options[optionIdentifier] = [];
            for (var fieldName in args) {
                if (args.hasOwnProperty(fieldName) && fieldName !== 'uuid') {
                    if (baseFields.indexOf(fieldName) > -1) {
                        filteredParams[fieldName] = args[fieldName];
                    } else {
                        options[optionIdentifier].push({
                            name: fieldName,
                            value: args[fieldName]
                        });
                    }
                }
            }

            if (options[optionIdentifier].length > 0) {
                filteredParams[optionsIdentifier] = options;
            }

            return filteredParams;
        };

        var tempArgs;
        if (clangMethodName.match('^email_sendTo')) {
            tempArgs = filterParams(inputDefinition, args, 'manualOptions', 'Option');
            tempArgs.uuid = args.uuid || clang.uuid;

            return tempArgs;
        }

        for (var key in inputDefinition) {
            if (inputDefinition.hasOwnProperty(key) &&
                ['customer', 'group', 'sms', 'email'].indexOf(key) > -1 &&
                Object.prototype.toString.call(inputDefinition[key])=== '[object Object]'
            ) {
                if (key === 'customer') {
                    var inputObject = inputDefinition[key]; //either the customer, group, sms or email object

                    //create the new/rearranged input object with all properties moved to a nested object
                    //  that nested object itself got al its none base fields moved to an options collection
                    tempArgs = {
                        uuid: args.uuid || clang.uuid,
                    };
                    tempArgs[key] = filterParams(inputObject, args, 'options', 'CustomerOption');
                } else { //key => group, sms or email
                    tempArgs = {
                        uuid: args.uuid || clang.uuid,
                    };
                    tempArgs[key] = args;

                    delete tempArgs[key].uuid;
                }

                return tempArgs;
            }
        }

        //if we're here then no rearranging occured
        args.uuid = args.uuid || clang.uuid;

        return args;
    };

    clang.clientCallback = function(err, result, clangMethodName, args, resourceId, resourceMethod, parentCallback) {
        logger.log('SOAP response arrived for method' + clangMethodName, ['SOAP']);
        
        //Check for actual SOAP Fault generated by callback in soap/lib/client line 152
        if (err) {
            return parentCallback(err);
        }
        //Check for No SOAP Fault, but result code not 0
        if (result.code != 0) {
            return parentCallback(new Error('No SOAP Fault, but result code not 0: ' + JSON.stringify(result.msg)));
        }
        //Check for No SOAP Fault, but result.msg = false (probably delete that didn't work out)
        if (result.msg === false) {
            return parentCallback(new Error('No SOAP Fault, but result msg is false (delete failed?): ' + JSON.stringify(result.msg)));
        }
        //checks maximum amount of roundtrips (due to nested call)
        if (clang.roundTrips > clang.maxAttempts) {
            return parentCallback(new Error('Exceeded the maximum number of attempts'));
        }
        //When resource is READY fetch the data using the resourceMethod
        if (clangMethodName === 'resource_getById' && result.msg.status === 'ERROR') {
            return parentCallback(new Error('Error getting resource (probably incorrect x_getByObject request parameters)'));
        }

        var method = clang.soap.clangAPI.clangPort[clangMethodName];

        //Check for result that should be fetched using new SOAP call
        if (method && method.useResource || //first attempt trying if resource is READY
            clangMethodName === 'resource_getById' && result.msg.status === 'PROCESSING'    //next attempts trying if resource is READY
        ) {

            resourceId     = resourceId     || result.msg ;//    = (typeof result.msg === "object" ? result.msg.id : result.msg );
            resourceMethod = resourceMethod || method.resourceMethod;
            var resourceArgs   = {
                uuid: args.uuid,
                resourceId: resourceId
            };

            logger.log('Invoking SOAP method to see if resourceId is READY: ' + resourceId, ['SOAP']);
            clang.roundTrips++;
            clang.client['resource_getById'](resourceArgs, function(err, result) {
                /* result looks like: {
                      "code": 0,
                      "msg": {
                        "id": "147082",
                        "type": "CUSTOMER_SET",
                        "status": "READY",
                        "size": "1"
                      }
                    }
                */
                clang.clientCallback(err, result, 'resource_getById', args, resourceId, resourceMethod, parentCallback);
            });
            clang.roundTrips--;
            logger.log('Awaiting SOAP response for resourceId: '+resourceId+'...', ['SOAP']);

            return;
        }
        if (clangMethodName === 'resource_getById' && result.msg.status === 'READY') {
            logger.log('Resource of resourceId: '+resourceId+' is READY ('+result.msg.size+ ' records)', ['SOAP']);
            if (result.msg.size > 0) {
                var resourceArgs   = {
                    uuid: args.uuid,
                    resourceId: resourceId,
                    offset: 0,
                    size: 50
                };
                clang.client[resourceMethod](resourceArgs, function(err, result) {
                    clang.clientCallback(err, result, 'dummy', null, null, null, parentCallback)
                });
                logger.log('Awaiting SOAP response for resource records of resourceId: ' + resourceId + '...', ['SOAP']);
            } else {
                logger.log('No records found in SOAP response', ['SOAP']);
                return parentCallback(null, []);
            }

            return;
        }

        //we have 0 or more results. Normalize it to just the array to output
        if (result.msg === true) {
            logger.log('SOAP success', ['SOAP']);
            return parentCallback(null, [{msg: true}]);
        }

        var msgKeys = Object.keys(result.msg);
        if (msgKeys.length === 0) {
            logger.log('No records found in SOAP response', ['SOAP']);
            return parentCallback(null, []);
        }
        if (msgKeys.length === 1 && result.msg[msgKeys[0]] instanceof Array) {
            logger.log('SOAP msg array returned', ['SOAP']); //{ code: 0, msg: { Group: [Object] } }
            return parentCallback(null, result.msg[msgKeys[0]]);
        }
        if (result.msg) { // 1 record from x_getById or x_create
            logger.log('SOAP msg object returned', ['SOAP']);
            return parentCallback(null, [result.msg]);
        }
      
    };

    exports.init = clang.init;
    exports.uuid = clang.uuid;
})();
{'fraction_non_alphanumeric': 0.11107577323686094, 'fraction_numerical': 0.0037369802019559515, 'mean_word_length': 1.6918138041733548, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1243946', 'n_tokens_mistral': 6566, 'n_tokens_neox': 6202, 'n_words': 1693}