Skip to content

Commit

Permalink
Merge pull request #88 from brandinarsenault/channel-points-v2
Browse files Browse the repository at this point in the history
Add Channel Points API
  • Loading branch information
echosa committed Dec 7, 2020
2 parents d81596b + 5eba061 commit 9c29be0
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 0 deletions.
6 changes: 6 additions & 0 deletions spec/NewTwitchApi/NewTwitchApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use NewTwitchApi\Auth\OauthApi;
use NewTwitchApi\Resources\AnalyticsApi;
use NewTwitchApi\Resources\BitsApi;
use NewTwitchApi\Resources\ChannelPointsApi;
use NewTwitchApi\Resources\EntitlementsApi;
use NewTwitchApi\Resources\GamesApi;
use NewTwitchApi\Resources\StreamsApi;
Expand Down Expand Up @@ -37,6 +38,11 @@ function it_should_provide_bits_api()
$this->getBitsApi()->shouldBeAnInstanceOf(BitsApi::class);
}

function it_should_provide_channel_points_api()
{
$this->getChannelPointsApi()->shouldBeAnInstanceOf(ChannelPointsApi::class);
}

function it_should_provide_entitlements_api()
{
$this->getEntitlementsApi()->shouldBeAnInstanceOf(EntitlementsApi::class);
Expand Down
47 changes: 47 additions & 0 deletions spec/NewTwitchApi/Resources/ChannelPointsApiSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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 ChannelPointsApiSpec extends ObjectBehavior
{
function let(Client $guzzleClient)
{
$this->beConstructedWith($guzzleClient);
}

function it_should_get_custom_reward(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'channel_points/custom_rewards?broadcaster_id=123', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getCustomReward('TEST_TOKEN', '123')->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_custom_reward_with_everything(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'channel_points/custom_rewards?broadcaster_id=123&id=321&only_manageable_rewards=1', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getCustomReward('TEST_TOKEN', '123', ['321'], true)->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_custom_reward_redemption(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'channel_points/custom_rewards/redemptions?broadcaster_id=123&reward_id=321', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getCustomRewardRedemption('TEST_TOKEN', '123', '321')->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_custom_reward_redemption_with_reward_everything(Client $guzzleClient, Response $response)
{
$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);
$this->getCustomRewardRedemption('TEST_TOKEN', '123', '321', [], 'UNFULFILLED', 'OLDEST', 'abc', '50')->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_custom_reward_redemption_with_id_everything(Client $guzzleClient, Response $response)
{
$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);
$this->getCustomRewardRedemption('TEST_TOKEN', '123', null, ['321', '333'], 'UNFULFILLED', 'OLDEST', 'abc', '50')->shouldBeAnInstanceOf(ResponseInterface::class);
}
}
8 changes: 8 additions & 0 deletions src/NewTwitchApi/NewTwitchApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use NewTwitchApi\Resources\AdsApi;
use NewTwitchApi\Resources\AnalyticsApi;
use NewTwitchApi\Resources\BitsApi;
use NewTwitchApi\Resources\ChannelPointsApi;
use NewTwitchApi\Resources\ClipsApi;
use NewTwitchApi\Resources\EntitlementsApi;
use NewTwitchApi\Resources\GamesApi;
Expand All @@ -29,6 +30,7 @@ class NewTwitchApi
private $adsApi;
private $analyticsApi;
private $bitsApi;
private $channelPointsApi;
private $clipsApi;
private $entitlementsApi;
private $gamesApi;
Expand All @@ -49,6 +51,7 @@ public function __construct(Client $helixGuzzleClient, string $clientId, string
$this->adsApi = new AdsApi($helixGuzzleClient);
$this->analyticsApi = new AnalyticsApi($helixGuzzleClient);
$this->bitsApi = new BitsApi($helixGuzzleClient);
$this->channelPointsApi = new ChannelPointsApi($helixGuzzleClient);
$this->clipsApi = new ClipsApi($helixGuzzleClient);
$this->entitlementsApi = new EntitlementsApi($helixGuzzleClient);
$this->gamesApi = new GamesApi($helixGuzzleClient);
Expand Down Expand Up @@ -84,6 +87,11 @@ public function getBitsApi(): BitsApi
return $this->bitsApi;
}

public function getChannelPointsApi(): ChannelPointsApi
{
return $this->channelPointsApi;
}

public function getClipsApi(): ClipsApi
{
return $this->clipsApi;
Expand Down
129 changes: 129 additions & 0 deletions src/NewTwitchApi/Resources/ChannelPointsApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

declare(strict_types=1);

namespace NewTwitchApi\Resources;

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

class ChannelPointsApi extends AbstractResource
{
/**
* @throws GuzzleException
*/
public function getCustomRewardById(string $bearer, string $broadcasterId, string $id, bool $onlyManageableRewards = null): ResponseInterface
{
return $this->getCustomReward($bearer, $broadcasterId, [$id], $onlyManageableRewards);
}

/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#get-custom-reward
*/
public function getCustomReward(string $bearer, string $broadcasterId, array $ids = [], bool $onlyManageableRewards = null): ResponseInterface
{
$queryParamsMap = [];

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

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

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

return $this->callApi('channel_points/custom_rewards', $bearer, $queryParamsMap);
}

/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#get-custom-reward-redemption
*/
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
{
$queryParamsMap = [];

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

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

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

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

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

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

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

return $this->callApi('channel_points/custom_rewards/redemptions', $bearer, $queryParamsMap);
}

/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#get-bits-leaderboard
*/
public function getBitsLeaderboard(string $bearer, int $count = null, string $period = null, string $startedAt = null, string $userId = null): ResponseInterface
{
$queryParamsMap = [];

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

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

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

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

return $this->callApi('bits/leaderboard', $bearer, $queryParamsMap);
}

/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#get-extension-transactions
*/
public function getExtensionTransactions(string $bearer, string $extensionId, array $transactionIds = [], int $first = null, string $after = null): ResponseInterface
{
$queryParamsMap = [];

$queryParamsMap[] = ['key' => 'extension_id', 'value' => $extensionId];

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

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

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

return $this->callApi('extensions/transactions', $bearer, $queryParamsMap);
}
}

0 comments on commit 9c29be0

Please sign in to comment.