Skip to content

Commit

Permalink
Merge pull request #123 from nicklaw5/v5-release
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandin authored May 28, 2021
2 parents 391a196 + 07449c3 commit 9a91bb1
Show file tree
Hide file tree
Showing 19 changed files with 441 additions and 92 deletions.
6 changes: 6 additions & 0 deletions spec/NewTwitchApi/NewTwitchApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GuzzleHttp\Client;
use NewTwitchApi\RequestGenerator;
use NewTwitchApi\Auth\OauthApi;
use NewTwitchApi\Resources\AdsApi;
use NewTwitchApi\Resources\AnalyticsApi;
use NewTwitchApi\Resources\BitsApi;
use NewTwitchApi\Resources\ChannelPointsApi;
Expand Down Expand Up @@ -37,6 +38,11 @@ function it_should_provide_oauth_api()
$this->getOauthApi()->shouldBeAnInstanceOf(OauthApi::class);
}

function it_should_provide_ads_api()
{
$this->getAdsApi()->shouldBeAnInstanceOf(AdsApi::class);
}

function it_should_provide_analytics_api()
{
$this->getAnalyticsApi()->shouldBeAnInstanceOf(AnalyticsApi::class);
Expand Down
24 changes: 24 additions & 0 deletions spec/NewTwitchApi/Resources/AdsApiSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace spec\NewTwitchApi\Resources;

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

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

function it_should_start_commercial(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('POST', 'channels/commercial', 'TEST_TOKEN', [], [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'length', 'value' => 30]])->willReturn($request);
$this->startCommercial('TEST_TOKEN', '123', 30)->shouldBe($response);
}
}
42 changes: 42 additions & 0 deletions spec/NewTwitchApi/Resources/ChannelPointsApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,51 @@ function it_should_get_custom_reward_redemption_by_ids(RequestGenerator $request
$this->getCustomRewardRedemption('TEST_TOKEN', '123', '321', ['111', '222'])->shouldBe($response);
}

function it_should_create_custom_reward(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('POST', 'channel_points/custom_rewards', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'title', 'value' => 'test 123'], ['key' => 'cost', 'value' => 100]])->willReturn($request);
$this->createCustomReward('TEST_TOKEN', '123', 'test 123', 100)->shouldBe($response);
}

function it_should_create_custom_reward_with_one_opt(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('POST', 'channel_points/custom_rewards', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'title', 'value' => 'test 123'], ['key' => 'cost', 'value' => 100], ['key' => 'prompt', 'value' => 'What is your name?']])->willReturn($request);
$this->createCustomReward('TEST_TOKEN', '123', 'test 123', 100, ['prompt' => 'What is your name?'])->shouldBe($response);
}

function it_should_create_custom_reward_with_multiple_opts(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('POST', 'channel_points/custom_rewards', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'title', 'value' => 'test 123'], ['key' => 'cost', 'value' => 100], ['key' => 'prompt', 'value' => 'What is your name?'], ['key' => 'is_enabled', 'value' => 1]])->willReturn($request);
$this->createCustomReward('TEST_TOKEN', '123', 'test 123', 100, ['prompt' => 'What is your name?', 'is_enabled' => 1])->shouldBe($response);
}

function it_should_update_custom_reward(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('PATCH', 'channel_points/custom_rewards', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'id', 'value' => '321']], [])->willReturn($request);
$this->updateCustomReward('TEST_TOKEN', '123', '321')->shouldBe($response);
}

function it_should_update_custom_reward_with_one_opt(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('PATCH', 'channel_points/custom_rewards', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'id', 'value' => '321']], [['key' => 'prompt', 'value' => 'What is your name?']])->willReturn($request);
$this->updateCustomReward('TEST_TOKEN', '123', '321', ['prompt' => 'What is your name?'])->shouldBe($response);
}

