Skip to content

Commit

Permalink
Merge pull request #94 from brandinarsenault/major-release
Browse files Browse the repository at this point in the history
Major Release: Multiple Endpoints and Code Cleanup
  • Loading branch information
Brandin authored Feb 25, 2021
2 parents 16aa4bc + d7cd94a commit 0755a8e
Show file tree
Hide file tree
Showing 23 changed files with 199 additions and 131 deletions.
3 changes: 2 additions & 1 deletion spec/NewTwitchApi/NewTwitchApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use NewTwitchApi\Resources\ChannelPointsApi;
use NewTwitchApi\Resources\EntitlementsApi;
use NewTwitchApi\Resources\GamesApi;
use NewTwitchApi\Resources\ModerationApi;
use NewTwitchApi\Resources\StreamsApi;
use NewTwitchApi\Resources\TagsApi;
use NewTwitchApi\Resources\UsersApi;
Expand Down Expand Up @@ -52,7 +53,7 @@ function it_should_provide_games_api()
{
$this->getGamesApi()->shouldBeAnInstanceOf(GamesApi::class);
}

function it_should_provide_streams_api()
{
$this->getStreamsApi()->shouldBeAnInstanceOf(StreamsApi::class);
Expand Down
6 changes: 6 additions & 0 deletions spec/NewTwitchApi/Resources/ChannelPointsApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ function it_should_get_custom_reward_redemption_with_id_everything(Client $guzzl
$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);
}

