Skip to content

Commit

Permalink
Merge pull request #148 from nicklaw5/v7.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandin authored Dec 9, 2022
2 parents fdad08a + d5087a9 commit 201c00d
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 0 deletions.
36 changes: 36 additions & 0 deletions spec/TwitchApi/Resources/CharityApiSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace spec\TwitchApi\Resources;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use TwitchApi\RequestGenerator;
use TwitchApi\HelixGuzzleClient;
use PhpSpec\ObjectBehavior;

class CharityApiSpec extends ObjectBehavior
{
function let(HelixGuzzleClient $guzzleClient, RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->beConstructedWith($guzzleClient, $requestGenerator);
$guzzleClient->send($request)->willReturn($response);
}

function it_should_get_charity_campaigns(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('GET', 'charity/campaigns', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [])->willReturn($request);
$this->getCharityCampaign('TEST_TOKEN', '123')->shouldBe($response);
}

function it_should_get_charity_campaign_donations(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('GET', 'charity/donations', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [])->willReturn($request);
$this->getCharityCampaignDonations('TEST_TOKEN', '123')->shouldBe($response);
}

function it_should_get_charity_campaign_donations_with_opts(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('GET', 'charity/donations', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'first', 'value' => 100], ['key' => 'after', 'value' => 'abc']], [])->willReturn($request);
$this->getCharityCampaignDonations('TEST_TOKEN', '123', 100, 'abc')->shouldBe($response);
}
}
12 changes: 12 additions & 0 deletions spec/TwitchApi/Resources/ChatApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,16 @@ function it_should_update_a_users_chat_color(RequestGenerator $requestGenerator,
$requestGenerator->generate('PUT', 'chat/color', 'TEST_TOKEN', [['key' => 'user_id', 'value' => '123'], ['key' => 'color', 'value' => 'red']], [])->willReturn($request);
$this->updateUserChatColor('TEST_TOKEN', '123', 'red')->shouldBe($response);
}

function it_should_get_chatters(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('GET', 'chat/chatters', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'moderator_id', 'value' => '456']], [])->willReturn($request);
$this->getChatters('TEST_TOKEN', '123', '456')->shouldBe($response);
}

function it_should_get_chatters_with_opts(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('GET', 'chat/chatters', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'moderator_id', 'value' => '456'],['key' => 'first', 'value' => 100], ['key' => 'after', 'value' => 'abc']], [])->willReturn($request);
$this->getChatters('TEST_TOKEN', '123', '456', 100, 'abc')->shouldBe($response);
}
}
36 changes: 36 additions & 0 deletions spec/TwitchApi/Resources/EventSubApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,40 @@ function it_should_subscribe_to_drop_entitelement_grant_with_opts(RequestGenerat
$this->createEventSubSubscription('drop.entitlement.grant', '1', ['organization_id' => '123', 'category_id' => '456', 'campaign_id' => '789'], $requestGenerator)->willReturn($request);
$this->subscribeToDropEntitlementGrant($this->bearer, $this->secret, $this->callback, '123', '456', '789')->shouldBe($response);
}

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

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

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

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

function it_should_subscribe_to_channel_shield_mode_begin(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.shield_mode.begin', 'beta', ['broadcaster_user_id' => '12345', 'moderator_user_id' => '54321'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelShieldModeBegin($this->bearer, $this->secret, $this->callback, '12345', '54321')->shouldBe($response);
}

function it_should_subscribe_to_channel_shield_mode_end(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$this->createEventSubSubscription('channel.shield_mode.end', 'beta', ['broadcaster_user_id' => '12345', 'moderator_user_id' => '54321'], $requestGenerator)->willReturn($request);
$this->subscribeToChannelShieldModeEnd($this->bearer, $this->secret, $this->callback, '12345', '54321')->shouldBe($response);
}
}
12 changes: 12 additions & 0 deletions spec/TwitchApi/Resources/ModerationApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,16 @@ function it_should_get_moderators_with_opts(RequestGenerator $requestGenerator,
$requestGenerator->generate('GET', 'moderation/moderators', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'user_id', 'value' => 'abc'], ['key' => 'user_id', 'value' => 'def'], ['key' => 'after', 'value' => 'abc'], ['key' => 'first', 'value' => '100']], [])->willReturn($request);
$this->getModerators('TEST_TOKEN', '123', ['abc', 'def'], 'abc', '100')->shouldBe($response);
}

function it_should_get_shield_mode_status(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('GET', 'moderation/shield_mode', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'moderator_id', 'value' => '456']], [])->willReturn($request);
$this->getShieldModeStatus('TEST_TOKEN', '123', '456')->shouldBe($response);
}

