Skip to content

Commit

Permalink
Add support for pagination from the REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
mewejo committed Jul 15, 2020
1 parent 911361a commit 489aac0
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 13 deletions.
16 changes: 16 additions & 0 deletions src/API/RestfulKatapultApiV1/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Krystal\Katapult\API\RestfulKatapultApiV1;

class Helper
{
public static function addQueryToUrl($url, $arguments = null)
{
if($arguments && isset($arguments['query']))
{
$url .= '?' . http_build_query($arguments['query']);
}

return $url;
}
}
21 changes: 18 additions & 3 deletions src/API/RestfulKatapultApiV1/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,29 @@ public function createApiUrl($resourceId = null, $contextualArguments = null)
*/
public function all()
{
$res = $this->api->get($this->createApiUrl());
$lastFetchedPage = null;
$nextPage = 1;
$totalPages = null;

$resources = [];
$resourceClass = $this->mappedResourceClass; // Due to lacking Uniform Variable Syntax in PHP < 7

foreach(\GuzzleHttp\json_decode($res->getBody())->{$this->resourceNamePlural} as $resourceSpec)
while($lastFetchedPage === null || $lastFetchedPage < $totalPages)
{
$resources[] = $resourceClass::instantiateFromSpec($resourceSpec, $this);
$res = $this->api->get($this->createApiUrl(null, ['query' => ['page' => $nextPage]]));
$responseBody = \GuzzleHttp\json_decode($res->getBody());

foreach($responseBody->{$this->resourceNamePlural} as $resourceSpec)
{
$resources[] = $resourceClass::instantiateFromSpec($resourceSpec, $this);
}

// If this route doesn't support pagination, break out
if(!isset($responseBody->pagination)) break;

$lastFetchedPage = $nextPage;
$totalPages = $responseBody->pagination->total_pages;
$nextPage++;
}

return $resources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ public static function getUrl($resourceId = null, $arguments = null)
{
if($resourceId)
{
return "dns/zones/{$resourceId}";
$url = "dns/zones/{$resourceId}";
}
else
{
if(!is_array($arguments) || count($arguments) < 1) throw new \Exception('No arguments supplied to getUrl method');
return 'organizations/' . Helper::pluckObject($arguments, \Krystal\Katapult\Resources\Organization::class)->id . '/dns/zones';
$url = 'organizations/' . Helper::pluckObject($arguments, \Krystal\Katapult\Resources\Organization::class)->id . '/dns/zones';
}

return \Krystal\Katapult\API\RestfulKatapultApiV1\Helper::addQueryToUrl($url, $arguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public static function getUrl($resourceId = null, $arguments = null)
else
{
if(!is_array($arguments) || count($arguments) < 1) throw new \Exception('No arguments supplied to getUrl method');
return 'organizations/' . Helper::pluckObject($arguments, Organization::class)->id . '/managed';
$url = 'organizations/' . Helper::pluckObject($arguments, Organization::class)->id . '/managed';
}

return \Krystal\Katapult\API\RestfulKatapultApiV1\Helper::addQueryToUrl($url, $arguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ public static function getUrl($resourceId = null, $arguments = null)
break;
}
}

return $url;
}
else
{
if(!is_array($arguments) || count($arguments) < 1) throw new \Exception('No arguments supplied to getUrl method');
return 'organizations/' . Helper::pluckObject($arguments, \Krystal\Katapult\Resources\Organization::class)->id . '/virtual_machines';
$url = 'organizations/' . Helper::pluckObject($arguments, \Krystal\Katapult\Resources\Organization::class)->id . '/virtual_machines';
}

return \Krystal\Katapult\API\RestfulKatapultApiV1\Helper::addQueryToUrl($url, $arguments);
}

public static function callApiAction(ResourceController $resourceController, $action, $arguments)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public static function getUrl($resourceId = null, $arguments = null)
else
{
if(!is_array($arguments) || count($arguments) < 1) throw new \Exception('No arguments supplied to getUrl method');
return 'virtual_machines/' . Helper::pluckObject($arguments, VirtualMachine::class)->id . '/console_sessions';
$url = 'virtual_machines/' . Helper::pluckObject($arguments, VirtualMachine::class)->id . '/console_sessions';
}

return \Krystal\Katapult\API\RestfulKatapultApiV1\Helper::addQueryToUrl($url, $arguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ public static function getUrl($resourceId = null, $arguments = null)
{
if($resourceId)
{
return "virtual_machines/builds/{$resourceId}";
$url = "virtual_machines/builds/{$resourceId}";
}
else
{
throw new \Exception('VirtualMachineBuild does not support fetching a multiple resources');
}

return \Krystal\Katapult\API\RestfulKatapultApiV1\Helper::addQueryToUrl($url, $arguments);
}
}
6 changes: 4 additions & 2 deletions src/API/RestfulKatapultApiV1/Traits/HasStandardUrls.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Krystal\Katapult\API\RestfulKatapultApiV1\Traits;

use Krystal\Katapult\API\RestfulKatapultApiV1\Helper;

trait HasStandardUrls
{
public static function getUrl($resourceId = null, $arguments = null)
{
if(!$resourceId) return self::getResourceName(true);
return self::getResourceName(true) . '/' . $resourceId;
$url = $resourceId ? self::getResourceName(true) . '/' . $resourceId : self::getResourceName(true);
return Helper::addQueryToUrl($url, $arguments);
}
}

0 comments on commit 489aac0

Please sign in to comment.