3

I have just discovered you can get pagination results through the api by passing in the page parameter like so:

$projects = $client->get('projects/147/time-records?page=3')->getJson();

Is there a way of knowing how many time records a project has so I know how many times I need to paginate?

Alternatively, how would I go about retrieving several pages worth of data - i'm struggling with the code!

mikestreety
  • 833
  • 6
  • 28

3 Answers3

2

Sure. All paginated results will include following headers:

  • X-Angie-PaginationCurrentPage - indicates current page
  • X-Angie-PaginationItemsPerPage - indicates number of items per page
  • X-Angie-PaginationTotalItems - indicates number of items in the entire data set.

When you get header values, simple:

$total_pages = ceil($total_items_header_value / $items_per_page_header_value);

will give you number of pages that are in the collection.

Alternative: You can iterate through pages (by starting with page GET parameter set to 1, and incrementing it) until you get an empty result (page with no records). Page that returns no records is the last page.

Ilija
  • 4,105
  • 4
  • 32
  • 46
  • Thanks for the swift reply [Ilija](http://stackoverflow.com/users/338473/ilija) - how do I get the header info using the feather SDK? – mikestreety Oct 13 '16 at 13:11
  • SDK does not expose headers :'( Please file an issue on GitHub (or even PR) so this can be covered in the future SDK updates. You can still loop until you get an empty result. – Ilija Oct 13 '16 at 17:57
2

I have created an issue on Github - will await a response.

For now, I do the following:

// Get all the projects

// Set the page number
$page = 1;

// Create an empty array
$project_records = array();

// Get the first page of results
$project_records_results = $client->get('projects?page=' . $page)->getJson();

// Merge the results with base array
$project_records = array_merge($project_records, $project_records_results);

// Get the next page of results, 
// if it returns something merge with the base array and continue
while ($project_records_results = $client->get('projects?page=' . ++$page)->getJson()) {
    $project_records = array_merge($project_records, $project_records_results);
}
mikestreety
  • 833
  • 6
  • 28
0

Please note, that the headers are now all lowercase (v1)! So the answer above should be corrected.

To get them call:

$headers = $client->get($path)->getHeaders();

Working code example from /api/v1/:

$paginationCurrentPage = isset($headers['x-angie-paginationcurrentpage'][0]) ? $headers['x-angie-paginationcurrentpage'][0] : NULL;
$paginationItemsPerPage = isset($headers['x-angie-paginationitemsperpage'][0]) ? $headers['x-angie-paginationitemsperpage'][0] : NULL;
$paginationTotalItems = isset($headers['x-angie-paginationtotalitems'][0]) ? $headers['x-angie-paginationtotalitems'][0] : NULL;