Skip to content

Commit

Permalink
Merge pull request #115 from brandinarsenault/predictions_polls
Browse files Browse the repository at this point in the history
Add Polls and Predictions Classes & GET Endpoints
  • Loading branch information
Brandin authored May 4, 2021
2 parents 6cf9714 + 992ab72 commit 51e01c1
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 0 deletions.
12 changes: 12 additions & 0 deletions spec/NewTwitchApi/NewTwitchApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use NewTwitchApi\Resources\EntitlementsApi;
use NewTwitchApi\Resources\GamesApi;
use NewTwitchApi\Resources\ModerationApi;
use NewTwitchApi\Resources\PollsApi;
use NewTwitchApi\Resources\PredictionsApi;
use NewTwitchApi\Resources\StreamsApi;
use NewTwitchApi\Resources\SubscriptionsApi;
use NewTwitchApi\Resources\TagsApi;
Expand Down Expand Up @@ -63,6 +65,16 @@ function it_should_provide_games_api()
$this->getGamesApi()->shouldBeAnInstanceOf(GamesApi::class);
}

function it_should_provide_polls_api()
{
$this->getPollsApi()->shouldBeAnInstanceOf(PollsApi::class);
}

function it_should_provide_predictions_api()
{
$this->getPredictionsApi()->shouldBeAnInstanceOf(PredictionsApi::class);
}

function it_should_provide_subscriptions_api()
{
$this->getSubscriptionsApi()->shouldBeAnInstanceOf(SubscriptionsApi::class);
Expand Down
41 changes: 41 additions & 0 deletions spec/NewTwitchApi/Resources/PollsApiSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace spec\NewTwitchApi\Resources;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\ResponseInterface;

class PollsApiSpec extends ObjectBehavior
{
function let(Client $guzzleClient)
{
$this->beConstructedWith($guzzleClient);
}

function it_should_get_polls(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'polls?broadcaster_id=12345', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getPolls('TEST_TOKEN', '12345')->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_polls_by_id(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'polls?broadcaster_id=12345&id=123', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getPolls('TEST_TOKEN', '12345', ['123'])->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_polls_by_ids(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'polls?broadcaster_id=12345&id=123&id=321', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getPolls('TEST_TOKEN', '12345', ['123', '321'])->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_polls_with_everything(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'polls?broadcaster_id=12345&id=123&id=321&after=abc&first=20', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getPolls('TEST_TOKEN', '12345', ['123', '321'], 'abc', 20)->shouldBeAnInstanceOf(ResponseInterface::class);
}
}
41 changes: 41 additions & 0 deletions spec/NewTwitchApi/Resources/PredictionsApiSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace spec\NewTwitchApi\Resources;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\ResponseInterface;

class PredictionsApiSpec extends ObjectBehavior
{
function let(Client $guzzleClient)
{
$this->beConstructedWith($guzzleClient);
}

function it_should_get_predictions(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'predictions?broadcaster_id=12345', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getPredictions('TEST_TOKEN', '12345')->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_predictions_by_id(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'predictions?broadcaster_id=12345&id=123', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getPredictions('TEST_TOKEN', '12345', ['123'])->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_predictions_by_ids(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'predictions?broadcaster_id=12345&id=123&id=321', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getPredictions('TEST_TOKEN', '12345', ['123', '321'])->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_predictions_with_everything(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'predictions?broadcaster_id=12345&id=123&id=321&after=abc&first=20', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getPredictions('TEST_TOKEN', '12345', ['123', '321'], 'abc', 20)->shouldBeAnInstanceOf(ResponseInterface::class);
}
}
16 changes: 16 additions & 0 deletions src/NewTwitchApi/NewTwitchApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use NewTwitchApi\Resources\GamesApi;
use NewTwitchApi\Resources\HypeTrainApi;
use NewTwitchApi\Resources\ModerationApi;
use NewTwitchApi\Resources\PollsApi;
use NewTwitchApi\Resources\PredictionsApi;
use NewTwitchApi\Resources\SearchApi;
use NewTwitchApi\Resources\StreamsApi;
use NewTwitchApi\Resources\SubscriptionsApi;
Expand All @@ -37,6 +39,8 @@ class NewTwitchApi
private $gamesApi;
private $hypeTrainApi;
private $moderationApi;
private $pollsApi;
private $predictionsApi;
private $searchApi;
private $streamsApi;
private $subscriptionsApi;
Expand All @@ -59,6 +63,8 @@ public function __construct(Client $helixGuzzleClient, string $clientId, string
$this->gamesApi = new GamesApi($helixGuzzleClient);
$this->hypeTrainApi = new HypeTrainApi($helixGuzzleClient);
$this->moderationApi = new ModerationApi($helixGuzzleClient);
$this->pollsApi = new PollsApi($helixGuzzleClient);
$this->predictionsApi = new PredictionsApi($helixGuzzleClient);
$this->searchApi = new SearchApi($helixGuzzleClient);
$this->streamsApi = new StreamsApi($helixGuzzleClient);
$this->subscriptionsApi = new SubscriptionsApi($helixGuzzleClient);
Expand Down Expand Up @@ -120,6 +126,16 @@ public function getModerationApi(): ModerationApi
return $this->moderationApi;
}

public function getPollsApi(): PollsApi
{
return $this->pollsApi;
}

public function getPredictionsApi(): PredictionsApi
{
return $this->predictionsApi;
}

public function getSearchApi(): SearchApi
{
return $this->searchApi;
Expand Down
36 changes: 36 additions & 0 deletions src/NewTwitchApi/Resources/PollsApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace NewTwitchApi\Resources;

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

class PollsApi extends AbstractResource
{
/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#get-polls
*/
public function getPolls(string $bearer, string $broadcasterId, array $ids = [], string $after = null, int $first = null): ResponseInterface
{
$queryParamsMap = [];

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

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

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

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

return $this->getApi('polls', $bearer, $queryParamsMap);
}
}
36 changes: 36 additions & 0 deletions src/NewTwitchApi/Resources/PredictionsApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace NewTwitchApi\Resources;

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

class PredictionsApi extends AbstractResource
{
/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#get-predictions
*/
public function getPredictions(string $bearer, string $broadcasterId, array $ids = [], string $after = null, int $first = null): ResponseInterface
{
$queryParamsMap = [];

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

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

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

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

return $this->getApi('predictions', $bearer, $queryParamsMap);
}
}

0 comments on commit 51e01c1

Please sign in to comment.