Skip to content

Commit 0755a8e

Browse files
authored
Merge pull request #94 from brandinarsenault/major-release
Major Release: Multiple Endpoints and Code Cleanup
2 parents 16aa4bc + d7cd94a commit 0755a8e

23 files changed

+199
-131
lines changed

spec/NewTwitchApi/NewTwitchApiSpec.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use NewTwitchApi\Resources\ChannelPointsApi;
1010
use NewTwitchApi\Resources\EntitlementsApi;
1111
use NewTwitchApi\Resources\GamesApi;
12+
use NewTwitchApi\Resources\ModerationApi;
1213
use NewTwitchApi\Resources\StreamsApi;
1314
use NewTwitchApi\Resources\TagsApi;
1415
use NewTwitchApi\Resources\UsersApi;
@@ -52,7 +53,7 @@ function it_should_provide_games_api()
5253
{
5354
$this->getGamesApi()->shouldBeAnInstanceOf(GamesApi::class);
5455
}
55-
56+
5657
function it_should_provide_streams_api()
5758
{
5859
$this->getStreamsApi()->shouldBeAnInstanceOf(StreamsApi::class);

spec/NewTwitchApi/Resources/ChannelPointsApiSpec.php

+6
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,10 @@ function it_should_get_custom_reward_redemption_with_id_everything(Client $guzzl
4444
$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);
4545
$this->getCustomRewardRedemption('TEST_TOKEN', '123', null, ['321', '333'], 'UNFULFILLED', 'OLDEST', 'abc', '50')->shouldBeAnInstanceOf(ResponseInterface::class);
4646
}
47+
48+
function it_should_delete_custom_reward(Client $guzzleClient, Response $response)
49+
{
50+
$guzzleClient->send(new Request('DELETE', 'channel_points/custom_rewards?broadcaster_id=123&id=321', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
51+
$this->deleteCustomReward('TEST_TOKEN', '123', '321')->shouldBeAnInstanceOf(ResponseInterface::class);
52+
}
4753
}

spec/NewTwitchApi/Resources/EntitlementsApi.php

+42
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,46 @@ function it_should_redeem_code(Client $guzzleClient, Response $response)
2626
$guzzleClient->send(new Request('POST', 'entitlements/code?user_id=123&code=abc', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
2727
$this->redeemCode('TEST_TOKEN', '123', ['abc'])->shouldBeAnInstanceOf(ResponseInterface::class);
2828
}
29+
30+
function it_should_get_drop_entitlements_by_id(Client $guzzleClient, Response $response)
31+
{
32+
$guzzleClient->send(new Request('GET', 'entitlements/drops?id=abc', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
33+
$this->getDropsEntitlements('TEST_TOKEN', 'abc')->shouldBeAnInstanceOf(ResponseInterface::class);
34+
}
35+
36+
function it_should_get_drop_entitlements_by_user_id(Client $guzzleClient, Response $response)
37+
{
38+
$guzzleClient->send(new Request('GET', 'entitlements/drops?user_id=123', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
39+
$this->getDropsEntitlements('TEST_TOKEN', NULL, '123')->shouldBeAnInstanceOf(ResponseInterface::class);
40+
}
41+
42+
function it_should_get_drop_entitlements_by_user_id_game_id(Client $guzzleClient, Response $response)
43+
{
44+
$guzzleClient->send(new Request('GET', 'entitlements/drops?user_id=123&game_id=321', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
45+
$this->getDropsEntitlements('TEST_TOKEN', NULL, '123', '321')->shouldBeAnInstanceOf(ResponseInterface::class);
46+
}
47+
48+
function it_should_get_drop_entitlements_by_game_id(Client $guzzleClient, Response $response)
49+
{
50+
$guzzleClient->send(new Request('GET', 'entitlements/drops?game_id=321', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
51+
$this->getDropsEntitlements('TEST_TOKEN', NULL, NULL, '321')->shouldBeAnInstanceOf(ResponseInterface::class);
52+
}
53+
54+
function it_should_get_drop_entitlements_by_with_all(Client $guzzleClient, Response $response)
55+
{
56+
$guzzleClient->send(new Request('GET', 'entitlements/drops?user_id=123&game_id=321&after=fff&first=100', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
57+
$this->getDropsEntitlements('TEST_TOKEN', NULL, '123', '321', 'fff', 100)->shouldBeAnInstanceOf(ResponseInterface::class);
58+
}
59+
60+
function it_should_create_entitlement_grants_upload_url(Client $guzzleClient, Response $response)
61+
{
62+
$guzzleClient->send(new Request('POST', 'entitlements/upload?manifest_id=123&type=bulk_drops_grant', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
63+
$this->createEntitlementGrantsUploadURL('TEST_TOKEN', '123', 'bulk_drops_grant')->shouldBeAnInstanceOf(ResponseInterface::class);
64+
}
65+
66+
function it_should_create_entitlement_grants_upload_url_shorthand(Client $guzzleClient, Response $response)
67+
{
68+
$guzzleClient->send(new Request('POST', 'entitlements/upload?manifest_id=123&type=bulk_drops_grant', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
69+
$this->createEntitlementGrantsUploadURL('TEST_TOKEN', '123')->shouldBeAnInstanceOf(ResponseInterface::class);
70+
}
2971
}

spec/NewTwitchApi/Resources/StreamsApiSpec.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,30 @@ function it_should_get_streams_by_game_ids(Client $guzzleClient, Response $respo
6666
function it_should_get_streams_by_languages(Client $guzzleClient, Response $response)
6767
{
6868
$guzzleClient->send(new Request('GET', 'streams?language=en&language=de', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
69-
$this->getStreams('TEST_TOKEN', [], [], [], [], ['en', 'de'])->shouldBeAnInstanceOf(ResponseInterface::class);
69+
$this->getStreams('TEST_TOKEN', [], [], [], ['en', 'de'])->shouldBeAnInstanceOf(ResponseInterface::class);
7070
}
7171

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

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

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

9090
function it_should_get_streams_by_everything(Client $guzzleClient, Response $response)
9191
{
9292
$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);
93-
$this->getStreams('TEST_TOKEN', ['12', '34'], ['twitchuser', 'anotheruser'], ['56', '78'], [], ['en', 'de'], 100, 200, 300)->shouldBeAnInstanceOf(ResponseInterface::class);
93+
$this->getStreams('TEST_TOKEN', ['12', '34'], ['twitchuser', 'anotheruser'], ['56', '78'], ['en', 'de'], 100, 200, 300)->shouldBeAnInstanceOf(ResponseInterface::class);
9494
}
9595
}

spec/NewTwitchApi/Resources/UsersApiSpec.php

+12
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,16 @@ function it_should_delete_a_follow(Client $guzzleClient, Response $response)
128128
$guzzleClient->send(new Request('DELETE', 'users/follows?from_id=123&to_id=321', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
129129
$this->deleteUserFollow('TEST_TOKEN', '123', '321')->shouldBeAnInstanceOf(ResponseInterface::class);
130130
}
131+
132+
function it_should_update_user(Client $guzzleClient, Response $response)
133+
{
134+
$guzzleClient->send(new Request('PUT', 'users', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
135+
$this->updateUser('TEST_TOKEN')->shouldBeAnInstanceOf(ResponseInterface::class);
136+
}
137+
138+
function it_should_update_user_description(Client $guzzleClient, Response $response)
139+
{
140+
$guzzleClient->send(new Request('PUT', 'users?description=test', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
141+
$this->updateUser('TEST_TOKEN', 'test')->shouldBeAnInstanceOf(ResponseInterface::class);
142+
}
131143
}

src/NewTwitchApi/NewTwitchApi.php

-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use GuzzleHttp\Client;
88
use NewTwitchApi\Auth\OauthApi;
9-
use NewTwitchApi\Resources\AdsApi;
109
use NewTwitchApi\Resources\AnalyticsApi;
1110
use NewTwitchApi\Resources\BitsApi;
1211
use NewTwitchApi\Resources\ChannelPointsApi;
@@ -27,7 +26,6 @@
2726
class NewTwitchApi
2827
{
2928
private $oauthApi;
30-
private $adsApi;
3129
private $analyticsApi;
3230
private $bitsApi;
3331
private $channelPointsApi;
@@ -48,7 +46,6 @@ class NewTwitchApi
4846
public function __construct(Client $helixGuzzleClient, string $clientId, string $clientSecret, Client $authGuzzleClient = null)
4947
{
5048
$this->oauthApi = new OauthApi($clientId, $clientSecret, $authGuzzleClient);
51-
$this->adsApi = new AdsApi($helixGuzzleClient);
5249
$this->analyticsApi = new AnalyticsApi($helixGuzzleClient);
5350
$this->bitsApi = new BitsApi($helixGuzzleClient);
5451
$this->channelPointsApi = new ChannelPointsApi($helixGuzzleClient);
@@ -72,11 +69,6 @@ public function getOauthApi(): OauthApi
7269
return $this->oauthApi;
7370
}
7471

75-
public function getAdsApi(): AdsApi
76-
{
77-
return $this->adsApi;
78-
}
79-
8072
public function getAnalyticsApi(): AnalyticsApi
8173
{
8274
return $this->analyticsApi;

src/NewTwitchApi/Resources/AbstractResource.php

+40-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct(Client $guzzleClient)
2121
/**
2222
* @throws GuzzleException
2323
*/
24-
protected function callApi(string $uriEndpoint, string $bearer, array $queryParamsMap = [], array $bodyParams = []): ResponseInterface
24+
protected function getApi(string $uriEndpoint, string $bearer, array $queryParamsMap = [], array $bodyParams = []): ResponseInterface
2525
{
2626
return $this->sendToApi('GET', $uriEndpoint, $bearer, $queryParamsMap, $bodyParams);
2727
}
@@ -34,6 +34,14 @@ protected function deleteApi(string $uriEndpoint, string $bearer, array $queryPa
3434
return $this->sendToApi('DELETE', $uriEndpoint, $bearer, $queryParamsMap, $bodyParams);
3535
}
3636

37+
/**
38+
* @throws GuzzleException
39+
*/
40+
protected function patchApi(string $uriEndpoint, string $bearer, array $queryParamsMap = [], array $bodyParams = []): ResponseInterface
41+
{
42+
return $this->sendToApi('PATCH', $uriEndpoint, $bearer, $queryParamsMap, $bodyParams);
43+
}
44+
3745
/**
3846
* @throws GuzzleException
3947
*/
@@ -42,23 +50,35 @@ protected function postApi(string $uriEndpoint, string $bearer, array $queryPara
4250
return $this->sendToApi('POST', $uriEndpoint, $bearer, $queryParamsMap, $bodyParams);
4351
}
4452

53+
/**
54+
* @throws GuzzleException
55+
*/
56+
protected function putApi(string $uriEndpoint, string $bearer, array $queryParamsMap = [], array $bodyParams = []): ResponseInterface
57+
{
58+
return $this->sendToApi('PUT', $uriEndpoint, $bearer, $queryParamsMap, $bodyParams);
59+
}
60+
4561
private function sendToApi(string $httpMethod, string $uriEndpoint, string $bearer, array $queryParamsMap = [], array $bodyParams = []): ResponseInterface
4662
{
4763
if (count($bodyParams) > 0) {
4864
$request = new Request(
4965
$httpMethod,
50-
sprintf('%s%s',
51-
$uriEndpoint,
52-
$this->generateQueryParams($queryParamsMap)),
66+
sprintf(
67+
'%s%s',
68+
$uriEndpoint,
69+
$this->generateQueryParams($queryParamsMap)
70+
),
5371
['Authorization' => sprintf('Bearer %s', $bearer), 'Accept' => 'application/json'],
54-
json_encode($bodyParams)
72+
$this->generateBodyParams($bodyParams)
5573
);
5674
} else {
5775
$request = new Request(
5876
$httpMethod,
59-
sprintf('%s%s',
60-
$uriEndpoint,
61-
$this->generateQueryParams($queryParamsMap)),
77+
sprintf(
78+
'%s%s',
79+
$uriEndpoint,
80+
$this->generateQueryParams($queryParamsMap)
81+
),
6282
['Authorization' => sprintf('Bearer %s', $bearer)]
6383
);
6484
}
@@ -89,4 +109,16 @@ protected function generateQueryParams(array $queryParamsMap): string
89109

90110
return $queryStringParams ? '?'.substr($queryStringParams, 1) : '';
91111
}
112+
113+
protected function generateBodyParams(array $bodyParamsMap): string
114+
{
115+
$bodyParams = [];
116+
foreach ($bodyParamsMap as $bodyParam) {
117+
if ($bodyParam['value'] !== null) {
118+
$bodyParams[$bodyParam['key']] = $bodyParam['value'];
119+
}
120+
}
121+
122+
return json_encode($bodyParams);
123+
}
92124
}

src/NewTwitchApi/Resources/AdsApi.php

-26
This file was deleted.

src/NewTwitchApi/Resources/AnalyticsApi.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function getExtensionAnalytics(string $bearer, string $extensionId = null
4141
$queryParamsMap[] = ['key' => 'ended_at', 'value' => $endedAt];
4242
}
4343

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

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

79-
return $this->callApi('analytics/games', $bearer, $queryParamsMap);
79+
return $this->getApi('analytics/games', $bearer, $queryParamsMap);
8080
}
8181
}

src/NewTwitchApi/Resources/BitsApi.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function getCheermotes(string $bearer, string $broadcasterId = null): Res
2121
$queryParamsMap[] = ['key' => 'broadcaster_id', 'value' => $broadcasterId];
2222
}
2323

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

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

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

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

76-
return $this->callApi('extensions/transactions', $bearer, $queryParamsMap);
76+
return $this->getApi('extensions/transactions', $bearer, $queryParamsMap);
7777
}
7878
}

src/NewTwitchApi/Resources/ChannelPointsApi.php

+7-44
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function getCustomReward(string $bearer, string $broadcasterId, array $id
3535
$queryParamsMap[] = ['key' => 'only_manageable_rewards', 'value' => $onlyManageableRewards];
3636
}
3737

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

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

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

7878
/**
7979
* @throws GuzzleException
80-
* @link https://dev.twitch.tv/docs/api/reference#get-bits-leaderboard
80+
* @link https://dev.twitch.tv/docs/api/reference#delete-custom-reward
8181
*/
82-
public function getBitsLeaderboard(string $bearer, int $count = null, string $period = null, string $startedAt = null, string $userId = null): ResponseInterface
82+
public function deleteCustomReward(string $bearer, string $broadcasterId, string $id): ResponseInterface
8383
{
8484
$queryParamsMap = [];
8585

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-
}
86+
$queryParamsMap[] = ['key' => 'broadcaster_id', 'value' => $broadcasterId];
12287

123-
if ($after) {
124-
$queryParamsMap[] = ['key' => 'after', 'value' => $after];
125-
}
88+
$queryParamsMap[] = ['key' => 'id', 'value' => $id];
12689

127-
return $this->callApi('extensions/transactions', $bearer, $queryParamsMap);
90+
return $this->deleteApi('channel_points/custom_rewards', $bearer, $queryParamsMap);
12891
}
12992
}

0 commit comments

Comments
 (0)