-
Notifications
You must be signed in to change notification settings - Fork 583
feat: Add support for token based pagination strategy #887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+512
−0
Merged
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 7653480
chore: add unit test
tiwarishubham635 7ef42e3
chore: fix unit test
tiwarishubham635 7045afc
Merge branch 'main' into add_token_pagination
tiwarishubham635 47cb948
chore: add unit tests
tiwarishubham635 ffa3a06
Merge branch 'main' into add_token_pagination
tiwarishubham635 1d2e5b7
Merge branch 'main' into add_token_pagination
tiwarishubham635 7eaa2cf
docs: add documentation for KeyErrorException
tiwarishubham635 6197096
core: switch full_url to camelCase
tiwarishubham635 7945dec
chore: use urlencode before adding pageToken
tiwarishubham635 d489cf6
chore: remove addQueryParam()
tiwarishubham635 1963019
chore: return null when null token is passed in getNextPageUrl() and …
tiwarishubham635 96ec664
docs: add TokenPaginationPage.php documentation
tiwarishubham635 a152794
Update src/Twilio/TokenPaginationPage.php
tiwarishubham635 bae09a4
chore: use camelCase for httpClient
tiwarishubham635 030a30f
Merge remote-tracking branch 'origin/add_token_pagination' into add_t…
tiwarishubham635 1a5d0c1
Update src/Twilio/TokenPaginationPage.php
tiwarishubham635 4c60bca
Update src/Twilio/TokenPaginationPage.php
tiwarishubham635 169af69
docs: add docblocks for the functions
tiwarishubham635 c5c5953
Update tests/Twilio/Unit/TokenPaginationPageTest.php
tiwarishubham635 8b8d4f3
Update src/Twilio/TokenPaginationPage.php
tiwarishubham635 feafb45
Update src/Twilio/TokenPaginationPage.php
tiwarishubham635 073ac2f
Update src/Twilio/TokenPaginationPage.php
tiwarishubham635 e2bc596
chore: rename query string getter
tiwarishubham635 506f642
Update src/Twilio/TokenPaginationPage.php
tiwarishubham635 1a6d763
Update src/Twilio/TokenPaginationPage.php
tiwarishubham635 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
tiwarishubham635 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| protected $pageSize; | ||
| protected $nextToken; | ||
| protected $previousToken; | ||
| protected $url; | ||
| protected $previousPageUrl; | ||
| protected $nextPageUrl; | ||
|
|
||
tiwarishubham635 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * 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]; | ||
tiwarishubham635 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
tiwarishubham635 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| $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. | ||
tiwarishubham635 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * @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'); | ||
tiwarishubham635 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if ($this->key) { | ||
| return $this->payload[$this->key]; | ||
| } | ||
|
|
||
tiwarishubham635 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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) { | ||
tiwarishubham635 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $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]'; | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.