Download all ad units.

  Args:
    inventory_service: An instance of the InventoryService.

  Returns:
    A list containing all ad units.

def get_all_ad_units(inventory_service):
  """Download all ad units.

  Args:
    inventory_service: An instance of the InventoryService.

  Returns:
    A list containing all ad units.
  """
  # Create a statement to get all ad units.
  statement = (ad_manager.StatementBuilder(version='v201811')
               .OrderBy('id', ascending=True))

  # Pull down all ad units into a list
  keep_iterating = True
  total_results = 0
  found_ad_units = []
  while keep_iterating:
    page = inventory_service.getAdUnitsByStatement(statement.ToStatement())
    if 'results' in page and len(page['results']):
      total_results = page['totalResultSetSize']
      found_ad_units.extend(page['results'])

    statement.offset += statement.limit
    keep_iterating = statement.offset < total_results

  return found_ad_units