<commit_before>var http = require('http');
var https = require('https');
var util = require('util');

function getHueIP(ret)
{
    var options = {
        host: 'www.meethue.com',
        path: '/api/nupnp',
    }

    var chunks = ''

    var request = https.request(options, function(response) {
        response.setEncoding('utf8');
        response.on('data', function(chunk) {
            chunks = chunks + chunk
        });
        response.on('end', function() {
            try {
                ret(null, JSON.parse(chunks)[0].internalipaddress);
            }
            catch (e) {
                ret(e);
            }
        });
    });

    request.on('error', function(e) {
        ret(e);
    });

    request.end();
}

function createUserInner(attempt, bridgeIP, devicetype, username, ret)
{
    var options = {
        host: bridgeIP,
        path: '/api',
        method: 'POST',
    }

    var body = {
        devicetype: devicetype,
    }

    if (username) {
        body.username = username;
    }

    var chunks = ''
    var request = http.request(options, function(response) {
        response.setEncoding('utf8');

        response.on('data', function(chunk) {
            chunks = chunks + chunk
        });

        response.on('end', function() {
            try {
                ret(null, attempt, JSON.parse(chunks));
            }
            catch (e) {
                ret(e);
            }
        });
    });

    request.on('error', function(e) {
        ret(e);
    });

    request.write(JSON.stringify(body));

    request.end();
}

function createUser(bridgeIP, devicetype, username, ret)
{
    var MAXATTEMPTS = 30;

    function retry(err, attempt, result) {
        if (err) {
            ret(err);
        }
        else {
            console.log(util.inspect(result[0], { showHiden: true, depth: null }));

            if (result[0].error) {
                if (result[0].error.type === 101) {
                    if (attempt === MAXATTEMPTS) {
                        ret(result[0].error);
                        return;
                    }

                    if (attempt === 0) {
                        console.log('Please press the link button on the Hue bridge...');
                    }

                    setTimeout(function() {
                        createUserInner(attempt + 1, bridgeIP, devicetype, username, retry);

                    }, 1000);
                }
                else {
                    ret(result[0].error);
                }
            }
            else {
                ret(null, result);
            }
        }
    }

    createUserInner(0, bridgeIP, devicetype, username, retry);
}



getHueIP(function(err, ip) {
    if (err) {
        console.log('ERROR: ' + err);
        return;
    }

    createUser(ip, 'tkhue', '3c3b79b41b2987ef10d80fdc396a5557', function(err, response) {
        if (err) {
            console.log('ERROR: ' + JSON.stringify(err));
        }
        else {
            console.log(util.inspect(response, { showHiden: true, depth: null }));
        }
    });
});
<commit_msg>Added support for more Hue APIs<commit_after>var http = require('http');
var https = require('https');
var util = require('util');

function hueRequest(bridgeIP, username, method, api, body, ret) {
    var url = '/api';
    if (username) {
        url = url + '/' + username;
    }
    if (api) {
        url = url + api;
    }

    var options = {
        host: bridgeIP,
        method: method,
        path: url,
    }

    var chunks = ''
    var request = http.request(options, function(response) {
        response.setEncoding('utf8');

        response.on('data', function(chunk) {
            chunks = chunks + chunk
        });

        response.on('end', function() {
            try {
                ret(null, JSON.parse(chunks));
            }
            catch (e) {
                ret(e);
            }
        });
    });

    request.on('error', function(e) {
        ret(e);
    });

    if (body) {
        request.write(JSON.stringify(body));
    }

    request.end();
}

function getHueIP(ret) {
    var options = {
        host: 'www.meethue.com',
        path: '/api/nupnp',
    }

    var chunks = ''

    var request = https.request(options, function(response) {
        response.setEncoding('utf8');
        response.on('data', function(chunk) {
            chunks = chunks + chunk
        });
        response.on('end', function() {
            try {
                ret(null, JSON.parse(chunks)[0].internalipaddress);
            }
            catch (e) {
                ret(e);
            }
        });
    });

    request.on('error', function(e) {
        ret(e);
    });

    request.end();
}

function createUser(bridgeIP, devicetype, username, timeout, ret) {
    var body = {
        devicetype: devicetype,
    }

    if (username) {
        body.username = username;
    }

    var retry = function(err, attempt, result) {
        console.log('Attempt ' + attempt.toString());

        if (err) {
            ret(err);
        }
        else {
            console.log(util.inspect(result, { showHiden: true, depth: null }));

            if (result[0].error) {
                if (result[0].error.type === 101) {
                    if (attempt === timeout) {
                        ret(result[0].error);
                        return;
                    }

                    if (attempt === 0) {
                        console.log('Please press the link button on the Hue bridge...');
                    }

                    setTimeout(function() {
                        hueRequest(bridgeIP, null, 'POST', '', body, function(err, result) {
                            retry(err, attempt + 1, result);
                        });

                    }, 1000);
                }
                else {
                    ret(result[0].error);
                }
            }
            else {
                ret(null, result[0]);
            }
        }
    }

    hueRequest(bridgeIP, null, 'POST', '', body, function(err, result) {
        retry(err, 0, result);
    });
}

function getConfiguration(bridgeIP, username, ret) {
    hueRequest(bridgeIP, username, 'GET', '/config', null, ret);
}

function deleteUser(bridgeIP, username, usertodelete, ret) {
    hueRequest(bridgeIP, username, 'DELETE', '/config/whitelist/' + usertodelete, null, ret);
}

function getFullState(bridgeIP, username, ret) {
    hueRequest(bridgeIP, username, 'GET', '', null, ret);
}

function getLights(bridgeIP, username, ret) {
    hueRequest(bridgeIP, username, 'GET', '/lights', null, ret);
}



getHueIP(function(err, ip) {
    if (err) {
        console.log('ERROR: ' + err);
        return;
    }

    //createUser(ip, 'tkhue', '3c3b79b41b2987ef10d80fdc396a5557', 5,
    //getConfiguration(ip, '3c3b79b41b2987ef10d80fdc396a5557',
    //deleteUser(ip, '3c3b79b41b2987ef10d80fdc396a5557', 'ada49f56b77363244d19e44d6d22aa0b',
    //getFullState(ip, '3c3b79b41b2987ef10d80fdc396a5557',
    getLights(ip, '3c3b79b41b2987ef10d80fdc396a5557',
        function(err, response) {
        if (err) {
            console.log('ERROR: ' + JSON.stringify(err));
        }
        else {
            console.log(util.inspect(response, { showHiden: true, depth: null }));
        }
    });

});

{'fraction_non_alphanumeric': 0.1408277557710695, 'fraction_numerical': 0.02526977188908619, 'mean_word_length': 1.572733661278988, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4051448', 'n_tokens_mistral': 2352, 'n_tokens_neox': 2170, 'n_words': 424}