From 4e34c9c161cd0594cb5707c55fad72fba015ef53 Mon Sep 17 00:00:00 2001 From: Alvin See Date: Thu, 27 Jan 2022 12:06:26 +1100 Subject: [PATCH 1/7] add webhooks support --- src/Zendesk/API/HttpClient.php | 3 +++ src/Zendesk/API/Resources/Core/Webhooks.php | 14 ++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 src/Zendesk/API/Resources/Core/Webhooks.php diff --git a/src/Zendesk/API/HttpClient.php b/src/Zendesk/API/HttpClient.php index 417d5c6d9..4ca93ec05 100644 --- a/src/Zendesk/API/HttpClient.php +++ b/src/Zendesk/API/HttpClient.php @@ -54,6 +54,7 @@ use Zendesk\API\Resources\Core\UserFields; use Zendesk\API\Resources\Core\Users; use Zendesk\API\Resources\Core\Views; +use Zendesk\API\Resources\Core\Webhooks; use Zendesk\API\Resources\Embeddable; use Zendesk\API\Resources\HelpCenter; use Zendesk\API\Resources\Talk; @@ -98,6 +99,7 @@ * @method SuspendedTickets suspendedTickets() * @method Tags tags($id = null) * @method Targets targets() + * @method Webhooks webhooks() * @method Tickets tickets($id = null) * @method TicketFields ticketFields($id = null) * @method TicketImports ticketImports() @@ -281,6 +283,7 @@ public static function getValidSubResources() 'userFields' => UserFields::class, 'users' => Users::class, 'views' => Views::class, + 'webhooks' => Webhooks::class, ]; } diff --git a/src/Zendesk/API/Resources/Core/Webhooks.php b/src/Zendesk/API/Resources/Core/Webhooks.php new file mode 100644 index 000000000..244f989b0 --- /dev/null +++ b/src/Zendesk/API/Resources/Core/Webhooks.php @@ -0,0 +1,14 @@ + Date: Fri, 28 Jan 2022 14:02:06 +1100 Subject: [PATCH 2/7] remove .json --- src/Zendesk/API/Resources/Core/Webhooks.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Zendesk/API/Resources/Core/Webhooks.php b/src/Zendesk/API/Resources/Core/Webhooks.php index 244f989b0..b9bd07e7d 100644 --- a/src/Zendesk/API/Resources/Core/Webhooks.php +++ b/src/Zendesk/API/Resources/Core/Webhooks.php @@ -11,4 +11,16 @@ class Webhooks extends ResourceAbstract { use Defaults; + + /** + * {@inheritdoc} + */ + protected function setUpRoutes() + { + parent::setUpRoutes(); + + $this->setRoutes([ + 'webhooks' => "{$this->resourceName}", + ]); + } } From 0926c5aa5a26552a7e68a6dcf3a28166518ddf84 Mon Sep 17 00:00:00 2001 From: Alvin See Date: Fri, 28 Jan 2022 14:40:38 +1100 Subject: [PATCH 3/7] change route --- src/Zendesk/API/Resources/Core/Webhooks.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Zendesk/API/Resources/Core/Webhooks.php b/src/Zendesk/API/Resources/Core/Webhooks.php index b9bd07e7d..ce77dec9f 100644 --- a/src/Zendesk/API/Resources/Core/Webhooks.php +++ b/src/Zendesk/API/Resources/Core/Webhooks.php @@ -20,7 +20,9 @@ protected function setUpRoutes() parent::setUpRoutes(); $this->setRoutes([ - 'webhooks' => "{$this->resourceName}", + 'create' => "{$this->resourceName}", + 'update' => "{$this->resourceName}/{id}", + 'delete' => "{$this->resourceName}", ]); } } From b95e840bdb1134ebf83ae626d6a6b1c89aabb89b Mon Sep 17 00:00:00 2001 From: Alvin See Date: Wed, 9 Feb 2022 12:31:50 +1100 Subject: [PATCH 4/7] add findAll endpoint --- src/Zendesk/API/Resources/Core/Webhooks.php | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Zendesk/API/Resources/Core/Webhooks.php b/src/Zendesk/API/Resources/Core/Webhooks.php index ce77dec9f..d7ab344ac 100644 --- a/src/Zendesk/API/Resources/Core/Webhooks.php +++ b/src/Zendesk/API/Resources/Core/Webhooks.php @@ -23,6 +23,29 @@ protected function setUpRoutes() 'create' => "{$this->resourceName}", 'update' => "{$this->resourceName}/{id}", 'delete' => "{$this->resourceName}", + 'findAll' => "{$this->resourceName}", ]); } + + /** + * {@inheritdoc} + */ + public function findAll(array $params = []) + { + $queryParams = array_filter(array_flip($params), [$this, 'filterParams']); + $queryParams = array_merge($params, array_flip($queryParams)); + + return $this->traitFindAll($queryParams); + } + + /** + * Filter parameters passed and only allow valid query parameters. + * + * @param $param + * @return int + */ + private function filterParams($param) + { + return preg_match("/^sort|page[[a-zA-Z_]*]|filter[[a-zA-Z_]*](\\[\\]?)/", $param); + } } From 8d76db51fb8e518fccf9d2f7e0d0d8835f483777 Mon Sep 17 00:00:00 2001 From: Alvin See Date: Wed, 9 Feb 2022 12:56:06 +1100 Subject: [PATCH 5/7] add --- src/Zendesk/API/Resources/Core/Webhooks.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Zendesk/API/Resources/Core/Webhooks.php b/src/Zendesk/API/Resources/Core/Webhooks.php index d7ab344ac..37def0ca1 100644 --- a/src/Zendesk/API/Resources/Core/Webhooks.php +++ b/src/Zendesk/API/Resources/Core/Webhooks.php @@ -11,6 +11,9 @@ class Webhooks extends ResourceAbstract { use Defaults; + use FindAll { + findAll as traitFindAll; + } /** * {@inheritdoc} From bc0cf487f416ca95e50ce5d6c0447f9e2d22498f Mon Sep 17 00:00:00 2001 From: Alvin See Date: Wed, 9 Feb 2022 13:23:05 +1100 Subject: [PATCH 6/7] import FindAll --- src/Zendesk/API/Resources/Core/Webhooks.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Zendesk/API/Resources/Core/Webhooks.php b/src/Zendesk/API/Resources/Core/Webhooks.php index 37def0ca1..6c462e06c 100644 --- a/src/Zendesk/API/Resources/Core/Webhooks.php +++ b/src/Zendesk/API/Resources/Core/Webhooks.php @@ -4,6 +4,7 @@ use Zendesk\API\Resources\ResourceAbstract; use Zendesk\API\Traits\Resource\Defaults; +use Zendesk\API\Traits\Resource\FindAll; /** * The Tags class exposes methods as detailed on https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/ From 83a1672cc71cb390410c83efb38a66650ae0cbbb Mon Sep 17 00:00:00 2001 From: Alvin See Date: Thu, 10 Feb 2022 10:06:18 +1100 Subject: [PATCH 7/7] add webhooks find all test --- .../API/UnitTests/Core/WebhooksTest.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/Zendesk/API/UnitTests/Core/WebhooksTest.php diff --git a/tests/Zendesk/API/UnitTests/Core/WebhooksTest.php b/tests/Zendesk/API/UnitTests/Core/WebhooksTest.php new file mode 100644 index 000000000..0c0e40efe --- /dev/null +++ b/tests/Zendesk/API/UnitTests/Core/WebhooksTest.php @@ -0,0 +1,38 @@ + 'somerule', + ]; + + // We expect invalid parameters are removed. + // We also expect url encoded keys and values + $expectedQueryParams = []; + foreach ($queryParams as $key => $value) { + $expectedQueryParams = array_merge($expectedQueryParams, [urlencode($key) => $value]); + } + + $this->assertEndpointCalled( + function () use ($queryParams) { + $this->client->webhooks()->findAll($queryParams); + }, + 'webhooks', + 'GET', + ['queryParams' => $expectedQueryParams] + ); + } +}