-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #517 from zendesk/RED-1984-order
Add CbpStrategy accepting OBP params
- Loading branch information
Showing
10 changed files
with
237 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,90 @@ | ||
# Upgrade guide | ||
|
||
## Useful links | ||
|
||
* [Pagination](https://developer.zendesk.com/api-reference/introduction/pagination) | ||
* [Ticketing sorting](https://developer.zendesk.com/api-reference/ticketing/tickets/tickets/#sorting) | ||
|
||
## CBP Basics | ||
|
||
**OBP (Offset Based Pagination)** is quite inefficient, and increasingly so the higher the page you fetch. Switching to **CBP (Cursor Based Pagination)** will improve your application performance. OBP will eventually be subject to limits. | ||
|
||
When using OBP, if you request page 100, the DB will need to index all records that proceed it before it can load the rows for the page you requested. | ||
|
||
CBP works based on cursors, so when ordering by `id` with page size 10, if the last item on your page has id 111, the response includes a link to the next page (`links.next`) which can be used to request the next 10 elements after id 111, and only the requested rows are indexed before fetching. When in the response `meta.has_more` is `false` you are done. | ||
|
||
## API calls | ||
|
||
URL example: | ||
|
||
* OBP: https://{subdomain}.zendesk.com/api/v2/tickets?sort_order=desc&sort_by=updated_at&per_page=2 | ||
* CBP: https://{subdomain}.zendesk.com/api/v2/tickets?sort=-updated_at&page[size]=2 | ||
|
||
### CBP ordering | ||
|
||
When moving from OBP to CBP sorting params change as well: | ||
|
||
* From: `?sort_name=updated_at&sort_order=desc` | ||
* To: `sort=-updated_at` | ||
|
||
Ticket example: | ||
|
||
* OBP: https://{subdomain}.zendesk.com/api/v2/tickets?sort_order=desc&sort_by=updated_at&per_page=2 | ||
* CBP: https://{subdomain}.zendesk.com/api/v2/tickets?sort=-updated_at&page[size]=2 | ||
|
||
However, the list of **attributes you can sort by might also change** with the pagination type: | ||
|
||
https://developer.zendesk.com/api-reference/ticketing/tickets/tickets/#sorting | ||
|
||
* OBP: `"assignee", "assignee.name", "created_at", "group", "id", "locale", "requester"` | ||
* CBP: `"updated_at", "id", "status"` | ||
|
||
Example: | ||
|
||
* OBP: https://{subdomain}.zendesk.com/api/v2/tickets?sort_order=desc&sort_by=assignee.name&per_page=2 `HTTP 200`, works | ||
* CBP: https://{subdomain}.zendesk.com/api/v2/tickets?sort=assignee.name&page[size]=2 `HTTP 400` | ||
|
||
```json | ||
{ | ||
"error": "InvalidPaginationParameter", | ||
"description": "sort is not valid" | ||
} | ||
``` | ||
|
||
If this is your situation, **you will need to change the sorting order** to a supported one. | ||
|
||
## The new iterator | ||
|
||
The most efficient and elegant way to implement CBP is to use the newly provided iterator on your resources, for example: | ||
|
||
```php | ||
$params = ['my' => 'param1', 'extra' => 'param2']; | ||
$iterator = $client->tickets()->iterator($params); | ||
|
||
foreach ($iterator as $ticket) { | ||
echo($ticket->id . " "); | ||
} | ||
``` | ||
|
||
This will choose the right type of pagination and adapt your parameters for pagination and ordering to work with CBP. | ||
|
||
##### Iterator with params example | ||
|
||
```php | ||
$params = ['my' => 'param1', 'extra' => 'param2']; | ||
$iterator = $client->tickets()->iterator($params); | ||
|
||
foreach ($iterator as $ticket) { | ||
echo($ticket->id . " "); | ||
} | ||
``` | ||
|
||
* Change page size with: `$params = ['page[size]' => 5];` | ||
* Change sorting with: `$params = ['sort' => '-updated_at'];` | ||
* Refer to the docs for details, including allowed sort fields | ||
* Combine everything: `$params = ['page[size]' => 2, 'sort' => 'updated_at', 'extra' => 'param'];` | ||
|
||
|
||
## Parallel requests | ||
|
||
If you are fetching multiple pages in parallel using OBP, you need to refactor to a serial execution, and fetch one page at a time. |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.