Skip to content

Commit

Permalink
Merge pull request #102 from ThibaultVlacich/new-endpoints
Browse files Browse the repository at this point in the history
Add new endpoints (teams and check user sub)
  • Loading branch information
Brandin committed Mar 16, 2021
2 parents c7a659c + 435c487 commit 8ab26c5
Show file tree
Hide file tree
Showing 6 changed files with 157 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,7 +11,9 @@
use NewTwitchApi\Resources\GamesApi;
use NewTwitchApi\Resources\ModerationApi;
use NewTwitchApi\Resources\StreamsApi;
use NewTwitchApi\Resources\SubscriptionsApi;
use NewTwitchApi\Resources\TagsApi;
use NewTwitchApi\Resources\TeamsApi;
use NewTwitchApi\Resources\UsersApi;
use NewTwitchApi\Resources\WebhooksApi;
use NewTwitchApi\Webhooks\WebhooksSubscriptionApi;
Expand Down Expand Up @@ -54,6 +56,11 @@ function it_should_provide_games_api()
$this->getGamesApi()->shouldBeAnInstanceOf(GamesApi::class);
}

function it_should_provide_subscriptions_api()
{
$this->getSubscriptionsApi()->shouldBeAnInstanceOf(SubscriptionsApi::class);
}

function it_should_provide_streams_api()
{
$this->getStreamsApi()->shouldBeAnInstanceOf(StreamsApi::class);
Expand All @@ -64,6 +71,11 @@ function it_should_provide_tags_api()
$this->getTagsApi()->shouldBeAnInstanceOf(TagsApi::class);
}

function it_should_provide_teams_api()
{
$this->getTeamsApi()->shouldBeAnInstanceOf(TeamsApi::class);
}

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

function it_should_check_user_subscriptions(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'subscriptions/user?broadcaster_id=123&user_id=456', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->checkUserSubscription('TEST_TOKEN', '123', '456')->shouldBeAnInstanceOf(ResponseInterface::class);
}
}
41 changes: 41 additions & 0 deletions spec/NewTwitchApi/Resources/TeamsApiSpec.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 TeamsApiSpec extends ObjectBehavior
{
function let(Client $guzzleClient)
{
$this->beConstructedWith($guzzleClient);
}

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

function it_should_get_teams(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'teams', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getTeams('TEST_TOKEN')->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_teams_by_name(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'teams?name=abc', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getTeamsByName('TEST_TOKEN', 'abc')->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_teams_by_id(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'teams?id=123', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getTeamsById('TEST_TOKEN', '123')->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 @@ -18,6 +18,7 @@
use NewTwitchApi\Resources\StreamsApi;
use NewTwitchApi\Resources\SubscriptionsApi;
use NewTwitchApi\Resources\TagsApi;
use NewTwitchApi\Resources\TeamsApi;
use NewTwitchApi\Resources\UsersApi;
use NewTwitchApi\Resources\VideosApi;
use NewTwitchApi\Resources\WebhooksApi;
Expand All @@ -38,6 +39,7 @@ class NewTwitchApi
private $streamsApi;
private $subscriptionsApi;
private $tagsApi;
private $teamsApi;
private $usersApi;
private $videosApi;
private $webhooksApi;
Expand All @@ -58,6 +60,7 @@ public function __construct(Client $helixGuzzleClient, string $clientId, string
$this->streamsApi = new StreamsApi($helixGuzzleClient);
$this->subscriptionsApi = new SubscriptionsApi($helixGuzzleClient);
$this->tagsApi = new TagsApi($helixGuzzleClient);
$this->teamsApi = new TeamsApi($helixGuzzleClient);
$this->usersApi = new UsersApi($helixGuzzleClient);
$this->videosApi = new VideosApi($helixGuzzleClient);
$this->webhooksApi = new WebhooksApi($helixGuzzleClient);
Expand Down Expand Up @@ -129,6 +132,11 @@ public function getTagsApi(): TagsApi
return $this->tagsApi;
}

public function getTeamsApi(): TeamsApi
{
return $this->teamsApi;
}

public function getUsersApi(): UsersApi
{
return $this->usersApi;
Expand Down
14 changes: 14 additions & 0 deletions src/NewTwitchApi/Resources/SubscriptionsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,18 @@ public function getSubscriptionEvents(string $bearer, string $broadcasterId, str

return $this->getApi('subscriptions/events', $bearer, $queryParamsMap);
}

/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#check-user-subscription
*/
public function checkUserSubscription(string $bearer, string $broadcasterId, string $userId): ResponseInterface
{
$queryParamsMap = [];

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

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

declare(strict_types=1);

namespace NewTwitchApi\Resources;

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

class TeamsApi extends AbstractResource
{
/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#get-channel-teams
*/
public function getChannelTeams(string $bearer, string $broadcasterId): ResponseInterface
{
$queryParamsMap = [];

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

return $this->getApi('teams/channel', $bearer, $queryParamsMap);
}

/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#get-teams
*/
public function getTeams(string $bearer, string $name = null, string $id = null): ResponseInterface
{
$queryParamsMap = [];

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

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

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

/**
* @throws GuzzleException
*/
public function getTeamsByName(string $bearer, string $name): ResponseInterface
{
return $this->getTeams($bearer, $name, null);
}

/**
* @throws GuzzleException
*/
public function getTeamsById(string $bearer, string $id): ResponseInterface
{
return $this->getTeams($bearer, null, $id);
}
}

0 comments on commit 8ab26c5

Please sign in to comment.