Skip to content

Commit

Permalink
Merge pull request #316 from EasyPost/fix_pagination
Browse files Browse the repository at this point in the history
fix: pagination errors with user-supplied params
  • Loading branch information
Justintime50 authored Nov 28, 2023
2 parents 5bcbc4b + bc5a28a commit 5e4c554
Show file tree
Hide file tree
Showing 26 changed files with 552 additions and 478 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Next Major Release

- Removed `withCarbonOffset` parameter from `create`, `buy`, and `regenerateRates` functions of the Shipment service as EasyPost now offers Carbon Neutral shipments by default for free
- Fixes a bug where the original filtering criteria of `all` calls wasn't passed along to `getNextPage` calls. Now, these are persisted via a `_params` key on response objects locally

## v6.9.1 (2023-11-20)

Expand Down
10 changes: 7 additions & 3 deletions lib/EasyPost/EasyPostClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,13 @@ public function __get($serviceName)
if (array_key_exists($serviceName, $serviceClassMap)) {
return new $serviceClassMap[$serviceName]($this);
} else {
throw new EasyPostException(
sprintf(Constants::UNDEFINED_PROPERTY_ERROR, 'EasyPostClient', $serviceName)
);
// TODO: checking for `_parent` is a hack and should be fixed when we revisit the
// (de)serialization of objects in this lib.
if ($serviceName != '_parent') {
throw new EasyPostException(
sprintf(Constants::UNDEFINED_PROPERTY_ERROR, 'EasyPostClient', $serviceName)
);
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion lib/EasyPost/EasyPostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ public static function constructFrom($client, $values, $class)
public function convertEach($client, $values)
{
foreach ($values as $k => $v) {
$this->_values[$k] = InternalUtil::convertToEasyPostObject($client, $v);
// We don't want `_params` to become the default `EasyPostObject` since it needs to remain a normal array
if ($k == '_params') {
$this->_values[$k] = $v;
} else {
$this->_values[$k] = InternalUtil::convertToEasyPostObject($client, $v);
}
}
}

Expand Down
21 changes: 13 additions & 8 deletions lib/EasyPost/Service/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
namespace EasyPost\Service;

use EasyPost\Constant\Constants;
use EasyPost\Exception\General\EndOfPaginationException;
use EasyPost\Exception\General\InvalidObjectException;
use EasyPost\Exception\General\InvalidParameterException;
use EasyPost\Exception\General\EndOfPaginationException;
use EasyPost\Http\Requestor;
use EasyPost\Util\InternalUtil;

use function PHPUnit\Framework\assertTrue;

class BaseService
{
protected $client;
Expand Down Expand Up @@ -137,6 +135,9 @@ protected function allResources($class, $params = null, $beta = false)
self::validate($params);
$url = self::classUrl($class);
$response = Requestor::request($this->client, 'get', $url, $params, $beta);
if (isset($params)) {
$response['_params'] = $params;
}

return InternalUtil::convertToEasyPostObject($this->client, $response);
}
Expand All @@ -148,13 +149,13 @@ protected function allResources($class, $params = null, $beta = false)
* @param string $class
* @param mixed $collection
* @param int $pageSize
* @param mixed $optionalParams
* @return mixed
*/
protected function getNextPageResources($class, $collection, $pageSize = null, $optionalParams = null)
protected function getNextPageResources($class, $collection, $pageSize = null)
{
$objectName = substr(self::classUrl($class), 1);
$collectionArray = $collection[$objectName];
$userParams = $collection['_params'] ?? null;

if (empty($collectionArray) || !$collection['has_more']) {
throw new EndOfPaginationException();
Expand All @@ -165,13 +166,17 @@ protected function getNextPageResources($class, $collection, $pageSize = null, $
'before_id' => $collectionArray[count($collectionArray) - 1]['id']
];

if (isset($optionalParams)) {
$params = array_merge($params, $optionalParams);
if (isset($userParams)) {
$params = array_merge($params, $userParams);
}

$response = $this->allResources($class, $params);

if (empty($response[$objectName]) || !$response['has_more']) {
if (isset($userParams)) {
$response['_params'] = $userParams;
}

if (empty($response[$objectName])) {
throw new EndOfPaginationException();
}

Expand Down
16 changes: 13 additions & 3 deletions lib/EasyPost/Service/ReportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace EasyPost\Service;

use EasyPost\Constant\Constants;
use EasyPost\Exception\General\MissingParameterException;
use EasyPost\Exception\General\EndOfPaginationException;
use EasyPost\Exception\General\MissingParameterException;
use EasyPost\Http\Requestor;
use EasyPost\Util\InternalUtil;

Expand Down Expand Up @@ -57,6 +57,7 @@ public function all($params = null)
public function getNextPage($reports, $pageSize = null)
{
$reportArray = $reports['reports'];
$userParams = $reports['_params'] ?? null;

if (empty($reportArray) || !$reports['has_more']) {
throw new EndOfPaginationException();
Expand All @@ -66,10 +67,19 @@ public function getNextPage($reports, $pageSize = null)
'page_size' => $pageSize,
'before_id' => $reportArray[count($reportArray) - 1]['id']
];
$url = self::reportUrl($reports->type);

if (isset($userParams)) {
$params = array_merge($params, $userParams);
}

$url = self::reportUrl($reportArray[0]->object);
$response = Requestor::request($this->client, 'get', $url, $params);

if (empty($response['reports']) || !$response['has_more']) {
if (isset($userParams)) {
$response['_params'] = $userParams;
}

if (empty($response['reports'])) {
throw new EndOfPaginationException();
}

Expand Down
18 changes: 2 additions & 16 deletions lib/EasyPost/Service/ShipmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ public function retrieve($id)
*/
public function all($params = null)
{
self::validate($params);
$response = Requestor::request($this->client, 'get', '/shipments', $params);
$response['purchased'] = $params['purchased'] ?? null;
$response['include_children'] = $params['include_children'] ?? null;

return InternalUtil::convertToEasyPostObject($this->client, $response);
return self::allResources(self::serviceModelClassName(self::class), $params);
}

/**
Expand All @@ -47,16 +42,7 @@ public function all($params = null)
*/
public function getNextPage($shipments, $pageSize = null)
{
$params = [];

if (isset($shipments->purchased)) {
$params['purchased'] = $shipments->purchased;
}

if (isset($shipments->include_children)) {
$params['include_children'] = $shipments->include_children;
}
return $this->getNextPageResources(self::serviceModelClassName(self::class), $shipments, $pageSize, $params);
return $this->getNextPageResources(self::serviceModelClassName(self::class), $shipments, $pageSize);
}

/**
Expand Down
18 changes: 2 additions & 16 deletions lib/EasyPost/Service/TrackerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ public function retrieve($id)
*/
public function all($params = null)
{
self::validate($params);
$response = Requestor::request($this->client, 'get', '/trackers', $params);
$response['tracking_code'] = $params['tracking_code'] ?? null;
$response['carrier'] = $params['carrier'] ?? null;

return InternalUtil::convertToEasyPostObject($this->client, $response);
return self::allResources(self::serviceModelClassName(self::class), $params);
}

/**
Expand All @@ -46,16 +41,7 @@ public function all($params = null)
*/
public function getNextPage($trackers, $pageSize = null)
{
$params = [];

if (isset($trackers->tracking_code)) {
$params['tracking_code'] = $trackers->tracking_code;
}

if (isset($trackers->carrier)) {
$params['carrier'] = $trackers->carrier;
}
return $this->getNextPageResources(self::serviceModelClassName(self::class), $trackers, $pageSize, $params);
return $this->getNextPageResources(self::serviceModelClassName(self::class), $trackers, $pageSize);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions test/EasyPost/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ public function testGetNextPage()
$secondIdOfSecondPage = $nextPage['addresses'][0]->id;

$this->assertNotEquals($firstIdOfFirstPage, $secondIdOfSecondPage);
} catch (Exception $error) {
if (!($error instanceof EndOfPaginationException)) {
throw new Exception('Test failed intentionally');
}
} catch (EndOfPaginationException $error) {
// There's no second page, that's not a failure
$this->assertTrue(true);
} catch (Exception $error) {
throw $error;
}
}

Expand Down
10 changes: 5 additions & 5 deletions test/EasyPost/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use EasyPost\Event;
use EasyPost\Exception\General\EasyPostException;
use EasyPost\Exception\General\EndOfPaginationException;
use Exception;
use EasyPost\Payload;
use EasyPost\Util\Util;
use Exception;

class EventTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -66,11 +66,11 @@ public function testGetNextPage()
$secondIdOfSecondPage = $nextPage['events'][0]->id;

$this->assertNotEquals($firstIdOfFirstPage, $secondIdOfSecondPage);
} catch (Exception $error) {
if (!($error instanceof EndOfPaginationException)) {
throw new Exception('Test failed intentionally');
}
} catch (EndOfPaginationException $error) {
// There's no second page, that's not a failure
$this->assertTrue(true);
} catch (Exception $error) {
throw $error;
}
}

Expand Down
9 changes: 5 additions & 4 deletions test/EasyPost/InsuranceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ public function testGetNextPage()
$secondIdOfSecondPage = $nextPage['insurances'][0]->id;

$this->assertNotEquals($firstIdOfFirstPage, $secondIdOfSecondPage);
} catch (Exception $error) {
if (!($error instanceof EndOfPaginationException)) {
throw new Exception('Test failed intentionally');
}
$this->assertNotNull($nextPage['_params']);
} catch (EndOfPaginationException $error) {
// There's no second page, that's not a failure
$this->assertTrue(true);
} catch (Exception $error) {
throw $error;
}
}
}
12 changes: 6 additions & 6 deletions test/EasyPost/PickupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace EasyPost\Test;

use EasyPost\EasyPostClient;
use EasyPost\Exception\General\FilteringException;
use EasyPost\Exception\General\EndOfPaginationException;
use Exception;
use EasyPost\Exception\General\FilteringException;
use EasyPost\Pickup;
use Exception;

class PickupTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -102,11 +102,11 @@ public function testGetNextPage()
$secondIdOfSecondPage = $nextPage['pickups'][0]->id;

$this->assertNotEquals($firstIdOfFirstPage, $secondIdOfSecondPage);
} catch (Exception $error) {
if (!($error instanceof EndOfPaginationException)) {
throw new Exception('Test failed intentionally');
}
} catch (EndOfPaginationException $error) {
// There's no second page, that's not a failure
$this->assertTrue(true);
} catch (Exception $error) {
throw $error;
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/EasyPost/ReferralCustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ public function testGetNextPage()
$secondIdOfSecondPage = $nextPage['referral_customers'][0]->id;

$this->assertNotEquals($firstIdOfFirstPage, $secondIdOfSecondPage);
} catch (Exception $error) {
if (!($error instanceof EndOfPaginationException)) {
throw new Exception('Test failed intentionally');
}
} catch (EndOfPaginationException $error) {
// There's no second page, that's not a failure
$this->assertTrue(true);
} catch (Exception $error) {
throw $error;
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/EasyPost/RefundTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ public function testGetNextPage()
$secondIdOfSecondPage = $nextPage['refunds'][0]->id;

$this->assertNotEquals($firstIdOfFirstPage, $secondIdOfSecondPage);
} catch (Exception $error) {
if (!($error instanceof EndOfPaginationException)) {
throw new Exception('Test failed intentionally');
}
} catch (EndOfPaginationException $error) {
// There's no second page, that's not a failure
$this->assertTrue(true);
} catch (Exception $error) {
throw $error;
}
}

