From b00d538b840cac08172345d448cc5c5c08ba51cc Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Fri, 17 Nov 2023 11:00:15 +1000 Subject: [PATCH 1/5] Delete CbpStrategy sortParams() --- .../Traits/Utility/Pagination/CbpStrategy.php | 20 +------------------ 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php index 6b481ce4..f392fbe3 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php @@ -35,30 +35,12 @@ public function shouldGetPage($position) { public function params() { - $result = array_merge($this->params, $this->paginationParams(), $this->sortParams()); + $result = array_merge($this->params, $this->paginationParams()); $result = $this->unsetObpParams($result); return $result; } - /** - * The params that are needed to ordering in CBP (eg: ["sort" => "-age"]) - * If OBP params are passed, they are converted to CBP - * - * 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 - * - * @return array Params with proper CBP sorting order - */ - private function sortParams() - { - if (isset($this->params['sort_by']) && !isset($this->params['sort'])) { - $direction = (isset($this->params['sort_order']) && strtolower($this->params['sort_order']) === 'desc') ? '-' : ''; - return array_merge($this->params, ['sort' => $direction . $this->params['sort_by']]); - } else { - return []; - } - } /** * The params that are needed to for pagination (eg: ["page[size]" => "100"]) * If OBP params are passed, they are converted to CBP From 8f4255469dcbf67e0c32aaf35d2f9a148958f4f3 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Fri, 17 Nov 2023 11:01:26 +1000 Subject: [PATCH 2/5] CBP_UPGRADE_GUIDE bump lib --- CBP_UPGRADE_GUIDE.md | 46 ++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/CBP_UPGRADE_GUIDE.md b/CBP_UPGRADE_GUIDE.md index 84216092..6c3793bc 100644 --- a/CBP_UPGRADE_GUIDE.md +++ b/CBP_UPGRADE_GUIDE.md @@ -1,12 +1,10 @@ # OBP to CBP Upgrade guide -TODO version requirement, how to bump. - ## Useful links -* [This README](./README.md#pagination) -* [Pagination](https://developer.zendesk.com/api-reference/introduction/pagination) -* [Ticketing sorting](https://developer.zendesk.com/api-reference/ticketing/tickets/tickets/#sorting) +- [This README](./README.md#pagination) +- [Pagination](https://developer.zendesk.com/api-reference/introduction/pagination) +- [Ticketing sorting](https://developer.zendesk.com/api-reference/ticketing/tickets/tickets/#sorting) ## CBP Basics @@ -16,47 +14,53 @@ When using OBP, if you request page 100, the DB will need to index all records t 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. +## The new iterator + +The best way to upgrade your app to support CBP is to use the iterator we provide. Please refer to the [README](./README.md#iterator-recommended). + +**Note**: The iterator will automatically convert the param `page_size` to work with CBP. + +The iterator is available in `v2.3`, which you can download with: + +```sh +composer update zendesk/zendesk_api_client_php +``` + ## API calls Note the query parameters change in these two URL examples: -* 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 +- 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` +- From: `?sort_name=updated_at&sort_order=desc` +- To: `sort=-updated_at` 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"` +- 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` +- 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" + "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 best way to upgrade your app to support CBP is to use the iterator we provide. Please refer to the [README](./README.md#iterator-recommended). - -**Note**: The iterator will automatically convert these parameters to work with CBP: `page_size, sort_by, sort_order`. - ## 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. From 660d79871c9e571d190e64ab8f616b33d956ab91 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Fri, 17 Nov 2023 11:03:01 +1000 Subject: [PATCH 3/5] composer.json php 7.4 --- CBP_UPGRADE_GUIDE.md | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CBP_UPGRADE_GUIDE.md b/CBP_UPGRADE_GUIDE.md index 6c3793bc..33d20ac9 100644 --- a/CBP_UPGRADE_GUIDE.md +++ b/CBP_UPGRADE_GUIDE.md @@ -20,7 +20,7 @@ The best way to upgrade your app to support CBP is to use the iterator we provid **Note**: The iterator will automatically convert the param `page_size` to work with CBP. -The iterator is available in `v2.3`, which you can download with: +The iterator is available in `v3.0`, which you can download with: ```sh composer update zendesk/zendesk_api_client_php diff --git a/composer.json b/composer.json index ced941ef..99fca817 100755 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "license": "Apache-2.0", "homepage": "https://github.com/zendesk/zendesk_api_client_php", "require": { - "php": ">=5.5.0", + "php": ">=7.4.0", "guzzlehttp/guzzle": "^6.0 || ^7.0", "guzzlehttp/psr7": "^1.7 || ^2.0", "mmucklo/inflect": "0.3.*" From 685ea929135b9370a95317aa8eb6fc98b72362c5 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Fri, 17 Nov 2023 11:30:23 +1000 Subject: [PATCH 4/5] Zendesk\API\Http log_api_calls --- CBP_UPGRADE_GUIDE.md | 4 ++++ README.md | 19 +++++++++++++++++++ src/Zendesk/API/Http.php | 4 +++- src/Zendesk/API/HttpClient.php | 8 ++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CBP_UPGRADE_GUIDE.md b/CBP_UPGRADE_GUIDE.md index 33d20ac9..feaac997 100644 --- a/CBP_UPGRADE_GUIDE.md +++ b/CBP_UPGRADE_GUIDE.md @@ -26,6 +26,10 @@ The iterator is available in `v3.0`, which you can download with: composer update zendesk/zendesk_api_client_php ``` +## Debugging + +Please refer to the [README](./README.md#debugging). + ## API calls Note the query parameters change in these two URL examples: diff --git a/README.md b/README.md index bf7e2cd8..73282390 100755 --- a/README.md +++ b/README.md @@ -275,11 +275,30 @@ speed up the process and would make sure that everybody follows the community's ### Debugging +#### REPL + To help would be contributors, we've added a REPL tool. It is a simple wrapper for [psysh](http://psysh.org) and symfony's console. On your terminal, run `bin/console `. This would automatically create an instance of `Zendesk\API\HttpClient` on $client variable. After that you would be able to enter any valid php statement. The goal of the tool is to speed up the process in which developers can experiment on the code base. +#### HTTP client print API calls + +You can print a line with details about every API call with: + +```php +$client = new ZendeskAPI($subdomain); +$client->log_api_calls = true; +``` + +#### HTTP client debug + +You can inspect this object for info about requests and responses: + +```php +$client->getDebug(); +``` + ## Copyright and license Copyright 2013-present Zendesk diff --git a/src/Zendesk/API/Http.php b/src/Zendesk/API/Http.php index 6e9f9a4a..14fba49f 100644 --- a/src/Zendesk/API/Http.php +++ b/src/Zendesk/API/Http.php @@ -90,7 +90,9 @@ public static function send( if ($client->getAuth()) { list ($request, $requestOptions) = $client->getAuth()->prepareRequest($request, $requestOptions); } - // echo "\nExternal API call: " . $request->getMethod() . " " . $request->getUri() . "\n"; + if ($client->log_api_calls) { + echo "\nZendesk\API\Http: " . $request->getMethod() . " " . $request->getUri() . "\n"; + } $response = $client->guzzle->send($request, $requestOptions); } catch (RequestException $e) { $requestException = RequestException::create($e->getRequest(), $e->getResponse(), $e); diff --git a/src/Zendesk/API/HttpClient.php b/src/Zendesk/API/HttpClient.php index 9895ba8f..4a54ec0d 100644 --- a/src/Zendesk/API/HttpClient.php +++ b/src/Zendesk/API/HttpClient.php @@ -159,6 +159,14 @@ class HttpClient */ protected $debug; + /** + * Whether or not to print every API call details right before execution + * + * E.G.: Zendesk\API\Http: GET https://my_company.zendesk.com/api/v2/tickets.json + * + * @var boolean + */ + public $log_api_calls = false; /** * @var \GuzzleHttp\Client */ From c1bb17622bafd446b7cc439bb768d4560c20f95a Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Fri, 17 Nov 2023 11:35:28 +1000 Subject: [PATCH 5/5] Delete CbpStrategy sortParams() --- CBP_UPGRADE_GUIDE.md | 2 +- README.md | 2 ++ .../Traits/Utility/PaginationIteratorTest.php | 17 ----------------- 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/CBP_UPGRADE_GUIDE.md b/CBP_UPGRADE_GUIDE.md index feaac997..45a11a37 100644 --- a/CBP_UPGRADE_GUIDE.md +++ b/CBP_UPGRADE_GUIDE.md @@ -39,7 +39,7 @@ Note the query parameters change in these two URL examples: ### CBP ordering -When moving from OBP to CBP sorting params change as well: +When moving from OBP to CBP sorting params _may_ change as well: - From: `?sort_name=updated_at&sort_order=desc` - To: `sort=-updated_at` diff --git a/README.md b/README.md index 73282390..b09caf4f 100755 --- a/README.md +++ b/README.md @@ -155,6 +155,8 @@ foreach ($iterator as $ticket) { * Refer to the docs for details, including allowed sort fields * Combine everything: `$params = ['page[size]' => 2, 'sort' => 'updated_at', 'extra' => 'param'];` +**Note**: Refer to the documentation for the correct params for sorting with the pagination type you're using. + ##### Iterator API call response The latest response is exposed in the iterator at `$iterator->latestResponse()`. This could come handy for debugging. diff --git a/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationIteratorTest.php b/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationIteratorTest.php index e0233eee..1e9fa7b4 100644 --- a/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationIteratorTest.php +++ b/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationIteratorTest.php @@ -125,23 +125,6 @@ public function testFetchesCbpWithParams() ], $mockTickets->params); } - public function testCorrectsParamsToCbp() - { - $mockTickets = new MockResource('tickets', [ - [['id' => 1], ['id' => 2]], - [['id' => 3], ['id' => 4]] - ]); - $strategy = new CbpStrategy('tickets', ['per_page' => 2, 'sort_by' => 'id', 'sort_order' => 'desc']); - $iterator = new PaginationIterator($mockTickets, $strategy, 'findAll'); - - iterator_to_array($iterator); - - $this->assertEquals([ - 'sort' => '-id', - 'page[size]' => 2, 'page[after]' => 'cursor_for_next_page' - ], $mockTickets->params); - } - public function testFetchesSinglePageWithParams() { $resultsKey = 'results';