Skip to content

Commit

Permalink
Add Webhook Events endpoint #187
Browse files Browse the repository at this point in the history
  • Loading branch information
jswift committed Mar 30, 2023
1 parent 976196e commit 66a79fa
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/BigCommerce/Api/Webhooks/WebhookEventsApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace BigCommerce\ApiV3\Api\Webhooks;

use BigCommerce\ApiV3\Api\Generic\GetAllResources;
use BigCommerce\ApiV3\Api\Generic\V3ApiBase;
use BigCommerce\ApiV3\ResponseModels\Webhook\WebhookEventsResponse;

class WebhookEventsApi extends V3ApiBase
{
use GetAllResources;

public const EVENTS_ENDPOINT = 'hooks/events';

public function multipleResourceUrl(): string
{
return self::EVENTS_ENDPOINT;
}

/**
* Get a list of events that were sent but not successfully received. Events are stored for not less than one week
*/
public function getAll(array $filters = [], int $page = 1, int $limit = 250): WebhookEventsResponse
{
return new WebhookEventsResponse($this->getAllResources($filters, $page, $limit));
}
}
7 changes: 6 additions & 1 deletion src/BigCommerce/Api/Webhooks/WebhooksApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function create(Webhook $webhook): WebhookResponse
return new WebhookResponse($this->createResource($webhook));
}

public function update(Webhook $webhook): WebhookResponse
public function update(Webhook $webhook): WebhookResponse
{
return new WebhookResponse($this->updateResource($webhook));
}
Expand All @@ -51,4 +51,9 @@ public function getAll(array $filters = [], int $page = 1, int $limit = 250): We
{
return new WebhooksResponse($this->getAllResources($filters, $page, $limit));
}

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

namespace BigCommerce\ApiV3\ResourceModels\Webhook;

use BigCommerce\ApiV3\ResourceModels\ResourceModel;

class WebhookEvent extends ResourceModel
{
public string $scope;
public string $store_id;
public object $data;
public string $hash;
public string $created_at;
public string $producer;
}
22 changes: 22 additions & 0 deletions src/BigCommerce/ResponseModels/Webhook/WebhookEventsResponse.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\WebhookEvent;
use BigCommerce\ApiV3\ResponseModels\PaginatedResponse;

class WebhookEventsResponse extends PaginatedResponse
{
/**
* @return WebhookEvent[]
*/
public function getEvents(): array
{
return $this->getData();
}

protected function resourceClass(): string
{
return WebhookEvent::class;
}
}
16 changes: 16 additions & 0 deletions tests/BigCommerce/Api/Webhooks/WebhookEventsApiTest.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 WebhookEventsApiTest extends BigCommerceApiTest
{
public function testCanGetAllEvents()
{
$this->setReturnData('webhooks__events__get_all.json');
$events = $this->getApi()->webhooks()->events()->getAll()->getEvents();

$this->assertEquals('store/sku/inventory/updated', $events[0]->scope);
}
}
24 changes: 24 additions & 0 deletions tests/BigCommerce/responses/webhooks__events__get_all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"data": [
{
"scope": "store/sku/inventory/updated",
"store_id": "25967773",
"data": {
"product": "new",
"stock": 123
},
"hash": "0c20013b66c630f5c7473eef26bd71089bb461b4",
"created_at": 1680144702,
"producer": "stores/xxbwhvt3gd"
}
],
"meta": {
"pagination": {
"total": 1,
"count": 1,
"per_page": 250,
"current_page": 1,
"total_pages": 1
}
}
}

0 comments on commit 66a79fa

Please sign in to comment.