From 89a1fef5220f1f0c673ca89ba273de3324d39445 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Thu, 30 Mar 2023 14:41:52 +1030 Subject: [PATCH] Add Webhook Admin endpoints #187 --- .../Api/Webhooks/WebhookAdminApi.php | 41 +++++++++++++++++++ src/BigCommerce/Api/Webhooks/WebhooksApi.php | 5 +++ .../ResourceModels/Webhook/BlockedDomain.php | 22 ++++++++++ .../Webhook/BlockedDomainReason.php | 12 ++++++ .../ResourceModels/Webhook/Webhook.php | 4 +- .../Webhook/WebhookAdminInfo.php | 28 +++++++++++++ .../Webhook/AdminInfoResponse.php | 22 ++++++++++ .../Api/Webhooks/WebhookAdminApiTest.php | 16 ++++++++ .../responses/webhooks__admin__get.json | 35 ++++++++++++++++ 9 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 src/BigCommerce/Api/Webhooks/WebhookAdminApi.php create mode 100644 src/BigCommerce/ResourceModels/Webhook/BlockedDomain.php create mode 100644 src/BigCommerce/ResourceModels/Webhook/BlockedDomainReason.php create mode 100644 src/BigCommerce/ResourceModels/Webhook/WebhookAdminInfo.php create mode 100644 src/BigCommerce/ResponseModels/Webhook/AdminInfoResponse.php create mode 100644 tests/BigCommerce/Api/Webhooks/WebhookAdminApiTest.php create mode 100644 tests/BigCommerce/responses/webhooks__admin__get.json diff --git a/src/BigCommerce/Api/Webhooks/WebhookAdminApi.php b/src/BigCommerce/Api/Webhooks/WebhookAdminApi.php new file mode 100644 index 0000000..ae1d834 --- /dev/null +++ b/src/BigCommerce/Api/Webhooks/WebhookAdminApi.php @@ -0,0 +1,41 @@ + $isActive]; + return new AdminInfoResponse($this->getResource($filter)); + } + + /** + * Update email addresses that are sent notification emails when any domain associated with the API account + * is denylisted or when a webhook is deactivated. Supports upsert functionality in the case that no email + * address exists yet. + */ + public function upsertEmails(array $emails): void + { + $this->getClient()->getRestClient()->put( + $this->singleResourceUrl(), + [ + RequestOptions::JSON => ['emails' => $emails] + ] + ); + } +} diff --git a/src/BigCommerce/Api/Webhooks/WebhooksApi.php b/src/BigCommerce/Api/Webhooks/WebhooksApi.php index 980307e..255b7b1 100644 --- a/src/BigCommerce/Api/Webhooks/WebhooksApi.php +++ b/src/BigCommerce/Api/Webhooks/WebhooksApi.php @@ -56,4 +56,9 @@ public function events(): WebhookEventsApi { return new WebhookEventsApi($this->getClient()); } + + public function admin(): WebhookAdminApi + { + return new WebhookAdminApi($this->getClient()); + } } diff --git a/src/BigCommerce/ResourceModels/Webhook/BlockedDomain.php b/src/BigCommerce/ResourceModels/Webhook/BlockedDomain.php new file mode 100644 index 0000000..270a8e5 --- /dev/null +++ b/src/BigCommerce/ResourceModels/Webhook/BlockedDomain.php @@ -0,0 +1,22 @@ +adminInfo; + } + + protected function addData(stdClass $rawData): void + { + $this->adminInfo = new WebhookAdminInfo($rawData); + } +} diff --git a/tests/BigCommerce/Api/Webhooks/WebhookAdminApiTest.php b/tests/BigCommerce/Api/Webhooks/WebhookAdminApiTest.php new file mode 100644 index 0000000..6852171 --- /dev/null +++ b/tests/BigCommerce/Api/Webhooks/WebhookAdminApiTest.php @@ -0,0 +1,16 @@ +setReturnData('webhooks__admin__get.json'); + $info = $this->getApi()->webhooks()->admin()->get()->getAdminInfo(); + + $this->assertEquals('https://httpstat.us/200', $info->hooks_list[1]->destination); + } +} diff --git a/tests/BigCommerce/responses/webhooks__admin__get.json b/tests/BigCommerce/responses/webhooks__admin__get.json new file mode 100644 index 0000000..32658a1 --- /dev/null +++ b/tests/BigCommerce/responses/webhooks__admin__get.json @@ -0,0 +1,35 @@ +{ + "data": { + "emails": [ + "jarrod.swift@aligent.com.au" + ], + "hooks_list": [ + { + "id": 25967773, + "client_id": "6qczasxdcbgvjubghnynimwthwr", + "store_hash": "xxabcdt9gd", + "scope": "store/sku/inventory/updated", + "destination": "https://aligent.com.au/test", + "headers": null, + "is_active": true, + "created_at": 1680144702, + "updated_at": 1680144702, + "events_history_enabled": false + }, + { + "id": 25967774, + "client_id": "6qczasxdcbgvjubghnynimwthwr", + "store_hash": "xxabcdt9gd", + "scope": "store/sku/inventory/updated", + "destination": "https://httpstat.us/200", + "headers": null, + "is_active": true, + "created_at": 1680144907, + "updated_at": 1680144907, + "events_history_enabled": false + } + ], + "blocked_domains": [] + }, + "meta": {} +} \ No newline at end of file