-1

I am Calling API to get a list of contacts(they might be in 100's or 1000's) and list only lists 100 at a time and its giving me this pagination option with an object at the end of the list called 'nextpage' and with URL to next 100 and so on..

so in my c# code and am getting first 100 and looping through them (to do something) and looking up for 'nextpage' object and getting the URL and re-calling the API etc.. looks like this next page chain goes on depending on how many ever contacts we have.

can you please let me know if there is a way for me to loop through same code and still be able to use new URL from 'nextpage' object and run the logic for every 100 i get ?

Bokbob
  • 89
  • 1
  • 9

1 Answers1

0

Pseudo-code, as we have no concrete examples to work with, but...

Most APIs with pagination will have a total count of items. You can set a max items per iteration and track it like that, or check for the null next_object, depending on how the API handles it.

    List<ApiObject> GetObjects() {

        const int ITERATION_COUNT = 100;
        int objectsCount = GetAPICount();

        var ApiObjects = new List<ApiObject>();

        for (int i = 0; i < objectsCount; i+= ITERATION_COUNT) {

            // get the next 100
            var apiObjects = callToAPI(i, ITERATION_COUNT); // pass the current offset, request the max per call
            ApiObjects.AddRange(apiObjects);

        }   // this loop will stop after you've reached objectsCount, so you should have all

        return ApiObjects;
    }

    // alternatively:

    List<ApiObject> GetObjects() {

        var nextObject = null;
        var ApiObjects = new List<ApiObject>();

        // get the first batch
        var apiObjects = callToAPI(null);
        ApiObjects.AddRange(apiObjects);
        nextObject = callResponse.nextObject;

        // and continue to loop until there's none left
        while (nextObject != null) {            
            var apiObjects = callToAPI(null);
            ApiObjects.AddRange(apiObjects);
            nextObject = callResponse.nextObject;   
        }

        return apiObjects;  
    }

That's the basic idea anyway, per the two usual web service approaches (with lots of detail left out, as this is not working code but only meant to demonstrate the general approach).

jleach
  • 7,410
  • 3
  • 33
  • 60
  • Glad to help. As a side note, it's usually a good practice to retrieve everything from the web service first (put it all into one collection), and *then* process the entire collection after you've retrieved them all. This keeps the "web service window" narrow and helps separate the data source from the actual business logic. (a notable exception here would be if you have a lot of data to get and want to start processing it in an async manner, in which case such approach changes a bit) – jleach Jul 29 '16 at 02:49
  • Yes about data am not sure how much we could expect. But all the calls are syncronous as another call is depending on the result of the first call and so on. – Bokbob Jul 29 '16 at 03:59