DigitalOceanPHP / Client

DigitalOcean API v2 client for PHP
MIT License
710 stars 205 forks source link

Updating Api/Droplet with new getTotal() function #235

Closed cam-vanorman closed 4 years ago

cam-vanorman commented 4 years ago

Purpose of this new function is to provide a starting point for paginating Droplet requests.

This PR implements a new function for returning the total amount of Droplets. This is an initial enhancement based on #29 . Implementation is basic and is as follows:

  1. Make API request for /droplets with $perPage = 1 and $page = 1 (we only need 1 result to get the meta object)
  2. Get meta->total for Droplet Total
  3. Return $total

Can be used like

        // Total Droplets from API
        $total    = DigitalOcean::droplet()->getTotal();

        // How many times we should iterate $page
        $pages    = ceil($total / $perPage);

        for ($i = 1; $i <= $pages; $i++) {
            $request = DigitalOcean::droplet()->getAll($perPage, $i);
            foreach ($request as $item) {
                array_push($droplets, $item);
            }
        }
glennjacobs commented 4 years ago

Just a note to say we'll be reviewing this pull request very soon, now that the package has new maintainers.

GrahamCampbell commented 4 years ago

Thanks.