Skip to content

Commit

Permalink
Merge pull request #150 from alexschastny/add-batching-enabled-param-…
Browse files Browse the repository at this point in the history
…support
  • Loading branch information
Brandin committed May 12, 2023
2 parents 201c00d + 591bcc8 commit 54c9ae2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
22 changes: 14 additions & 8 deletions spec/TwitchApi/Resources/EventSubApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ class EventSubApiSpec extends ObjectBehavior
private string $secret = 'SECRET';
private string $callback = 'https://example.com/';

private function createEventSubSubscription(string $type, string $version, array $condition, RequestGenerator $requestGenerator)
private function createEventSubSubscription(string $type, string $version, array $condition, RequestGenerator $requestGenerator, bool $isBatchingEnabled = null)
{
$bodyParams = [];

$bodyParams[] = ['key' => 'type', 'value' => $type];
$bodyParams[] = ['key' => 'version', 'value' => $version];
$bodyParams[] = ['key' => 'condition', 'value' => $condition];
$bodyParams[] = ['key' => 'transport', 'value' => [
'method' => 'webhook',
'callback' => $this->callback,
'secret' => $this->secret,
]
$bodyParams[] = [
'key' => 'transport',
'value' => [
'method' => 'webhook',
'callback' => $this->callback,
'secret' => $this->secret,
],
];

if (null !== $isBatchingEnabled) {
$bodyParams[] = ['key' => 'is_batching_enabled', 'value' => $isBatchingEnabled];
}

return $requestGenerator->generate('POST', 'eventsub/subscriptions', $this->bearer, [], $bodyParams);
}

Expand Down Expand Up @@ -291,13 +297,13 @@ function it_should_subscribe_to_channel_goal_end(RequestGenerator $requestGenera

function it_should_subscribe_to_drop_entitelement_grant(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('drop.entitlement.grant', '1', ['organization_id' => '12345'], $requestGenerator)->willReturn($request);
$this->createEventSubSubscription('drop.entitlement.grant', '1', ['organization_id' => '12345'], $requestGenerator, true)->willReturn($request);
$this->subscribeToDropEntitlementGrant($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_drop_entitelement_grant_with_opts(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('drop.entitlement.grant', '1', ['organization_id' => '123', 'category_id' => '456', 'campaign_id' => '789'], $requestGenerator)->willReturn($request);
$this->createEventSubSubscription('drop.entitlement.grant', '1', ['organization_id' => '123', 'category_id' => '456', 'campaign_id' => '789'], $requestGenerator, true)->willReturn($request);
$this->subscribeToDropEntitlementGrant($this->bearer, $this->secret, $this->callback, '123', '456', '789')->shouldBe($response);
}

Expand Down
7 changes: 6 additions & 1 deletion src/Resources/EventSubApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getEventSubSubscription(string $bearer, string $status = null, s
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#create-eventsub-subscription
*/
public function createEventSubSubscription(string $bearer, string $secret, string $callback, string $type, string $version, array $condition): ResponseInterface
public function createEventSubSubscription(string $bearer, string $secret, string $callback, string $type, string $version, array $condition, bool $isBatchingEnabled = null): ResponseInterface
{
$bodyParams = [];

Expand All @@ -56,6 +56,10 @@ public function createEventSubSubscription(string $bearer, string $secret, strin
],
];

if (null !== $isBatchingEnabled) {
$bodyParams[] = ['key' => 'is_batching_enabled', 'value' => $isBatchingEnabled];
}

return $this->postApi('eventsub/subscriptions', $bearer, [], $bodyParams);
}

Expand Down Expand Up @@ -511,6 +515,7 @@ public function subscribeToDropEntitlementGrant(string $bearer, string $secret,
'drop.entitlement.grant',
'1',
$condition,
true
);
}

Expand Down

0 comments on commit 54c9ae2

Please sign in to comment.