Skip to content

Commit

Permalink
Merge pull request #100 from ThibaultVlacich/event-sub
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandin authored May 28, 2021
2 parents 9bed0f6 + bbe4483 commit 391a196
Show file tree
Hide file tree
Showing 5 changed files with 745 additions and 1 deletion.
6 changes: 6 additions & 0 deletions spec/NewTwitchApi/NewTwitchApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use NewTwitchApi\Resources\ChannelPointsApi;
use NewTwitchApi\Resources\ChannelsApi;
use NewTwitchApi\Resources\EntitlementsApi;
use NewTwitchApi\Resources\EventSubApi;
use NewTwitchApi\Resources\GamesApi;
use NewTwitchApi\Resources\ModerationApi;
use NewTwitchApi\Resources\PollsApi;
Expand Down Expand Up @@ -61,6 +62,11 @@ function it_should_provide_entitlements_api()
$this->getEntitlementsApi()->shouldBeAnInstanceOf(EntitlementsApi::class);
}

function it_should_provide_event_sub_api()
{
$this->getEventSubApi()->shouldBeAnInstanceOf(EventSubApi::class);
}

function it_should_provide_games_api()
{
$this->getGamesApi()->shouldBeAnInstanceOf(GamesApi::class);
Expand Down
237 changes: 237 additions & 0 deletions spec/NewTwitchApi/Resources/EventSubApiSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
<?php

namespace spec\NewTwitchApi\Resources;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use NewTwitchApi\RequestGenerator;
use PhpSpec\ObjectBehavior;

class EventSubApiSpec extends ObjectBehavior
{
private string $bearer = 'TEST_TOKEN';
private string $secret = 'SECRET';
private string $callback = 'https://example.com/';

private function createEventSubSubscription(string $type, string $version, array $condition, RequestGenerator $requestGenerator)
{
$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,
]
];

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

function let(Client $guzzleClient, RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->beConstructedWith($guzzleClient, $requestGenerator);
$guzzleClient->send($request)->willReturn($response);
}

function it_should_get_event_sub_subscription(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('GET', 'eventsub/subscriptions', 'TEST_TOKEN', [], [])->willReturn($request);
$this->getEventSubSubscription('TEST_TOKEN')->shouldBe($response);
}

function it_should_get_event_sub_subscription_with_status(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('GET', 'eventsub/subscriptions', 'TEST_TOKEN', [['key' => 'status', 'value' => 'enabled']], [])->willReturn($request);
$this->getEventSubSubscription('TEST_TOKEN', 'enabled')->shouldBe($response);
}

function it_should_get_event_sub_subscription_with_type(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('GET', 'eventsub/subscriptions', 'TEST_TOKEN', [['key' => 'type', 'value' => 'channel.update']], [])->willReturn($request);
$this->getEventSubSubscription('TEST_TOKEN', null, 'channel.update')->shouldBe($response);
}

function it_should_delete_event_sub_subscription(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('DELETE', 'eventsub/subscriptions', 'TEST_TOKEN', [['key' => 'id', 'value' => '123']], [])->willReturn($request);
$this->deleteEventSubSubscription('TEST_TOKEN', '123')->shouldBe($response);
}

function it_should_subscribe_to_channel_update(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.update', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelUpdate($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_follow(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.follow', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelFollow($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_subscribe(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.subscribe', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelSubscribe($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_unsubscribe(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.unsubscribe', 'beta', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelUnsubscribe($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_cheer(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.cheer', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelCheer($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_raid(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.raid', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelRaid($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_ban(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.ban', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelBan($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_unban(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.unban', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelUnban($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_moderator_add(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.moderator.add', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelModeratorAdd($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_moderator_remove(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.moderator.remove', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelModeratorRemove($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_points_custom_reward_add(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.channel_points_custom_reward.add', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelPointsCustomRewardAdd($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_points_custom_reward_update(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.channel_points_custom_reward.update', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelPointsCustomRewardUpdate($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_points_custom_reward_remove(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.channel_points_custom_reward.remove', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelPointsCustomRewardRemove($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_points_custom_reward_redemption_add(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.channel_points_custom_reward_redemption.add', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelPointsCustomRewardRedemptionAdd($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_points_custom_reward_redemption_update(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.channel_points_custom_reward_redemption.update', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelPointsCustomRewardRedemptionUpdate($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_poll_begin(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.poll.begin', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelPollBegin($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_poll_progress(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.poll.progress', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelPollProgress($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_poll_endn(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.poll.end', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelPollEnd($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_prediction_begin(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.prediction.begin', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelPredictionBegin($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_prediction_progress(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.prediction.progress', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelPredictionProgress($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_prediction_lock(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.prediction.lock', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelPredictionLock($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_prediction_endn(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.prediction.end', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelPredictionEnd($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_hype_train_begin(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.hype_train.begin', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelHypeTrainBegin($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_hype_train_progress(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.hype_train.progress', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelHypeTrainProgress($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_channel_hype_train_end(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.hype_train.end', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelHypeTrainEnd($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_stream_online(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('stream.online', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToStreamOnline($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_stream_offline(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('stream.offline', '1', ['broadcaster_user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToStreamOffline($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_user_update(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('user.update', '1', ['user_id' => '12345'], $requestGenerator)->willReturn($request);
$this->subscribeToUserUpdate($this->bearer, $this->secret, $this->callback, '12345')->shouldBe($response);
}

function it_should_subscribe_to_extension_bits_transaction_create(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('extension.bits_transaction.create', 'beta', ['extension_client_id' => 'deadbeef'], $requestGenerator)->willReturn($request);
$this->subscribeToExtensionBitsTransactionCreate($this->bearer, $this->secret, $this->callback, 'deadbeef')->shouldBe($response);
}
}
8 changes: 8 additions & 0 deletions src/NewTwitchApi/NewTwitchApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use NewTwitchApi\Resources\ChannelsApi;
use NewTwitchApi\Resources\ClipsApi;
use NewTwitchApi\Resources\EntitlementsApi;
use NewTwitchApi\Resources\EventSubApi;
use NewTwitchApi\Resources\GamesApi;
use NewTwitchApi\Resources\HypeTrainApi;
use NewTwitchApi\Resources\ModerationApi;
Expand All @@ -36,6 +37,7 @@ class NewTwitchApi
private $channelsApi;
private $clipsApi;
private $entitlementsApi;
private $eventSubApi;
private $gamesApi;
private $hypeTrainApi;
private $moderationApi;
Expand All @@ -61,6 +63,7 @@ public function __construct(Client $helixGuzzleClient, string $clientId, string
$this->channelsApi = new ChannelsApi($helixGuzzleClient, $requestGenerator);
$this->clipsApi = new ClipsApi($helixGuzzleClient, $requestGenerator);
$this->entitlementsApi = new EntitlementsApi($helixGuzzleClient, $requestGenerator);
$this->eventSubApi = new EventSubApi($helixGuzzleClient, $requestGenerator);
$this->gamesApi = new GamesApi($helixGuzzleClient, $requestGenerator);
$this->hypeTrainApi = new HypeTrainApi($helixGuzzleClient, $requestGenerator);
$this->moderationApi = new ModerationApi($helixGuzzleClient, $requestGenerator);
Expand Down Expand Up @@ -112,6 +115,11 @@ public function getEntitlementsApi(): EntitlementsApi
return $this->entitlementsApi;
}

public function getEventSubApi(): EventSubApi
{
return $this->eventSubApi;
}

public function getGamesApi(): GamesApi
{
return $this->gamesApi;
Expand Down
2 changes: 1 addition & 1 deletion src/NewTwitchApi/Resources/BitsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getExtensionTransactions(string $bearer, string $extensionId, ar

$queryParamsMap[] = ['key' => 'extension_id', 'value' => $extensionId];

foreach ($transactionIds as $transactionId) {
foreach ($transactionIds as $transactionId) {
$queryParamsMap[] = ['key' => 'id', 'value' => $transactionId];
}

Expand Down
Loading

0 comments on commit 391a196

Please sign in to comment.