从REST API调用访问所有用户?

时间:2016-02-26 17:09:03

标签: javascript node.js api rest

API调用只允许我一次只能获得1000个用户。但是,我知道我有9800个用户。我可以在url中传入一个参数,从哪里开始和最大结果(限制为1000)。但是我需要把它们全部拿走而且不知道如何去做。

我正在使用Node。和蓝鸟和请求NPM模块。

这是我的代码

/********************** MODULES/DEPENDENCIES **********************/
var express = require('express');
var request = require('request');
var Promise = require('bluebird');
var _ = require("lodash");

/********************** INITIATE APP **********************/
var app = express();
console.log("Starting node server...");

/**
 * @param url - The url to GET
 * HTTPS GET Request Function
 * @return Promise - Promise containing the JSON response. 
 */

 /********************** URL GET FUNCTION **********************/
function get(url) {
    return new Promise(function(resolve, reject) {

        // var auth = "Basic " + new Buffer(username + ':' + password).toString("base64");

        var options = {
            url: url,
            headers: {
                // 'Authorization': auth,
                'Content-Type': 'application/json',
                'Accept': 'application/json'
            }
        };

        console.log("Calling GET: ", url);

        if ('development' == app.get('env')) {
            console.log("Rejecting node tls");
            process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
        }

        request(options, function(error, response, body) {
            if (error) {
                reject(error);
            } else {
                // console.log("This is body: ", body.length);
                resolve(body);
            }
        }); 
    });
};

/********************** HTTPS GET SRERVER DATA **********************/
function getServerData() {

    /********************** URL VARIABLES **********************/
    var username = 'username',
        password = 'password',
        role = 'Read-Only',
        url_host = 'https://link.com:1000';

    /********************** URL 1 **********************/
    var url1 = url_host + '/type/PropertySetClasses/SystemObject/Server/?username=' + username + '&password=' + password + '&role=' + role;
    // firstResult=9500&maxResults=1&

    console.log("Getting server data...", url1);

    /********************** GET REQUEST 1 **********************/
    return get(url1)
    .then(function(res) {

        console.log("Got response!");

        /********************** FETCH URI FROM RES NESTED OBJECT **********************/
        res = JSON.parse(res);
        res = res.PropertySetClassChildrenResponse.PropertySetClassChildren.PropertySetInstances.Elements;

        // console.log("This is res 1: ", res);

        var server_ids = _.map(res, function(server) {
            return server.uri;
        });

        console.log("Calling server urls", server_ids);

        /********************** RETURN URL WITH SERVER URI **********************/
        return Promise.map(server_ids, function (id) {
            var url2 = url_host + id + '?username=' + username + '&password=' + password + '&role=' + role;

            console.log("Calling URL", url2);

            /********************** RETURN SERVER PROPERTIES **********************/
            return get(url2)
            .then(function(res2) {
                res2 = JSON.parse(res2);
                var elements = res2.PropertySetInstanceResponse.PropertySetInstance.PropertyValues.Elements;

                console.log("Got second response", res2, elements);
                return elements;
            });
        })
        .then(function (allUrls) {
            console.log("Got all URLS", allUrls);
            return allUrls;
        });
    })
    .catch(function(err) {
        console.error(err);
        throw err;
    });

};

/********************** PORT HANDLER **********************/
app.listen(8080, function() {
    console.log("Server listening and booted on: " + 8080);

    /********************** ROUTER  **********************/
    app.get("/serverInfo", function (req, res) {
        console.log("Calling server info");

        /********************** RETURN PROMISE **********************/
        return getServerData()
        .then(function(userData) {
            var userData = JSON.stringify(userData, null, "\t");
            console.log("This is USERDATA Data: ", userData);

            res.send(userData);
        })
        .catch(function(err) {
            console.error(err);

            res.send({
                __error: err,
                message: err.message
            });
        });
    });
});

0 个答案:

没有答案