Expand Down
12 changes: 6 additions & 6 deletions test/EasyPost/ReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace EasyPost\Test;

use EasyPost\EasyPostClient;
use EasyPost\Exception\General\MissingParameterException;
use EasyPost\Exception\General\EndOfPaginationException;
use Exception;
use EasyPost\Exception\General\MissingParameterException;
use EasyPost\Report;
use Exception;

class ReportTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -143,11 +143,11 @@ public function testGetNextPage()
$secondIdOfSecondPage = $nextPage['reports'][0]->id;

$this->assertNotEquals($firstIdOfFirstPage, $secondIdOfSecondPage);
} catch (Exception $error) {
if (!($error instanceof EndOfPaginationException)) {
throw new Exception('Test failed intentionally');
}
} catch (EndOfPaginationException $error) {
// There's no second page, that's not a failure
$this->assertTrue(true);
} catch (Exception $error) {
throw $error;
}
}

Expand Down
10 changes: 5 additions & 5 deletions test/EasyPost/ScanFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace EasyPost\Test;

use EasyPost\EasyPostClient;
use EasyPost\ScanForm;
use EasyPost\Exception\General\EndOfPaginationException;
use EasyPost\ScanForm;
use Exception;

class ScanFormTest extends \PHPUnit\Framework\TestCase
Expand Down Expand Up @@ -99,11 +99,11 @@ public function testGetNextPage()
$secondIdOfSecondPage = $nextPage['scan_forms'][0]->id;

$this->assertNotEquals($firstIdOfFirstPage, $secondIdOfSecondPage);
} catch (Exception $error) {
if (!($error instanceof EndOfPaginationException)) {
throw new Exception('Test failed intentionally');
}
} catch (EndOfPaginationException $error) {
// There's no second page, that's not a failure
$this->assertTrue(true);
} catch (Exception $error) {
throw $error;
}
}
}
9 changes: 5 additions & 4 deletions test/EasyPost/ShipmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ public function testGetNextPage()
$secondIdOfSecondPage = $nextPage['shipments'][0]->id;

$this->assertNotEquals($firstIdOfFirstPage, $secondIdOfSecondPage);
} catch (Exception $error) {
if (!($error instanceof EndOfPaginationException)) {
throw new Exception('Test failed intentionally');
}
$this->assertNotNull($nextPage['_params']);
} catch (EndOfPaginationException $error) {
// There's no second page, that's not a failure
$this->assertTrue(true);
} catch (Exception $error) {
throw $error;
}
}

Expand Down
Loading

0 comments on commit 5e4c554

Please sign in to comment.