Skip to main content
Commonmark migration
Source Link

###VectorizedIdList

VectorizedIdList

###VectorizedIdList

VectorizedIdList

replaced http://stackapps.com/ with https://stackapps.com/
Source Link

The problem is that the 'vectorized list' is part of the url path, which much remain below a certain length (around 240) to avoid 400 (Bad Request) errors400 (Bad Request) errors.

var apiKey = 'qgAq_KfDu0KYzlNG-qaTuw';
var factory = Soapi.RouteFactory("api.meta.stackoverflow.com", apiKey);
factory.Users({ pagesize: 100 }).getResponse(function(data)
{
    // 240 is a safe value for max path length otherwise you risk 400 (Bad Request) errors
    // See httphttps://stackapps.com/questions/889/dev-tip-how-to-batch-up-multiple-id-requests-to-maximize-economy-and-still-avoid/1021#1021

    var idList = new Soapi.Utilities
        .VectorizedIdList(240 - Soapi.Routes.UsersByIdReputationRouteMap.routeFormat.length);
    for (var i = 0; i < data.items.length; i++)
    {
        idList.add(data.items[i].user_id);
    }

    // so now we have a VectorizedIdList loaded with 100 user id values
    var batches = idList.getBatches();

    // batches:            
    //    [0]: [1, 811, 22656, 22164, 23354, 658, 9314, 18393, 98786, 130154, 3043, 2915, 115866, 1288, 12950, 132636, 59303, 1782, 13295, 22459, 136558, 17174, 52443, 16587, 18511, 14048, 130127, 59808, 27414, 132480, 23921, 2598, 13531, 4660, 52738, 1069, 2509]
    //    [1]: [131779, 95589, 893, 33708, 55747, 138112, 146126, 130914, 745, 131932, 139541, 130090, 101371, 140478, 2961, 141911, 132632, 75852, 135200, 1337, 54262, 8435, 76337, 137798, 69307, 3, 56338, 138822, 89334, 4, 91, 133733, 3724, 572, 102937, 47529, 137184, 5190]
    //    [2]: [130109, 74022, 84237, 419, 131713, 42471, 132223, 9345, 44765, 132073, 6309, 14148, 56555, 6258, 16417, 20654, 44330, 16487, 68727, 2, 146719, 133301, 140171, 63550, 130494]
    //    length: 3

    for (var i = 0; i < batches.length; i++)
    {
        // each batch will be a list of user id that will contain the
        // maximum number of items and still remain under the 240 length limit

        factory
        .UsersById(batches[i], { pagesize: batches[i].length })
        .getResponse(function(data)
        {
            // do something interesting with your batch of users here
            alert(data.items.length);
        }, function(error)
        {
            alert(error.message);
        });
    }


});

The problem is that the 'vectorized list' is part of the url path, which much remain below a certain length (around 240) to avoid 400 (Bad Request) errors.

var apiKey = 'qgAq_KfDu0KYzlNG-qaTuw';
var factory = Soapi.RouteFactory("api.meta.stackoverflow.com", apiKey);
factory.Users({ pagesize: 100 }).getResponse(function(data)
{
    // 240 is a safe value for max path length otherwise you risk 400 (Bad Request) errors
    // See http://stackapps.com/questions/889/dev-tip-how-to-batch-up-multiple-id-requests-to-maximize-economy-and-still-avoid/1021#1021

    var idList = new Soapi.Utilities
        .VectorizedIdList(240 - Soapi.Routes.UsersByIdReputationRouteMap.routeFormat.length);
    for (var i = 0; i < data.items.length; i++)
    {
        idList.add(data.items[i].user_id);
    }

    // so now we have a VectorizedIdList loaded with 100 user id values
    var batches = idList.getBatches();

    // batches:            
    //    [0]: [1, 811, 22656, 22164, 23354, 658, 9314, 18393, 98786, 130154, 3043, 2915, 115866, 1288, 12950, 132636, 59303, 1782, 13295, 22459, 136558, 17174, 52443, 16587, 18511, 14048, 130127, 59808, 27414, 132480, 23921, 2598, 13531, 4660, 52738, 1069, 2509]
    //    [1]: [131779, 95589, 893, 33708, 55747, 138112, 146126, 130914, 745, 131932, 139541, 130090, 101371, 140478, 2961, 141911, 132632, 75852, 135200, 1337, 54262, 8435, 76337, 137798, 69307, 3, 56338, 138822, 89334, 4, 91, 133733, 3724, 572, 102937, 47529, 137184, 5190]
    //    [2]: [130109, 74022, 84237, 419, 131713, 42471, 132223, 9345, 44765, 132073, 6309, 14148, 56555, 6258, 16417, 20654, 44330, 16487, 68727, 2, 146719, 133301, 140171, 63550, 130494]
    //    length: 3

    for (var i = 0; i < batches.length; i++)
    {
        // each batch will be a list of user id that will contain the
        // maximum number of items and still remain under the 240 length limit

        factory
        .UsersById(batches[i], { pagesize: batches[i].length })
        .getResponse(function(data)
        {
            // do something interesting with your batch of users here
            alert(data.items.length);
        }, function(error)
        {
            alert(error.message);
        });
    }


});

The problem is that the 'vectorized list' is part of the url path, which much remain below a certain length (around 240) to avoid 400 (Bad Request) errors.