function it_should_update_custom_reward_with_multiple_opts(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('PATCH', 'channel_points/custom_rewards', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'id', 'value' => '321']], [['key' => 'prompt', 'value' => 'What is your name?'], ['key' => 'is_enabled', 'value' => 1]])->willReturn($request);
$this->updateCustomReward('TEST_TOKEN', '123', '321', ['prompt' => 'What is your name?', 'is_enabled' => 1])->shouldBe($response);
}

function it_should_delete_custom_reward(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('DELETE', 'channel_points/custom_rewards', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'id', 'value' => '321']], [])->willReturn($request);
$this->deleteCustomReward('TEST_TOKEN', '123', '321')->shouldBe($response);
}

function it_should_update_redemption_status(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('PATCH', 'channel_points/custom_rewards/redemptions', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'reward_id', 'value' => '456'], ['key' => 'id', 'value' => '789']], [['key' => 'status', 'value' => 'FULFILLED']])->willReturn($request);
$this->updateRedemptionStatus('TEST_TOKEN', '123', '456', '789', 'FULFILLED')->shouldBe($response);
}
}
30 changes: 30 additions & 0 deletions spec/NewTwitchApi/Resources/ChannelsApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,34 @@ function it_should_get_channel_editors(RequestGenerator $requestGenerator, Reque
$requestGenerator->generate('GET', 'channels/editors', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [])->willReturn($request);
$this->getChannelEditors('TEST_TOKEN', '123')->shouldBe($response);
}

function it_should_modify_channel_with_game_id(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('PATCH', 'channels', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'game_id', 'value' => '0']])->willReturn($request);
$this->modifyChannelInfo('TEST_TOKEN', '123', ['game_id' => '0'])->shouldBe($response);
}

function it_should_modify_channel_with_language(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('PATCH', 'channels', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'broadcaster_language', 'value' => 'en']])->willReturn($request);
$this->modifyChannelInfo('TEST_TOKEN', '123', ['broadcaster_language' => 'en'])->shouldBe($response);
}

function it_should_modify_channel_with_title(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('PATCH', 'channels', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'title', 'value' => 'test 123']])->willReturn($request);
$this->modifyChannelInfo('TEST_TOKEN', '123', ['title' => 'test 123'])->shouldBe($response);
}

function it_should_modify_channel_with_delay(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('PATCH', 'channels', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'delay', 'value' => 5]])->willReturn($request);
$this->modifyChannelInfo('TEST_TOKEN', '123', ['delay' => 5])->shouldBe($response);
}

function it_should_modify_channel_with_opts(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('PATCH', 'channels', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'game_id', 'value' => '0'], ['key' => 'broadcaster_language', 'value' => 'en'], ['key' => 'title', 'value' => 'test 123'], ['key' => 'delay', 'value' => 5]])->willReturn($request);
$this->modifyChannelInfo('TEST_TOKEN', '123', ['game_id' => '0', 'broadcaster_language' => 'en', 'title' => 'test 123', 'delay' => 5])->shouldBe($response);
}
}
30 changes: 30 additions & 0 deletions spec/NewTwitchApi/Resources/ModerationApiSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace spec\NewTwitchApi\Resources;

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

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

function it_should_check_automod_status(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('POST', 'moderation/enforcements/status', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'msg_id', 'value' => '456'], ['key' => 'msg_text', 'value' => 'test 123'], ['key' => 'user_id', 'value' => '789']])->willReturn($request);
$this->checkAutoModStatus('TEST_TOKEN', '123', '456', 'test 123', '789')->shouldBe($response);
}

function it_should_release_held_message(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('POST', 'moderation/automod/message', 'TEST_TOKEN', [], [['key' => 'user_id', 'value' => '123'], ['key' => 'msg_id', 'value' => '456'], ['key' => 'action', 'value' => 'ALLOW']])->willReturn($request);
$this->manageHeldAutoModMessage('TEST_TOKEN', '123', '456', 'ALLOW')->shouldBe($response);
}
}
18 changes: 18 additions & 0 deletions spec/NewTwitchApi/Resources/PollsApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,22 @@ function it_should_get_polls_with_opts(RequestGenerator $requestGenerator, Reque
$requestGenerator->generate('GET', 'polls', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'after', 'value' => 'abc'], ['key' => 'first', 'value' => 100]], [])->willReturn($request);
$this->getPolls('TEST_TOKEN', '123', [], 'abc', 100)->shouldBe($response);
}