function it_should_update_shield_mode_status(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('PUT', 'moderation/shield_mode', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'moderator_id', 'value' => '456']], [['key' => 'is_active', 'value' => true]])->willReturn($request);
$this->updateShieldModeStatus('TEST_TOKEN', '123', '456', true)->shouldBe($response);
}
}
6 changes: 6 additions & 0 deletions spec/TwitchApi/TwitchApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use TwitchApi\Resources\BitsApi;
use TwitchApi\Resources\ChannelPointsApi;
use TwitchApi\Resources\ChannelsApi;
use TwitchApi\Resources\CharityApi;
use TwitchApi\Resources\ChatApi;
use TwitchApi\Resources\ClipsApi;
use TwitchApi\Resources\EntitlementsApi;
Expand Down Expand Up @@ -70,6 +71,11 @@ function it_should_provide_channels_api()
$this->getChannelsApi()->shouldBeAnInstanceOf(ChannelsApi::class);
}

function it_should_provide_charity_api()
{
$this->getCharityApi()->shouldBeAnInstanceOf(CharityApi::class);
}

function it_should_provide_chat_api()
{
$this->getChatApi()->shouldBeAnInstanceOf(ChatApi::class);
Expand Down
43 changes: 43 additions & 0 deletions src/Resources/CharityApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace TwitchApi\Resources;

use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;

class CharityApi extends AbstractResource
{
/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#get-charity-campaign
*/
public function getCharityCampaign(string $bearer, string $broadcasterId): ResponseInterface
{
$queryParamsMap = [];
$queryParamsMap[] = ['key' => 'broadcaster_id', 'value' => $broadcasterId];

return $this->getApi('charity/campaigns', $bearer, $queryParamsMap);
}

/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#get-charity-campaign-donations
*/
public function getCharityCampaignDonations(string $bearer, string $broadcasterId, int $first = null, string $after = null): ResponseInterface
{
$queryParamsMap = [];
$queryParamsMap[] = ['key' => 'broadcaster_id', 'value' => $broadcasterId];

if ($first) {
$queryParamsMap[] = ['key' => 'first', 'value' => $first];
}

if ($after) {
$queryParamsMap[] = ['key' => 'after', 'value' => $after];
}

return $this->getApi('charity/donations', $bearer, $queryParamsMap);
}
}
23 changes: 23 additions & 0 deletions src/Resources/ChatApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,27 @@ public function updateUserChatColor(string $bearer, string $userId, string $colo

return $this->putApi('chat/color', $bearer, $queryParamsMap);
}

/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#get-chatters
*/
public function getChatters(string $bearer, string $broadcasterId, string $moderatorId, int $first = null, string $after = null): ResponseInterface
{
$queryParamsMap = [];

$queryParamsMap[] = ['key' => 'broadcaster_id', 'value' => $broadcasterId];

$queryParamsMap[] = ['key' => 'moderator_id', 'value' => $moderatorId];

if ($first) {
$queryParamsMap[] = ['key' => 'first', 'value' => $first];
}

if ($after) {
$queryParamsMap[] = ['key' => 'after', 'value' => $after];
}

return $this->getApi('chat/chatters', $bearer, $queryParamsMap);
}
}
75 changes: 75 additions & 0 deletions src/Resources/EventSubApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,54 @@ public function subscribeToChannelGoalEnd(string $bearer, string $secret, string
return $this->subscribeToChannelGoal($bearer, $secret, $callback, $twitchId, 'end');
}

/**
* @link https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelcharity_campaignstart
*/
public function subscribeToChannelCharityCampaignStart(string $bearer, string $secret, string $callback, string $twitchId): ResponseInterface
{
return $this->subscribeToChannelCharityCampaign($bearer, $secret, $callback, $twitchId, 'start');
}

/**
* @link https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelcharity_campaignprogress
*/
public function subscribeToChannelCharityCampaignProgress(string $bearer, string $secret, string $callback, string $twitchId): ResponseInterface
{
return $this->subscribeToChannelCharityCampaign($bearer, $secret, $callback, $twitchId, 'progress');
}

/**
* @link https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelcharity_campaignstop
*/
public function subscribeToChannelCharityCampaignStop(string $bearer, string $secret, string $callback, string $twitchId): ResponseInterface
{
return $this->subscribeToChannelCharityCampaign($bearer, $secret, $callback, $twitchId, 'stop');
}

/**
* @link https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelcharity_campaigndonate
*/
public function subscribeToChannelCharityCampaignDonate(string $bearer, string $secret, string $callback, string $twitchId): ResponseInterface
{
return $this->subscribeToChannelCharityCampaign($bearer, $secret, $callback, $twitchId, 'donate');
}