var apiKey = 'qgAq_KfDu0KYzlNG-qaTuw';
var factory = Soapi.RouteFactory("api.meta.stackoverflow.com", apiKey);
factory.Users({ pagesize: 100 }).getResponse(function(data)
{
    // 240 is a safe value for max path length otherwise you risk 400 (Bad Request) errors
    // See https://stackapps.com/questions/889/dev-tip-how-to-batch-up-multiple-id-requests-to-maximize-economy-and-still-avoid/1021#1021

    var idList = new Soapi.Utilities
        .VectorizedIdList(240 - Soapi.Routes.UsersByIdReputationRouteMap.routeFormat.length);
    for (var i = 0; i < data.items.length; i++)
    {
        idList.add(data.items[i].user_id);
    }

    // so now we have a VectorizedIdList loaded with 100 user id values
    var batches = idList.getBatches();

    // batches:            
    //    [0]: [1, 811, 22656, 22164, 23354, 658, 9314, 18393, 98786, 130154, 3043, 2915, 115866, 1288, 12950, 132636, 59303, 1782, 13295, 22459, 136558, 17174, 52443, 16587, 18511, 14048, 130127, 59808, 27414, 132480, 23921, 2598, 13531, 4660, 52738, 1069, 2509]
    //    [1]: [131779, 95589, 893, 33708, 55747, 138112, 146126, 130914, 745, 131932, 139541, 130090, 101371, 140478, 2961, 141911, 132632, 75852, 135200, 1337, 54262, 8435, 76337, 137798, 69307, 3, 56338, 138822, 89334, 4, 91, 133733, 3724, 572, 102937, 47529, 137184, 5190]
    //    [2]: [130109, 74022, 84237, 419, 131713, 42471, 132223, 9345, 44765, 132073, 6309, 14148, 56555, 6258, 16417, 20654, 44330, 16487, 68727, 2, 146719, 133301, 140171, 63550, 130494]
    //    length: 3

    for (var i = 0; i < batches.length; i++)
    {
        // each batch will be a list of user id that will contain the
        // maximum number of items and still remain under the 240 length limit

        factory
        .UsersById(batches[i], { pagesize: batches[i].length })
        .getResponse(function(data)
        {
            // do something interesting with your batch of users here
            alert(data.items.length);
        }, function(error)
        {
            alert(error.message);
        });
    }


});
Source Link
Sky Sanders
  • 12.1k
  • 3
  • 32
  • 60

###VectorizedIdList

Many routes accept a 'vectorized list of identifiers'. This allows you to retrieve data for multiple items and helps maximize efficiency and performance.

The problem is that the 'vectorized list' is part of the url path, which much remain below a certain length (around 240) to avoid 400 (Bad Request) errors.

the VectorizedIdList allows you to add an arbitrary number of values and will emit batches that will result in a url that is withing length tolerances.

You can then simply iterate the batches and retrieve your results as shown below

var apiKey = 'qgAq_KfDu0KYzlNG-qaTuw';
var factory = Soapi.RouteFactory("api.meta.stackoverflow.com", apiKey);
factory.Users({ pagesize: 100 }).getResponse(function(data)
{
    // 240 is a safe value for max path length otherwise you risk 400 (Bad Request) errors
    // See http://stackapps.com/questions/889/dev-tip-how-to-batch-up-multiple-id-requests-to-maximize-economy-and-still-avoid/1021#1021

    var idList = new Soapi.Utilities
        .VectorizedIdList(240 - Soapi.Routes.UsersByIdReputationRouteMap.routeFormat.length);
    for (var i = 0; i < data.items.length; i++)
    {
        idList.add(data.items[i].user_id);
    }

    // so now we have a VectorizedIdList loaded with 100 user id values
    var batches = idList.getBatches();

    // batches:            
    //    [0]: [1, 811, 22656, 22164, 23354, 658, 9314, 18393, 98786, 130154, 3043, 2915, 115866, 1288, 12950, 132636, 59303, 1782, 13295, 22459, 136558, 17174, 52443, 16587, 18511, 14048, 130127, 59808, 27414, 132480, 23921, 2598, 13531, 4660, 52738, 1069, 2509]
    //    [1]: [131779, 95589, 893, 33708, 55747, 138112, 146126, 130914, 745, 131932, 139541, 130090, 101371, 140478, 2961, 141911, 132632, 75852, 135200, 1337, 54262, 8435, 76337, 137798, 69307, 3, 56338, 138822, 89334, 4, 91, 133733, 3724, 572, 102937, 47529, 137184, 5190]
    //    [2]: [130109, 74022, 84237, 419, 131713, 42471, 132223, 9345, 44765, 132073, 6309, 14148, 56555, 6258, 16417, 20654, 44330, 16487, 68727, 2, 146719, 133301, 140171, 63550, 130494]
    //    length: 3

    for (var i = 0; i < batches.length; i++)
    {
        // each batch will be a list of user id that will contain the
        // maximum number of items and still remain under the 240 length limit

        factory
        .UsersById(batches[i], { pagesize: batches[i].length })
        .getResponse(function(data)
        {
            // do something interesting with your batch of users here
            alert(data.items.length);
        }, function(error)
        {
            alert(error.message);
        });
    }


});