function it_should_delete_custom_reward(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('DELETE', 'channel_points/custom_rewards?broadcaster_id=123&id=321', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->deleteCustomReward('TEST_TOKEN', '123', '321')->shouldBeAnInstanceOf(ResponseInterface::class);
}
}
42 changes: 42 additions & 0 deletions spec/NewTwitchApi/Resources/EntitlementsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,46 @@ function it_should_redeem_code(Client $guzzleClient, Response $response)
$guzzleClient->send(new Request('POST', 'entitlements/code?user_id=123&code=abc', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->redeemCode('TEST_TOKEN', '123', ['abc'])->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_drop_entitlements_by_id(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'entitlements/drops?id=abc', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getDropsEntitlements('TEST_TOKEN', 'abc')->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_drop_entitlements_by_user_id(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'entitlements/drops?user_id=123', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getDropsEntitlements('TEST_TOKEN', NULL, '123')->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_drop_entitlements_by_user_id_game_id(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'entitlements/drops?user_id=123&game_id=321', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getDropsEntitlements('TEST_TOKEN', NULL, '123', '321')->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_drop_entitlements_by_game_id(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'entitlements/drops?game_id=321', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getDropsEntitlements('TEST_TOKEN', NULL, NULL, '321')->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_drop_entitlements_by_with_all(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'entitlements/drops?user_id=123&game_id=321&after=fff&first=100', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getDropsEntitlements('TEST_TOKEN', NULL, '123', '321', 'fff', 100)->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_create_entitlement_grants_upload_url(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('POST', 'entitlements/upload?manifest_id=123&type=bulk_drops_grant', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->createEntitlementGrantsUploadURL('TEST_TOKEN', '123', 'bulk_drops_grant')->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_create_entitlement_grants_upload_url_shorthand(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('POST', 'entitlements/upload?manifest_id=123&type=bulk_drops_grant', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->createEntitlementGrantsUploadURL('TEST_TOKEN', '123')->shouldBeAnInstanceOf(ResponseInterface::class);
}
}
10 changes: 5 additions & 5 deletions spec/NewTwitchApi/Resources/StreamsApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,30 @@ function it_should_get_streams_by_game_ids(Client $guzzleClient, Response $respo
function it_should_get_streams_by_languages(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'streams?language=en&language=de', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getStreams('TEST_TOKEN', [], [], [], [], ['en', 'de'])->shouldBeAnInstanceOf(ResponseInterface::class);
$this->getStreams('TEST_TOKEN', [], [], [], ['en', 'de'])->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_streams_page_by_first(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'streams?first=42', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getStreams('TEST_TOKEN', [], [], [], [], [], 42)->shouldBeAnInstanceOf(ResponseInterface::class);
$this->getStreams('TEST_TOKEN', [], [], [], [], 42)->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_streams_page_by_before(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'streams?before=42', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getStreams('TEST_TOKEN', [], [], [], [], [], null, 42)->shouldBeAnInstanceOf(ResponseInterface::class);
$this->getStreams('TEST_TOKEN', [], [], [], [], null, 42)->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_streams_page_by_after(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'streams?after=42', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getStreams('TEST_TOKEN', [], [], [], [], [], null, null, 42)->shouldBeAnInstanceOf(ResponseInterface::class);
$this->getStreams('TEST_TOKEN', [], [], [], [], null, null, 42)->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_get_streams_by_everything(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('GET', 'streams?user_id=12&user_id=34&user_login=twitchuser&user_login=anotheruser&game_id=56&game_id=78&language=en&language=de&first=100&before=200&after=300', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->getStreams('TEST_TOKEN', ['12', '34'], ['twitchuser', 'anotheruser'], ['56', '78'], [], ['en', 'de'], 100, 200, 300)->shouldBeAnInstanceOf(ResponseInterface::class);
$this->getStreams('TEST_TOKEN', ['12', '34'], ['twitchuser', 'anotheruser'], ['56', '78'], ['en', 'de'], 100, 200, 300)->shouldBeAnInstanceOf(ResponseInterface::class);
}
}
12 changes: 12 additions & 0 deletions spec/NewTwitchApi/Resources/UsersApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,16 @@ function it_should_delete_a_follow(Client $guzzleClient, Response $response)
$guzzleClient->send(new Request('DELETE', 'users/follows?from_id=123&to_id=321', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->deleteUserFollow('TEST_TOKEN', '123', '321')->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_update_user(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('PUT', 'users', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->updateUser('TEST_TOKEN')->shouldBeAnInstanceOf(ResponseInterface::class);
}

function it_should_update_user_description(Client $guzzleClient, Response $response)
{
$guzzleClient->send(new Request('PUT', 'users?description=test', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->updateUser('TEST_TOKEN', 'test')->shouldBeAnInstanceOf(ResponseInterface::class);
}
}
8 changes: 0 additions & 8 deletions src/NewTwitchApi/NewTwitchApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use GuzzleHttp\Client;
use NewTwitchApi\Auth\OauthApi;
use NewTwitchApi\Resources\AdsApi;
use NewTwitchApi\Resources\AnalyticsApi;
use NewTwitchApi\Resources\BitsApi;
use NewTwitchApi\Resources\ChannelPointsApi;
Expand All @@ -27,7 +26,6 @@
class NewTwitchApi
{
private $oauthApi;
private $adsApi;
private $analyticsApi;
private $bitsApi;
private $channelPointsApi;
Expand All @@ -48,7 +46,6 @@ class NewTwitchApi
public function __construct(Client $helixGuzzleClient, string $clientId, string $clientSecret, Client $authGuzzleClient = null)
{
$this->oauthApi = new OauthApi($clientId, $clientSecret, $authGuzzleClient);
$this->adsApi = new AdsApi($helixGuzzleClient);
$this->analyticsApi = new AnalyticsApi($helixGuzzleClient);
$this->bitsApi = new BitsApi($helixGuzzleClient);
$this->channelPointsApi = new ChannelPointsApi($helixGuzzleClient);
Expand All @@ -72,11 +69,6 @@ public function getOauthApi(): OauthApi
return $this->oauthApi;
}

public function getAdsApi(): AdsApi
{
return $this->adsApi;
}

public function getAnalyticsApi(): AnalyticsApi
{
return $this->analyticsApi;
Expand Down
48 changes: 40 additions & 8 deletions src/NewTwitchApi/Resources/AbstractResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(Client $guzzleClient)
/**
* @throws GuzzleException
*/
protected function callApi(string $uriEndpoint, string $bearer, array $queryParamsMap = [], array $bodyParams = []): ResponseInterface
protected function getApi(string $uriEndpoint, string $bearer, array $queryParamsMap = [], array $bodyParams = []): ResponseInterface
{
return $this->sendToApi('GET', $uriEndpoint, $bearer, $queryParamsMap, $bodyParams);
}
Expand All @@ -34,6 +34,14 @@ protected function deleteApi(string $uriEndpoint, string $bearer, array $queryPa
return $this->sendToApi('DELETE', $uriEndpoint, $bearer, $queryParamsMap, $bodyParams);
}

/**
* @throws GuzzleException
*/
protected function patchApi(string $uriEndpoint, string $bearer, array $queryParamsMap = [], array $bodyParams = []): ResponseInterface
{
return $this->sendToApi('PATCH', $uriEndpoint, $bearer, $queryParamsMap, $bodyParams);
}

/**
* @throws GuzzleException
*/
Expand All @@ -42,23 +50,35 @@ protected function postApi(string $uriEndpoint, string $bearer, array $queryPara
return $this->sendToApi('POST', $uriEndpoint, $bearer, $queryParamsMap, $bodyParams);
}

/**
* @throws GuzzleException
*/
protected function putApi(string $uriEndpoint, string $bearer, array $queryParamsMap = [], array $bodyParams = []): ResponseInterface
{
return $this->sendToApi('PUT', $uriEndpoint, $bearer, $queryParamsMap, $bodyParams);
}

private function sendToApi(string $httpMethod, string $uriEndpoint, string $bearer, array $queryParamsMap = [], array $bodyParams = []): ResponseInterface
{
if (count($bodyParams) > 0) {
$request = new Request(
$httpMethod,
sprintf('%s%s',
$uriEndpoint,
$this->generateQueryParams($queryParamsMap)),
sprintf(
'%s%s',
$uriEndpoint,
$this->generateQueryParams($queryParamsMap)
),
['Authorization' => sprintf('Bearer %s', $bearer), 'Accept' => 'application/json'],
json_encode($bodyParams)
$this->generateBodyParams($bodyParams)
);
} else {
$request = new Request(
$httpMethod,
sprintf('%s%s',
$uriEndpoint,
$this->generateQueryParams($queryParamsMap)),
sprintf(
'%s%s',
$uriEndpoint,
$this->generateQueryParams($queryParamsMap)
),
['Authorization' => sprintf('Bearer %s', $bearer)]
);
}
Expand Down Expand Up @@ -89,4 +109,16 @@ protected function generateQueryParams(array $queryParamsMap): string

return $queryStringParams ? '?'.substr($queryStringParams, 1) : '';
}

protected function generateBodyParams(array $bodyParamsMap): string
{
$bodyParams = [];
foreach ($bodyParamsMap as $bodyParam) {
if ($bodyParam['value'] !== null) {
$bodyParams[$bodyParam['key']] = $bodyParam['value'];
}
}

return json_encode($bodyParams);
}
}
26 changes: 0 additions & 26 deletions src/NewTwitchApi/Resources/AdsApi.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/NewTwitchApi/Resources/AnalyticsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getExtensionAnalytics(string $bearer, string $extensionId = null
$queryParamsMap[] = ['key' => 'ended_at', 'value' => $endedAt];
}

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

/**
Expand Down Expand Up @@ -76,6 +76,6 @@ public function getGameAnalytics(string $bearer, string $gameId = null, string $
$queryParamsMap[] = ['key' => 'ended_at', 'value' => $endedAt];
}

return $this->callApi('analytics/games', $bearer, $queryParamsMap);
return $this->getApi('analytics/games', $bearer, $queryParamsMap);
}
}
6 changes: 3 additions & 3 deletions src/NewTwitchApi/Resources/BitsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getCheermotes(string $bearer, string $broadcasterId = null): Res
$queryParamsMap[] = ['key' => 'broadcaster_id', 'value' => $broadcasterId];
}

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

/**
Expand All @@ -48,7 +48,7 @@ public function getBitsLeaderboard(string $bearer, int $count = null, string $pe
$queryParamsMap[] = ['key' => 'user_id', 'value' => $userId];
}

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

/**
Expand All @@ -73,6 +73,6 @@ public function getExtensionTransactions(string $bearer, string $extensionId, ar
$queryParamsMap[] = ['key' => 'after', 'value' => $after];
}

return $this->callApi('extensions/transactions', $bearer, $queryParamsMap);
return $this->getApi('extensions/transactions', $bearer, $queryParamsMap);
}
}
51 changes: 7 additions & 44 deletions src/NewTwitchApi/Resources/ChannelPointsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getCustomReward(string $bearer, string $broadcasterId, array $id
$queryParamsMap[] = ['key' => 'only_manageable_rewards', 'value' => $onlyManageableRewards];
}

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

/**
Expand Down Expand Up @@ -72,58 +72,21 @@ public function getCustomRewardRedemption(string $bearer, string $broadcasterId,
$queryParamsMap[] = ['key' => 'first', 'value' => $first];
}

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

/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#get-bits-leaderboard
* @link https://dev.twitch.tv/docs/api/reference#delete-custom-reward
*/
public function getBitsLeaderboard(string $bearer, int $count = null, string $period = null, string $startedAt = null, string $userId = null): ResponseInterface
public function deleteCustomReward(string $bearer, string $broadcasterId, string $id): 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];
}
$queryParamsMap[] = ['key' => 'broadcaster_id', 'value' => $broadcasterId];

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

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

0 comments on commit 0755a8e

Please sign in to comment.