Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0cfb598
chore: add support for token based pagination
tiwarishubham635 Nov 11, 2025
7653480
chore: add unit test
tiwarishubham635 Nov 11, 2025
7ef42e3
chore: fix unit test
tiwarishubham635 Nov 13, 2025
7045afc
Merge branch 'main' into add_token_pagination
tiwarishubham635 Nov 13, 2025
47cb948
chore: add unit tests
tiwarishubham635 Nov 13, 2025
ffa3a06
Merge branch 'main' into add_token_pagination
tiwarishubham635 Nov 21, 2025
1d2e5b7
Merge branch 'main' into add_token_pagination
tiwarishubham635 Nov 24, 2025
7eaa2cf
docs: add documentation for KeyErrorException
tiwarishubham635 Nov 26, 2025
6197096
core: switch full_url to camelCase
tiwarishubham635 Nov 26, 2025
7945dec
chore: use urlencode before adding pageToken
tiwarishubham635 Nov 26, 2025
d489cf6
chore: remove addQueryParam()
tiwarishubham635 Nov 26, 2025
1963019
chore: return null when null token is passed in getNextPageUrl() and …
tiwarishubham635 Nov 26, 2025
96ec664
docs: add TokenPaginationPage.php documentation
tiwarishubham635 Nov 26, 2025
a152794
Update src/Twilio/TokenPaginationPage.php
tiwarishubham635 Nov 26, 2025
bae09a4
chore: use camelCase for httpClient
tiwarishubham635 Nov 26, 2025
030a30f
Merge remote-tracking branch 'origin/add_token_pagination' into add_t…
tiwarishubham635 Nov 26, 2025
1a5d0c1
Update src/Twilio/TokenPaginationPage.php
tiwarishubham635 Nov 26, 2025
4c60bca
Update src/Twilio/TokenPaginationPage.php
tiwarishubham635 Nov 26, 2025
169af69
docs: add docblocks for the functions
tiwarishubham635 Nov 26, 2025
c5c5953
Update tests/Twilio/Unit/TokenPaginationPageTest.php
tiwarishubham635 Nov 26, 2025
8b8d4f3
Update src/Twilio/TokenPaginationPage.php
tiwarishubham635 Nov 26, 2025
feafb45
Update src/Twilio/TokenPaginationPage.php
tiwarishubham635 Nov 26, 2025
073ac2f
Update src/Twilio/TokenPaginationPage.php
tiwarishubham635 Nov 26, 2025
e2bc596
chore: rename query string getter
tiwarishubham635 Nov 26, 2025
506f642
Update src/Twilio/TokenPaginationPage.php
tiwarishubham635 Nov 26, 2025
1a6d763
Update src/Twilio/TokenPaginationPage.php
tiwarishubham635 Nov 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Twilio/Exceptions/KeyErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php


namespace Twilio\Exceptions;


/**
* Exception thrown when a required key is missing from a response or metadata.
*
* This exception is typically thrown when the expected 'key' field is not present
* in the response data or metadata, which may indicate a malformed or incomplete response.
*
* Example scenarios:
* - When parsing API responses and the 'key' field is missing.
* - When validating metadata and a required key is not found.
*/
class KeyErrorException extends TwilioException {

}
139 changes: 139 additions & 0 deletions src/Twilio/TokenPaginationPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php


namespace Twilio;


use Twilio\Exceptions\KeyErrorException;
use Twilio\Http\Response;

/**
* TokenPaginationPage is an abstract base class for handling paginated API responses
* that use token-based pagination rather than traditional page numbers. These are part of
* the new Twilio API Standards V1.
*
* Unlike the base {@see Page} class, which typically uses page numbers and URLs for navigation,
* TokenPaginationPage manages pagination using tokens (e.g., nextToken, previousToken) provided
* in the API response metadata. This allows for more flexible and scalable pagination, especially
* for APIs that do not support offset-based pagination.
*
* Example expected response format with token metadata:
* {
* "meta": {
* "key": "items",
* "pageSize": 50,
* "nextToken": "abc123",
* "previousToken": "xyz789"
* },
* "items": [
* { "id": 1, "name": "Item 1" },
* { "id": 2, "name": "Item 2" }
* // ...
* ]
* }
*
*/
abstract class TokenPaginationPage extends Page {

protected $key;
protected $pageSize;
protected $nextToken;
protected $previousToken;
protected $url;
protected $previousPageUrl;
protected $nextPageUrl;

/**
* TokenPaginationPage constructor.
*
* @param Version $version The API version object.
* @param Response $response The HTTP response object.
* @throws KeyErrorException If the 'key' metadata is missing.
*/
public function __construct(Version $version, Response $response) {
parent::__construct($version, $response);

$httpClient = $version->getDomain()->getClient()->getHttpClient();

$this->url = '';
if ($httpClient->lastRequest) {
$fullUrl = $httpClient->lastRequest[CURLOPT_URL];
// remove query parameters from url
$parts = explode('?', $fullUrl);
$this->url = $parts[0];
}

$this->key = $this->getMeta('key');
$this->pageSize = (int) $this->getMeta('pageSize');
$this->nextToken = $this->getMeta('nextToken');
$this->previousToken = $this->getMeta('previousToken');
}

/**
* Load the current page of records based on the 'key' metadata.
*
* @return array Array of records from the current page.
* @throws KeyErrorException If the 'key' metadata is missing.
*/
protected function loadPage(): array {
$this->key = $this->getMeta('key');
if ($this->key) {
return $this->payload[$this->key];
}

throw new KeyErrorException('key not found in the response');
}

/**
* Construct the query string for pagination URLs.
*
* @param string|null $pageToken The token for the desired page.
* @return string The constructed query string.
*/
protected function getQueryString(?string $pageToken): string {
$params = [];
if ($this->pageSize) {
$params['pageSize'] = $this->pageSize;
}
if ($pageToken !== null && $pageToken !== '') {
$params['pageToken'] = $pageToken;
}
$queryString = http_build_query($params);
return $queryString ? '?' . $queryString : '';
}

/**
* Get the URL for the previous page of results.
*
* @return string|null The URL for the previous page, or null if there is no previous page.
*/
public function getPreviousPageUrl(): ?string {
if (!$this->previousToken) {
return null;
}
if (!$this->previousPageUrl) {
$this->previousPageUrl = $this->url . $this->getQueryString($this->previousToken);
}
return $this->previousPageUrl;
}

/**
* Get the URL for the next page of results.
*
* @return string|null The URL for the next page, or null if there is no next page.
*/
public function getNextPageUrl(): ?string {
if (!$this->nextToken) {
return null;
}
if (!$this->nextPageUrl) {
$this->nextPageUrl = $this->url . $this->getQueryString($this->nextToken);
}
return $this->nextPageUrl;
}

public function __toString(): string {
return '[TokenPaginationPage]';
}

}
Loading
Loading