/**
* @link https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelshield_modebegin
*/
public function subscribeToChannelShieldModeBegin(string $bearer, string $secret, string $callback, string $twitchId, string $moderatorId): ResponseInterface
{
return $this->subscribeToChannelShieldMode($bearer, $secret, $callback, $twitchId, $moderatorId, 'begin');
}

/**
* @link https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelshield_modeend
*/
public function subscribeToChannelShieldModeEnd(string $bearer, string $secret, string $callback, string $twitchId, string $moderatorId): ResponseInterface
{
return $this->subscribeToChannelShieldMode($bearer, $secret, $callback, $twitchId, $moderatorId, 'end');
}

/**
* @link https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/#dropentitlementgrant
*/
Expand Down Expand Up @@ -600,4 +648,31 @@ private function subscribeToChannelGoal(string $bearer, string $secret, string $
['broadcaster_user_id' => $twitchId],
);
}

private function subscribeToChannelCharityCampaign(string $bearer, string $secret, string $callback, string $twitchId, string $eventType): ResponseInterface
{
return $this->createEventSubSubscription(
$bearer,
$secret,
$callback,
sprintf('channel.charity_campaign.%s', $eventType),
'beta',
['broadcaster_user_id' => $twitchId],
);
}

private function subscribeToChannelShieldMode(string $bearer, string $secret, string $callback, string $twitchId, string $moderatorId, string $eventType): ResponseInterface
{
return $this->createEventSubSubscription(
$bearer,
$secret,
$callback,
sprintf('channel.shield_mode.%s', $eventType),
'beta',
[
'broadcaster_user_id' => $twitchId,
'moderator_user_id' => $moderatorId,
],
);
}
}
29 changes: 29 additions & 0 deletions src/Resources/ModerationApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,33 @@ public function removeChannelVip(string $bearer, string $broadcasterId, string $

return $this->deleteApi('channels/vips', $bearer, $queryParamsMap);
}

/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#get-shield-mode-status
*/
public function getShieldModeStatus(string $bearer, string $broadcasterId, string $moderatorId): ResponseInterface
{
$queryParamsMap = [];
$queryParamsMap[] = ['key' => 'broadcaster_id', 'value' => $broadcasterId];
$queryParamsMap[] = ['key' => 'moderator_id', 'value' => $moderatorId];

return $this->getApi('moderation/shield_mode', $bearer, $queryParamsMap);
}

/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#update-shield-mode-status
*/
public function updateShieldModeStatus(string $bearer, string $broadcasterId, string $moderatorId, bool $isActive): ResponseInterface
{
$queryParamsMap = [];
$queryParamsMap[] = ['key' => 'broadcaster_id', 'value' => $broadcasterId];
$queryParamsMap[] = ['key' => 'moderator_id', 'value' => $moderatorId];

$bodyParamsMap = [];
$bodyParamsMap[] = ['key' => 'is_active', 'value' => $isActive];

return $this->putApi('moderation/shield_mode', $bearer, $queryParamsMap, $bodyParamsMap);
}
}
8 changes: 8 additions & 0 deletions src/TwitchApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use TwitchApi\Resources\BitsApi;
use TwitchApi\Resources\ChannelPointsApi;
use TwitchApi\Resources\ChannelsApi;
use TwitchApi\Resources\CharityApi;
use TwitchApi\Resources\ChatApi;
use TwitchApi\Resources\ClipsApi;
use TwitchApi\Resources\EntitlementsApi;
Expand Down Expand Up @@ -41,6 +42,7 @@ class TwitchApi
private $bitsApi;
private $channelPointsApi;
private $channelsApi;
private $charityApi;
private $chatApi;
private $clipsApi;
private $entitlementsApi;
Expand Down Expand Up @@ -72,6 +74,7 @@ public function __construct(HelixGuzzleClient $helixGuzzleClient, string $client
$this->bitsApi = new BitsApi($helixGuzzleClient, $requestGenerator);
$this->channelPointsApi = new ChannelPointsApi($helixGuzzleClient, $requestGenerator);
$this->channelsApi = new ChannelsApi($helixGuzzleClient, $requestGenerator);
$this->charityApi = new CharityApi($helixGuzzleClient, $requestGenerator);
$this->chatApi = new ChatApi($helixGuzzleClient, $requestGenerator);
$this->clipsApi = new ClipsApi($helixGuzzleClient, $requestGenerator);
$this->entitlementsApi = new EntitlementsApi($helixGuzzleClient, $requestGenerator);
Expand Down Expand Up @@ -125,6 +128,11 @@ public function getChannelsApi(): ChannelsApi
return $this->channelsApi;
}

public function getCharityApi(): CharityApi
{
return $this->charityApi;
}

public function getChatApi(): ChatApi
{
return $this->chatApi;
Expand Down

0 comments on commit 201c00d

Please sign in to comment.