function it_should_create_a_poll(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('POST', 'polls', 'TEST_TOKEN', [], [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'title', 'value' => 'What is my name?'], ['key' => 'choices', 'value' => [['title' => 'John'], ['title' => 'Doe']]], ['key' => 'duration', 'value' => 15]])->willReturn($request);
$this->createPoll('TEST_TOKEN', '123', 'What is my name?', [['title' => 'John'], ['title' => 'Doe']], 15)->shouldBe($response);
}

function it_should_create_a_poll_with_opts(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('POST', 'polls', 'TEST_TOKEN', [], [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'title', 'value' => 'What is my name?'], ['key' => 'choices', 'value' => [['title' => 'John'], ['title' => 'Doe']]], ['key' => 'duration', 'value' => 15], ['key' => 'bits_voting_enabled', 'value' => 1]])->willReturn($request);
$this->createPoll('TEST_TOKEN', '123', 'What is my name?', [['title' => 'John'], ['title' => 'Doe']], 15, ['bits_voting_enabled' => 1])->shouldBe($response);
}

function it_should_end_a_poll(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('PATCH', 'polls', 'TEST_TOKEN', [], [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'id', 'value' => '456'], ['key' => 'status', 'value' => 'TERMINATED']])->willReturn($request);
$this->endPoll('TEST_TOKEN', '123', '456', 'TERMINATED')->shouldBe($response);
}
}
18 changes: 18 additions & 0 deletions spec/NewTwitchApi/Resources/PredictionsApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,22 @@ function it_should_get_predictions_with_opts(RequestGenerator $requestGenerator,
$requestGenerator->generate('GET', 'predictions', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'after', 'value' => 'abc'], ['key' => 'first', 'value' => 100]], [])->willReturn($request);
$this->getPredictions('TEST_TOKEN', '123', [], 'abc', 100)->shouldBe($response);
}

function it_should_create_a_prediction(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('POST', 'predictions', 'TEST_TOKEN', [], [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'title', 'value' => 'Will the coin land on heads or tails?'], ['key' => 'outcomes', 'value' => [['title' => 'Heads'], ['title' => 'Tails']]], ['key' => 'prediction_window', 'value' => 15]])->willReturn($request);
$this->createPrediction('TEST_TOKEN', '123', 'Will the coin land on heads or tails?', [['title' => 'Heads'], ['title' => 'Tails']], 15)->shouldBe($response);
}

function it_should_end_a_prediction(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('PATCH', 'predictions', 'TEST_TOKEN', [], [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'id', 'value' => '456'], ['key' => 'status', 'value' => 'CANCELLED']])->willReturn($request);
$this->endPrediction('TEST_TOKEN', '123', '456', 'CANCELLED')->shouldBe($response);
}

function it_should_resolve_a_prediction(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('PATCH', 'predictions', 'TEST_TOKEN', [], [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'id', 'value' => '456'], ['key' => 'status', 'value' => 'RESOLVED'], ['key' => 'winning_outcome_id', 'value' => '1']])->willReturn($request);
$this->endPrediction('TEST_TOKEN', '123', '456', 'RESOLVED', '1')->shouldBe($response);
}
}
12 changes: 12 additions & 0 deletions spec/NewTwitchApi/Resources/StreamsApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,16 @@ function it_should_get_followed_streams_with_opts(RequestGenerator $requestGener
$requestGenerator->generate('GET', 'streams/followed', 'TEST_TOKEN', [['key' => 'user_id', 'value' => '123'], ['key' => 'first', 'value' => 100], ['key' => 'after', 'value' => 'abc']], [])->willReturn($request);
$this->getFollowedStreams('TEST_TOKEN', '123', 100, 'abc')->shouldBe($response);
}

