Skip to content

Commit

Permalink
Add Webhook Admin endpoints #187
Browse files Browse the repository at this point in the history
  • Loading branch information
jswift committed Mar 30, 2023
1 parent 66a79fa commit 89a1fef
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 2 deletions.
41 changes: 41 additions & 0 deletions src/BigCommerce/Api/Webhooks/WebhookAdminApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace BigCommerce\ApiV3\Api\Webhooks;

use BigCommerce\ApiV3\Api\Generic\GetResource;
use BigCommerce\ApiV3\Api\Generic\V3ApiBase;
use BigCommerce\ApiV3\ResponseModels\Webhook\AdminInfoResponse;
use GuzzleHttp\RequestOptions;

class WebhookAdminApi extends V3ApiBase
{
use GetResource;

public const WEBHOOK_ADMIN_ENDPOINT = 'hooks/admin';

public function singleResourceUrl(): string
{
return self::WEBHOOK_ADMIN_ENDPOINT;
}

public function get(?bool $isActive = null): AdminInfoResponse
{
$filter = is_null($isActive) ? [] : ['is_active' => $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]
]
);
}
}
5 changes: 5 additions & 0 deletions src/BigCommerce/Api/Webhooks/WebhooksApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ public function events(): WebhookEventsApi
{
return new WebhookEventsApi($this->getClient());
}

public function admin(): WebhookAdminApi
{
return new WebhookAdminApi($this->getClient());
}
}
22 changes: 22 additions & 0 deletions src/BigCommerce/ResourceModels/Webhook/BlockedDomain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace BigCommerce\ApiV3\ResourceModels\Webhook;

use BigCommerce\ApiV3\ResourceModels\ResourceModel;

class BlockedDomain extends ResourceModel
{
public string $destination;
public string $time_left;

/**
* @var BlockedDomainReason[]
*/
public array $reasons;

protected function beforeBuildObject(): void
{
self::buildObjectArray('reasons', BlockedDomainReason::class);
parent::beforeBuildObject();
}
}
12 changes: 12 additions & 0 deletions src/BigCommerce/ResourceModels/Webhook/BlockedDomainReason.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace BigCommerce\ApiV3\ResourceModels\Webhook;

use BigCommerce\ApiV3\ResourceModels\ResourceModel;

class BlockedDomainReason extends ResourceModel
{
public string $failure_description;
public int $count;
public int $timestamp;
}
4 changes: 2 additions & 2 deletions src/BigCommerce/ResourceModels/Webhook/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class Webhook extends ResourceModel
public string $store_hash;
public string $scope;
public string $destination;
public array $headers;
public ?array $headers;
public string $created_at;
public string $updated_at;
public bool $is_active;

protected function beforeBuildObject(): void
{
parent::beforeBuildObject();
self::buildHashArray('headers');
parent::beforeBuildObject();
}
}
28 changes: 28 additions & 0 deletions src/BigCommerce/ResourceModels/Webhook/WebhookAdminInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace BigCommerce\ApiV3\ResourceModels\Webhook;

use BigCommerce\ApiV3\ResourceModels\ResourceModel;

class WebhookAdminInfo extends ResourceModel
{
/**
* @var string[]
*/
public array $emails;
/**
* @var Webhook[]
*/
public array $hooks_list;
/**
* @var BlockedDomain[]
*/
public array $blocked_domains;

protected function beforeBuildObject(): void
{
self::buildObjectArray('hooks_list', Webhook::class);
self::buildObjectArray('blocked_domains', BlockedDomain::class);
parent::beforeBuildObject();
}
}
22 changes: 22 additions & 0 deletions src/BigCommerce/ResponseModels/Webhook/AdminInfoResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace BigCommerce\ApiV3\ResponseModels\Webhook;

use BigCommerce\ApiV3\ResourceModels\Webhook\WebhookAdminInfo;
use BigCommerce\ApiV3\ResponseModels\SingleResourceResponse;
use stdClass;

class AdminInfoResponse extends SingleResourceResponse
{
private WebhookAdminInfo $adminInfo;

public function getAdminInfo(): WebhookAdminInfo
{
return $this->adminInfo;
}

protected function addData(stdClass $rawData): void
{
$this->adminInfo = new WebhookAdminInfo($rawData);
}
}
16 changes: 16 additions & 0 deletions tests/BigCommerce/Api/Webhooks/WebhookAdminApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace BigCommerce\Tests\Api\Webhooks;

use BigCommerce\Tests\BigCommerceApiTest;

class WebhookAdminApiTest extends BigCommerceApiTest
{
public function testCanGetAdminInfo()
{
$this->setReturnData('webhooks__admin__get.json');
$info = $this->getApi()->webhooks()->admin()->get()->getAdminInfo();

$this->assertEquals('https://httpstat.us/200', $info->hooks_list[1]->destination);
}
}
35 changes: 35 additions & 0 deletions tests/BigCommerce/responses/webhooks__admin__get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"data": {
"emails": [
"[email protected]"
],
"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": {}
}

0 comments on commit 89a1fef

Please sign in to comment.