Skip to content

Commit 9c29be0

Browse files
authored
Merge pull request #88 from brandinarsenault/channel-points-v2
Add Channel Points API
2 parents d81596b + 5eba061 commit 9c29be0

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed

spec/NewTwitchApi/NewTwitchApiSpec.php

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use NewTwitchApi\Auth\OauthApi;
77
use NewTwitchApi\Resources\AnalyticsApi;
88
use NewTwitchApi\Resources\BitsApi;
9+
use NewTwitchApi\Resources\ChannelPointsApi;
910
use NewTwitchApi\Resources\EntitlementsApi;
1011
use NewTwitchApi\Resources\GamesApi;
1112
use NewTwitchApi\Resources\StreamsApi;
@@ -37,6 +38,11 @@ function it_should_provide_bits_api()
3738
$this->getBitsApi()->shouldBeAnInstanceOf(BitsApi::class);
3839
}
3940

41+
function it_should_provide_channel_points_api()
42+
{
43+
$this->getChannelPointsApi()->shouldBeAnInstanceOf(ChannelPointsApi::class);
44+
}
45+
4046
function it_should_provide_entitlements_api()
4147
{
4248
$this->getEntitlementsApi()->shouldBeAnInstanceOf(EntitlementsApi::class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 ChannelPointsApiSpec extends ObjectBehavior
12+
{
13+
function let(Client $guzzleClient)
14+
{
15+
$this->beConstructedWith($guzzleClient);
16+
}
17+
18+
function it_should_get_custom_reward(Client $guzzleClient, Response $response)
19+
{
20+
$guzzleClient->send(new Request('GET', 'channel_points/custom_rewards?broadcaster_id=123', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
21+
$this->getCustomReward('TEST_TOKEN', '123')->shouldBeAnInstanceOf(ResponseInterface::class);
22+
}
23+
24+
function it_should_get_custom_reward_with_everything(Client $guzzleClient, Response $response)
25+
{
26+
$guzzleClient->send(new Request('GET', 'channel_points/custom_rewards?broadcaster_id=123&id=321&only_manageable_rewards=1', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
27+
$this->getCustomReward('TEST_TOKEN', '123', ['321'], true)->shouldBeAnInstanceOf(ResponseInterface::class);
28+
}
29+
30+
function it_should_get_custom_reward_redemption(Client $guzzleClient, Response $response)
31+
{
32+
$guzzleClient->send(new Request('GET', 'channel_points/custom_rewards/redemptions?broadcaster_id=123&reward_id=321', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
33+
$this->getCustomRewardRedemption('TEST_TOKEN', '123', '321')->shouldBeAnInstanceOf(ResponseInterface::class);
34+
}
35+
36+
function it_should_get_custom_reward_redemption_with_reward_everything(Client $guzzleClient, Response $response)
37+
{
38+
$guzzleClient->send(new Request('GET', 'channel_points/custom_rewards/redemptions?broadcaster_id=123&reward_id=321&status=UNFULFILLED&sort=OLDEST&after=abc&first=50', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
39+
$this->getCustomRewardRedemption('TEST_TOKEN', '123', '321', [], 'UNFULFILLED', 'OLDEST', 'abc', '50')->shouldBeAnInstanceOf(ResponseInterface::class);
40+
}
41+
42+
function it_should_get_custom_reward_redemption_with_id_everything(Client $guzzleClient, Response $response)
43+
{
44+
$guzzleClient->send(new Request('GET', 'channel_points/custom_rewards/redemptions?broadcaster_id=123&id=321&id=333&status=UNFULFILLED&sort=OLDEST&after=abc&first=50', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
45+
$this->getCustomRewardRedemption('TEST_TOKEN', '123', null, ['321', '333'], 'UNFULFILLED', 'OLDEST', 'abc', '50')->shouldBeAnInstanceOf(ResponseInterface::class);
46+
}
47+
}

src/NewTwitchApi/NewTwitchApi.php

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use NewTwitchApi\Resources\AdsApi;
1010
use NewTwitchApi\Resources\AnalyticsApi;
1111
use NewTwitchApi\Resources\BitsApi;
12+
use NewTwitchApi\Resources\ChannelPointsApi;
1213
use NewTwitchApi\Resources\ClipsApi;
1314
use NewTwitchApi\Resources\EntitlementsApi;
1415
use NewTwitchApi\Resources\GamesApi;
@@ -29,6 +30,7 @@ class NewTwitchApi
2930
private $adsApi;
3031
private $analyticsApi;
3132
private $bitsApi;
33+
private $channelPointsApi;
3234
private $clipsApi;
3335
private $entitlementsApi;
3436
private $gamesApi;
@@ -49,6 +51,7 @@ public function __construct(Client $helixGuzzleClient, string $clientId, string
4951
$this->adsApi = new AdsApi($helixGuzzleClient);
5052
$this->analyticsApi = new AnalyticsApi($helixGuzzleClient);
5153
$this->bitsApi = new BitsApi($helixGuzzleClient);
54+
$this->channelPointsApi = new ChannelPointsApi($helixGuzzleClient);
5255
$this->clipsApi = new ClipsApi($helixGuzzleClient);
5356
$this->entitlementsApi = new EntitlementsApi($helixGuzzleClient);
5457
$this->gamesApi = new GamesApi($helixGuzzleClient);
@@ -84,6 +87,11 @@ public function getBitsApi(): BitsApi
8487
return $this->bitsApi;
8588
}
8689

90+
public function getChannelPointsApi(): ChannelPointsApi
91+
{
92+
return $this->channelPointsApi;
93+
}
94+
8795
public function getClipsApi(): ClipsApi
8896
{
8997
return $this->clipsApi;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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 ChannelPointsApi extends AbstractResource
11+
{
12+
/**
13+
* @throws GuzzleException
14+
*/
15+
public function getCustomRewardById(string $bearer, string $broadcasterId, string $id, bool $onlyManageableRewards = null): ResponseInterface
16+
{
17+
return $this->getCustomReward($bearer, $broadcasterId, [$id], $onlyManageableRewards);
18+
}
19+
20+
/**
21+
* @throws GuzzleException
22+
* @link https://dev.twitch.tv/docs/api/reference#get-custom-reward
23+
*/
24+
public function getCustomReward(string $bearer, string $broadcasterId, array $ids = [], bool $onlyManageableRewards = null): ResponseInterface
25+
{
26+
$queryParamsMap = [];
27+
28+
$queryParamsMap[] = ['key' => 'broadcaster_id', 'value' => $broadcasterId];
29+
30+
foreach ($ids as $id) {
31+
$queryParamsMap[] = ['key' => 'id', 'value' => $id];
32+
}
33+
34+
if ($onlyManageableRewards) {
35+
$queryParamsMap[] = ['key' => 'only_manageable_rewards', 'value' => $onlyManageableRewards];
36+
}
37+
38+
return $this->callApi('channel_points/custom_rewards', $bearer, $queryParamsMap);
39+
}
40+
41+
/**
42+
* @throws GuzzleException
43+
* @link https://dev.twitch.tv/docs/api/reference#get-custom-reward-redemption
44+
*/
45+
public function getCustomRewardRedemption(string $bearer, string $broadcasterId, string $rewardId = null, array $ids = [], string $status = null, string $sort = null, string $after = null, string $first = null): ResponseInterface
46+
{
47+
$queryParamsMap = [];
48+
49+
$queryParamsMap[] = ['key' => 'broadcaster_id', 'value' => $broadcasterId];
50+
51+
if ($rewardId) {
52+
$queryParamsMap[] = ['key' => 'reward_id', 'value' => $rewardId];
53+
}
54+
55+
foreach ($ids as $id) {
56+
$queryParamsMap[] = ['key' => 'id', 'value' => $id];
57+
}
58+
59+
if ($status) {
60+
$queryParamsMap[] = ['key' => 'status', 'value' => $status];
61+
}
62+
63+
if ($sort) {
64+
$queryParamsMap[] = ['key' => 'sort', 'value' => $sort];
65+
}
66+
67+
if ($after) {
68+
$queryParamsMap[] = ['key' => 'after', 'value' => $after];
69+
}
70+
71+
if ($first) {
72+
$queryParamsMap[] = ['key' => 'first', 'value' => $first];
73+
}
74+
75+
return $this->callApi('channel_points/custom_rewards/redemptions', $bearer, $queryParamsMap);
76+
}
77+
78+
/**
79+
* @throws GuzzleException
80+
* @link https://dev.twitch.tv/docs/api/reference#get-bits-leaderboard
81+
*/
82+
public function getBitsLeaderboard(string $bearer, int $count = null, string $period = null, string $startedAt = null, string $userId = null): ResponseInterface
83+
{
84+
$queryParamsMap = [];
85+
86+
if ($count) {
87+
$queryParamsMap[] = ['key' => 'count', 'value' => $count];
88+
}
89+
90+
if ($period) {
91+
$queryParamsMap[] = ['key' => 'period', 'value' => $period];
92+
}
93+
94+
if ($startedAt) {
95+
$queryParamsMap[] = ['key' => 'started_at', 'value' => $startedAt];
96+
}
97+
98+
if ($userId) {
99+
$queryParamsMap[] = ['key' => 'user_id', 'value' => $userId];
100+
}
101+
102+
return $this->callApi('bits/leaderboard', $bearer, $queryParamsMap);
103+
}
104+
105+
/**
106+
* @throws GuzzleException
107+
* @link https://dev.twitch.tv/docs/api/reference#get-extension-transactions
108+
*/
109+
public function getExtensionTransactions(string $bearer, string $extensionId, array $transactionIds = [], int $first = null, string $after = null): ResponseInterface
110+
{
111+
$queryParamsMap = [];
112+
113+
$queryParamsMap[] = ['key' => 'extension_id', 'value' => $extensionId];
114+
115+
foreach ($transactionIds as $transactionId) {
116+
$queryParamsMap[] = ['key' => 'id', 'value' => $transactionId];
117+
}
118+
119+
if ($first) {
120+
$queryParamsMap[] = ['key' => 'first', 'value' => $first];
121+
}
122+
123+
if ($after) {
124+
$queryParamsMap[] = ['key' => 'after', 'value' => $after];
125+
}
126+
127+
return $this->callApi('extensions/transactions', $bearer, $queryParamsMap);
128+
}
129+
}

0 commit comments

Comments
 (0)