function it_should_create_stream_marker(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('POST', 'streams/markers', 'TEST_TOKEN', [], [['key' => 'user_id', 'value' => '123']])->willReturn($request);
$this->createStreamMarker('TEST_TOKEN', '123')->shouldBe($response);
}

function it_should_create_stream_marker_with_description(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('POST', 'streams/markers', 'TEST_TOKEN', [], [['key' => 'user_id', 'value' => '123'], ['key' => 'description', 'value' => 'This is a marker']])->willReturn($request);
$this->createStreamMarker('TEST_TOKEN', '123', 'This is a marker')->shouldBe($response);
}
}
18 changes: 18 additions & 0 deletions spec/NewTwitchApi/Resources/TagsApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,22 @@ function it_should_get_stream_tags(RequestGenerator $requestGenerator, Request $
$requestGenerator->generate('GET', 'streams/tags', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [])->willReturn($request);
$this->getStreamTags('TEST_TOKEN', '123')->shouldBe($response);
}

function it_should_replace_stream_tags(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('PUT', 'streams/tags', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [])->willReturn($request);
$this->replaceStreamTags('TEST_TOKEN', '123')->shouldBe($response);
}

function it_should_replace_stream_tags_with_one_tag(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('PUT', 'streams/tags', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'tag_ids', 'value' => ['456']]])->willReturn($request);
$this->replaceStreamTags('TEST_TOKEN', '123', ['456'])->shouldBe($response);
}

function it_should_replace_stream_tags_with_multiple_tags(RequestGenerator $requestGenerator, Request $request, Response $response)
{
$requestGenerator->generate('PUT', 'streams/tags', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'tag_ids', 'value' => ['456', '789']]])->willReturn($request);
$this->replaceStreamTags('TEST_TOKEN', '123', ['456', '789'])->shouldBe($response);
}
}
8 changes: 8 additions & 0 deletions src/NewTwitchApi/NewTwitchApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use GuzzleHttp\Client;
use NewTwitchApi\Auth\OauthApi;
use NewTwitchApi\Resources\AdsApi;
use NewTwitchApi\Resources\AnalyticsApi;
use NewTwitchApi\Resources\BitsApi;
use NewTwitchApi\Resources\ChannelPointsApi;
Expand All @@ -31,6 +32,7 @@
class NewTwitchApi
{
private $oauthApi;
private $adsApi;
private $analyticsApi;
private $bitsApi;
private $channelPointsApi;
Expand All @@ -57,6 +59,7 @@ public function __construct(Client $helixGuzzleClient, string $clientId, string
{
$requestGenerator = new RequestGenerator();
$this->oauthApi = new OauthApi($clientId, $clientSecret, $authGuzzleClient);
$this->adsApi = new AdsApi($helixGuzzleClient, $requestGenerator);
$this->analyticsApi = new AnalyticsApi($helixGuzzleClient, $requestGenerator);
$this->bitsApi = new BitsApi($helixGuzzleClient, $requestGenerator);
$this->channelPointsApi = new ChannelPointsApi($helixGuzzleClient, $requestGenerator);
Expand Down Expand Up @@ -85,6 +88,11 @@ public function getOauthApi(): OauthApi
return $this->oauthApi;
}

public function getAdsApi(): AdsApi
{
return $this->adsApi;
}

public function getAnalyticsApi(): AnalyticsApi
{
return $this->analyticsApi;
Expand Down
25 changes: 25 additions & 0 deletions src/NewTwitchApi/Resources/AdsApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace NewTwitchApi\Resources;

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

class AdsApi extends AbstractResource
{
/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#start-commercial
*/
public function startCommercial(string $bearer, string $broadcasterId, int $length): ResponseInterface
{
$bodyParamsMap = [];

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

return $this->postApi('channels/commercial', $bearer, [], $bodyParamsMap);
}
}
Loading

0 comments on commit 9a91bb1

Please sign in to comment.