Skip to content

Commit

Permalink
Add "Get Webhooks" endpoint #187
Browse files Browse the repository at this point in the history
  • Loading branch information
jswift committed Mar 29, 2023
1 parent 49562cb commit 248fcee
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 9 deletions.
31 changes: 23 additions & 8 deletions src/BigCommerce/Api/Webhooks/WebhooksApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,38 @@

namespace BigCommerce\ApiV3\Api\Webhooks;

use BigCommerce\ApiV3\Api\Generic\GetResource;
use BigCommerce\ApiV3\Api\Generic\V3ApiBase;
use BigCommerce\ApiV3\Api\Generic\ResourceApi;
use BigCommerce\ApiV3\ResponseModels\Webhook\WebhookResponse;
use BigCommerce\ApiV3\ResponseModels\Webhook\WebhooksResponse;

class WebhooksApi extends V3ApiBase
class WebhooksApi extends ResourceApi
{
use GetResource;

public const WEBHOOK_ENDPOINT = 'hooks/%d';
public const WEBHOOKS_ENDPOINT = 'hooks';
public const RESOURCE_NAME = 'hooks';

public function singleResourceUrl(): string
public function get(): WebhookResponse
{
return new WebhookResponse($this->getResource());
}

protected function singleResourceEndpoint(): string
{
return self::WEBHOOK_ENDPOINT;
}

public function get(): WebhookResponse
protected function multipleResourcesEndpoint(): string
{
return new WebhookResponse($this->getResource());
return self::WEBHOOKS_ENDPOINT;
}

protected function resourceName(): string
{
return self::RESOURCE_NAME;
}

public function getAll(array $filters = [], int $page = 1, int $limit = 250): WebhooksResponse
{
return new WebhooksResponse($this->getAllResources($filters, $page, $limit));
}
}
5 changes: 5 additions & 0 deletions src/BigCommerce/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ public function webhook(int $id): WebhooksApi
return new WebhooksApi($this, $id);
}

public function webhooks(): WebhooksApi
{
return new WebhooksApi($this);
}

protected function defaultBaseUrl(): string
{
return self::API_URI;
Expand Down
21 changes: 21 additions & 0 deletions src/BigCommerce/ResponseModels/Webhook/WebhooksResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace BigCommerce\ApiV3\ResponseModels\Webhook;

use BigCommerce\ApiV3\ResourceModels\Webhook\Webhook;
use BigCommerce\ApiV3\ResponseModels\PaginatedResponse;

class WebhooksResponse extends PaginatedResponse
{
/**
* @return Webhook[]
*/
public function getWebhooks(): array
{
return $this->getData();
}
protected function resourceClass(): string
{
return Webhook::class;
}
}
9 changes: 8 additions & 1 deletion tests/BigCommerce/Api/Webhooks/WebhooksApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ public function testCanGetWebhook()
$this->assertEquals('https://665b65a6.ngrok.io/webhooks', $webhook->destination);
$this->assertEquals('string', $webhook->headers['custom']);
}
}

public function testCanGetWebhooks()
{
$this->setReturnData('webhooks__get_all.json');
$webhooks = $this->getApi()->webhooks()->getAll()->getWebhooks();
$this->assertEquals('store/order/*', $webhooks[0]->scope);
}
}
16 changes: 16 additions & 0 deletions tests/BigCommerce/responses/webhooks__get_all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"data": [{
"id": 18048287,
"client_id": "m9r6keqmo7h7f23btnpwernbez1kglkl",
"store_hash": "sftg45fsd",
"created_at": 1561488106,
"updated_at": 1561488106,
"scope": "store/order/*",
"destination": "https://665b65a6.ngrok.io/webhooks",
"is_active": true,
"headers": {
"custom": "string"
}
}],
"meta": {}
}

0 comments on commit 248fcee

Please sign in to comment.