Skip to content

Commit 51e01c1

Browse files
authored
Merge pull request #115 from brandinarsenault/predictions_polls
Add Polls and Predictions Classes & GET Endpoints
2 parents 6cf9714 + 992ab72 commit 51e01c1

File tree

6 files changed

+182
-0
lines changed

6 files changed

+182
-0
lines changed

spec/NewTwitchApi/NewTwitchApiSpec.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use NewTwitchApi\Resources\EntitlementsApi;
1212
use NewTwitchApi\Resources\GamesApi;
1313
use NewTwitchApi\Resources\ModerationApi;
14+
use NewTwitchApi\Resources\PollsApi;
15+
use NewTwitchApi\Resources\PredictionsApi;
1416
use NewTwitchApi\Resources\StreamsApi;
1517
use NewTwitchApi\Resources\SubscriptionsApi;
1618
use NewTwitchApi\Resources\TagsApi;
@@ -63,6 +65,16 @@ function it_should_provide_games_api()
6365
$this->getGamesApi()->shouldBeAnInstanceOf(GamesApi::class);
6466
}
6567

68+
function it_should_provide_polls_api()
69+
{
70+
$this->getPollsApi()->shouldBeAnInstanceOf(PollsApi::class);
71+
}
72+
73+
function it_should_provide_predictions_api()
74+
{
75+
$this->getPredictionsApi()->shouldBeAnInstanceOf(PredictionsApi::class);
76+
}
77+
6678
function it_should_provide_subscriptions_api()
6779
{
6880
$this->getSubscriptionsApi()->shouldBeAnInstanceOf(SubscriptionsApi::class);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace spec\NewTwitchApi\Resources;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Psr7\Request;
7+
use GuzzleHttp\Psr7\Response;
8+
use PhpSpec\ObjectBehavior;
9+
use Psr\Http\Message\ResponseInterface;
10+
11+
class PollsApiSpec extends ObjectBehavior
12+
{
13+
function let(Client $guzzleClient)
14+
{
15+
$this->beConstructedWith($guzzleClient);
16+
}
17+
18+
function it_should_get_polls(Client $guzzleClient, Response $response)
19+
{
20+
$guzzleClient->send(new Request('GET', 'polls?broadcaster_id=12345', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
21+
$this->getPolls('TEST_TOKEN', '12345')->shouldBeAnInstanceOf(ResponseInterface::class);
22+
}
23+
24+
function it_should_get_polls_by_id(Client $guzzleClient, Response $response)
25+
{
26+
$guzzleClient->send(new Request('GET', 'polls?broadcaster_id=12345&id=123', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
27+
$this->getPolls('TEST_TOKEN', '12345', ['123'])->shouldBeAnInstanceOf(ResponseInterface::class);
28+
}
29+
30+
function it_should_get_polls_by_ids(Client $guzzleClient, Response $response)
31+
{
32+
$guzzleClient->send(new Request('GET', 'polls?broadcaster_id=12345&id=123&id=321', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
33+
$this->getPolls('TEST_TOKEN', '12345', ['123', '321'])->shouldBeAnInstanceOf(ResponseInterface::class);
34+
}
35+
36+
function it_should_get_polls_with_everything(Client $guzzleClient, Response $response)
37+
{
38+
$guzzleClient->send(new Request('GET', 'polls?broadcaster_id=12345&id=123&id=321&after=abc&first=20', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
39+
$this->getPolls('TEST_TOKEN', '12345', ['123', '321'], 'abc', 20)->shouldBeAnInstanceOf(ResponseInterface::class);
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace spec\NewTwitchApi\Resources;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Psr7\Request;
7+
use GuzzleHttp\Psr7\Response;
8+
use PhpSpec\ObjectBehavior;
9+
use Psr\Http\Message\ResponseInterface;
10+
11+
class PredictionsApiSpec extends ObjectBehavior
12+
{
13+
function let(Client $guzzleClient)
14+
{
15+
$this->beConstructedWith($guzzleClient);
16+
}
17+
18+
function it_should_get_predictions(Client $guzzleClient, Response $response)
19+
{
20+
$guzzleClient->send(new Request('GET', 'predictions?broadcaster_id=12345', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
21+
$this->getPredictions('TEST_TOKEN', '12345')->shouldBeAnInstanceOf(ResponseInterface::class);
22+
}
23+
24+
function it_should_get_predictions_by_id(Client $guzzleClient, Response $response)
25+
{
26+
$guzzleClient->send(new Request('GET', 'predictions?broadcaster_id=12345&id=123', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
27+
$this->getPredictions('TEST_TOKEN', '12345', ['123'])->shouldBeAnInstanceOf(ResponseInterface::class);
28+
}
29+
30+
function it_should_get_predictions_by_ids(Client $guzzleClient, Response $response)
31+
{
32+
$guzzleClient->send(new Request('GET', 'predictions?broadcaster_id=12345&id=123&id=321', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
33+
$this->getPredictions('TEST_TOKEN', '12345', ['123', '321'])->shouldBeAnInstanceOf(ResponseInterface::class);
34+
}
35+
36+
function it_should_get_predictions_with_everything(Client $guzzleClient, Response $response)
37+
{
38+
$guzzleClient->send(new Request('GET', 'predictions?broadcaster_id=12345&id=123&id=321&after=abc&first=20', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
39+
$this->getPredictions('TEST_TOKEN', '12345', ['123', '321'], 'abc', 20)->shouldBeAnInstanceOf(ResponseInterface::class);
40+
}
41+
}

src/NewTwitchApi/NewTwitchApi.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use NewTwitchApi\Resources\GamesApi;
1616
use NewTwitchApi\Resources\HypeTrainApi;
1717
use NewTwitchApi\Resources\ModerationApi;
18+
use NewTwitchApi\Resources\PollsApi;
19+
use NewTwitchApi\Resources\PredictionsApi;
1820
use NewTwitchApi\Resources\SearchApi;
1921
use NewTwitchApi\Resources\StreamsApi;
2022
use NewTwitchApi\Resources\SubscriptionsApi;
@@ -37,6 +39,8 @@ class NewTwitchApi
3739
private $gamesApi;
3840
private $hypeTrainApi;
3941
private $moderationApi;
42+
private $pollsApi;
43+
private $predictionsApi;
4044
private $searchApi;
4145
private $streamsApi;
4246
private $subscriptionsApi;
@@ -59,6 +63,8 @@ public function __construct(Client $helixGuzzleClient, string $clientId, string
5963
$this->gamesApi = new GamesApi($helixGuzzleClient);
6064
$this->hypeTrainApi = new HypeTrainApi($helixGuzzleClient);
6165
$this->moderationApi = new ModerationApi($helixGuzzleClient);
66+
$this->pollsApi = new PollsApi($helixGuzzleClient);
67+
$this->predictionsApi = new PredictionsApi($helixGuzzleClient);
6268
$this->searchApi = new SearchApi($helixGuzzleClient);
6369
$this->streamsApi = new StreamsApi($helixGuzzleClient);
6470
$this->subscriptionsApi = new SubscriptionsApi($helixGuzzleClient);
@@ -120,6 +126,16 @@ public function getModerationApi(): ModerationApi
120126
return $this->moderationApi;
121127
}
122128

129+
public function getPollsApi(): PollsApi
130+
{
131+
return $this->pollsApi;
132+
}
133+
134+
public function getPredictionsApi(): PredictionsApi
135+
{
136+
return $this->predictionsApi;
137+
}
138+
123139
public function getSearchApi(): SearchApi
124140
{
125141
return $this->searchApi;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NewTwitchApi\Resources;
6+
7+
use GuzzleHttp\Exception\GuzzleException;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
class PollsApi extends AbstractResource
11+
{
12+
/**
13+
* @throws GuzzleException
14+
* @link https://dev.twitch.tv/docs/api/reference#get-polls
15+
*/
16+
public function getPolls(string $bearer, string $broadcasterId, array $ids = [], string $after = null, int $first = null): ResponseInterface
17+
{
18+
$queryParamsMap = [];
19+
20+
$queryParamsMap[] = ['key' => 'broadcaster_id', 'value' => $broadcasterId];
21+
22+
foreach ($ids as $id) {
23+
$queryParamsMap[] = ['key' => 'id', 'value' => $id];
24+
}
25+
26+
if ($after) {
27+
$queryParamsMap[] = ['key' => 'after', 'value' => $after];
28+
}
29+
30+
if ($first) {
31+
$queryParamsMap[] = ['key' => 'first', 'value' => $first];
32+
}
33+
34+
return $this->getApi('polls', $bearer, $queryParamsMap);
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NewTwitchApi\Resources;
6+
7+
use GuzzleHttp\Exception\GuzzleException;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
class PredictionsApi extends AbstractResource
11+
{
12+
/**
13+
* @throws GuzzleException
14+
* @link https://dev.twitch.tv/docs/api/reference#get-predictions
15+
*/
16+
public function getPredictions(string $bearer, string $broadcasterId, array $ids = [], string $after = null, int $first = null): ResponseInterface
17+
{
18+
$queryParamsMap = [];
19+
20+
$queryParamsMap[] = ['key' => 'broadcaster_id', 'value' => $broadcasterId];
21+
22+
foreach ($ids as $id) {
23+
$queryParamsMap[] = ['key' => 'id', 'value' => $id];
24+
}
25+
26+
if ($after) {
27+
$queryParamsMap[] = ['key' => 'after', 'value' => $after];
28+
}
29+
30+
if ($first) {
31+
$queryParamsMap[] = ['key' => 'first', 'value' => $first];
32+
}
33+
34+
return $this->getApi('predictions', $bearer, $queryParamsMap);
35+
}
36+
}

0 commit comments

Comments
 (0)