Paging Example for All Supported Languages

Get an in-depth look at different methods of paging for developers using Intrinio’s Javascript, Python, Ruby, Java, and C# SDKs.

Our paging documentation gives a brief overview of paged responses as well as URL use case examples. This is a more in-depth look at paging for developers using our Javascript, Python, Ruby, Java, and C# SDKs.

Paging Overview

Each language has an appropriate api_response._next_page method which is used to redefine the next_page string. The examples below show some different methods that the retrieval and reuse of the next page string can be performed in our supported languages.

In Python, Java, and C# the next_page is a string which can be changed directly, so the argument can be changed directly from the returned value.

Python Example


...
max_number_pages_returned= 1000 # Integer | most pages that can be retrieved
stockPriceList = []

while max_number_pages_returned > 0:

  try:
    api_response = security_api.get_security_stock_prices(identifier, start_date=start_date, end_date=end_date, frequency=frequency, page_size=page_size, next_page=next_page)
    next_page = api_response._next_page
    for i in range(len(api_response._stock_prices)):
        stockPriceList.append(api_response._stock_prices[i])

  except ApiException as e:
    print("Exception when calling SecurityApi->get_security_stock_prices: %s\r\n" % e)
    break

  if next_page == None:
    break
  max_number_pages_returned -= 1

for j in range(len(stockPriceList)):
    pprint(stockPriceList[j])

Java Example



...

    Integer maxNumberPagesReturned = 1000;// Integer | The most
    ArrayList<StockPriceSummary> stockPriceList = new ArrayList<>();

    while (maxNumberPagesReturned != 0) {
      try {
        ApiResponseSecurityStockPrices result = securityApi.getSecurityStockPrices(identifier, startDate, endDate, frequency, pageSize, nextPage);
        nextPage = result.getNextPage();
        for(int i = 0; i < result.getStockPrices().size(); i++){
          stockPriceList.add(result.getStockPrices().get(i));
        }
        if (nextPage == null) {
          break;
        }
      } catch (ApiException e) {
        System.err.println("Exception when calling SecurityApi#getSecurityStockPrices");
        e.printStackTrace();
        break;
      }
      maxNumberPagesReturned--;
    }

    for(int j = 0; j < stockPriceList.size(); j++){
      System.out.println(stockPriceList.get(j));
    }

  }
}

C# Example

...

      var maxNumberOfPagesReturned = 1000;
      ApiResponseSecurityStockPrices result = null;
      ArrayList responses = new ArrayList();

      while (maxNumberOfPagesReturned > 0)
      {
        try
        {
          result = securityApi.GetSecurityStockPrices(identifier, startDate, endDate, frequency, pageSize, nextPage);
          nextPage = result.NextPage;          
          for (int i = 0; i < result.StockPrices.Count; i++)
          {
            Console.WriteLine(result.StockPrices[i]);
            responses.Add(result.StockPrices[i]);
          }
        }

        catch (Exception e)
        {
          Console.WriteLine("Exception when calling SecurityApi.GetSecurityStockPrices: " + e.Message);
          break;
        }

        if (nextPage == null)
        {
          break;
        }
        maxNumberOfPagesReturned--;
      }
     
      for (int j = 0; j < responses.Count; j++)
      {
        Console.WriteLine(responses[j]);
      }
    }
  }
}

Ruby uses hashes to store the arguments of the API call, thus when the next page key is returned, the string must be remerged into the hash. 

Ruby Example


...
max_number_pages_returned = 1000
next_page_key = nil
stock_Price_List = Array.new
opts = {
  start_date: Date.parse("2018-01-01"), # Date | Return prices on or after the date
  end_date: Date.parse("2019-01-02"), # Date | Return prices on or before the date
  frequency: "daily", # String | Return stock prices in the given frequency
  page_size: 100, # Integer | The number of results to return
  next_page: nil # String | Gets the next page of data from a previous API call
}

while max_number_pages_returned > 0
  begin
    results = security_api.get_security_stock_prices(identifier, opts)
    next_page_key = results.next_page
    opts_update = {next_page: next_page_key}
    opts.merge!(opts_update)

    for result in results.stock_prices
      stock_Price_List.push(result)
    end

  rescue Intrinio::ApiError => e
    puts "Exception when calling SecurityApi->get_security_stock_prices: #{e}"
    break
  end

  if next_page_key == nil
    break
  end
  max_number_pages_returned -= 1
end

for stock_Price in stock_Price_List
  pp stock_Price
end

Our Javascript API returns data using the promise format, meaning that the eventual response will be asynchronous. Wrapping the API call in an async function allows us to await the response and provides synchronous executions. Since Javascript redefines its arguments every time the asynchronous function is called, the returned next string value is passed back to the function on recall.

Javascript Example

var intrinioSDK = require('intrinio-sdk');

async function getData() {
  responses = [];
  stockPriceList = [];
  var nextPage = null;
  var moreData = true;
  var maxNumberOfPagesReturned = 1000;

  while(maxNumberOfPagesReturned > 0){
    dataPage = await callApi(nextPage);
    responses.push(dataPage);
    nextPage = dataPage.next_page;

    if (!nextPage || nextPage == '') {
      break
    };
    maxNumberOfPagesReturned--;
  };

  for (const stockPrices of responses) {
    for (const stockPrice of stockPrices.stock_prices) {
      stockPriceList.push(stockPrice)
    };
  };

  for (const pricePrint of stockPriceList){
    console.log(pricePrint);
  }

};

async function callApi(nextPage = null) {
  intrinioSDK.ApiClient.instance.authentications['ApiKeyAuth'].apiKey = "YOUR_API_KEY";
  var securityAPI = new intrinioSDK.SecurityApi();
  var identifier = "AAPL"; // String | A Security identifier (Ticker, FIGI, ISIN, CUSIP, Intrinio ID)

  var opts = {
    'startDate': new Date("2018-01-01"), // Date | Return prices on or after the date
    'endDate': new Date("2019-01-01"), // Date | Return prices on or before the date
    'frequency': "daily", // String | Return stock prices in the given frequency
    'pageSize': 100, // Number | The number of results to return
    'nextPage': nextPage // String | Gets the next page of data from a previous API call
  };

  data = await securityAPI.getSecurityStockPrices(identifier, opts);
  return data;
};

getData();

In Summary

In summary, the paging system is an efficient string based system, as opposed to the integer system normally associated with paging. While this system adds an extra step to the process of returning multiple pages, it improves performance and reliability compared to the integer system. All that is required is that the next page string returned from an API be placed back into the API call arguments and the subsequent page will be returned.