Skip to content

Commit

Permalink
Added bearer token support for unsubscribe methods #7 (#80)
Browse files Browse the repository at this point in the history
* Added bearer token support for unsubscribe methods

* Added bearer token support for unsubscribe methods

* Added bearer token support for unsubscribe methods

* Fixed optional bearer token

* Removed is null check

* Removed all optional , removed without_bearer WebhookSubscriptionApiSpec method

Co-authored-by: Brandon Begle <[email protected]>
  • Loading branch information
brandonjbegle and Brandon Begle authored Sep 21, 2020
1 parent 2691203 commit de953bf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
21 changes: 6 additions & 15 deletions spec/NewTwitchApi/Webhooks/WebhooksSubscriptionApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,51 +80,42 @@ function it_subscribes_to_user_follows(Client $guzzleClient)
$this->subscribeToUserFollows('12345', '54321', 1, 'https://redirect.url', 'bearer-token', 100);
}

function it_subscribes_to_a_user_without_bearer(Client $guzzleClient)
{
$guzzleClient->post('webhooks/hub', [
'headers' => [
'Client-ID' => 'client-id',
],
'body' => '{"hub.callback":"https:\/\/redirect.url","hub.mode":"subscribe","hub.topic":"https:\/\/api.twitch.tv\/helix\/users\/follows?from_id=12345&to_id=54321&first=1","hub.lease_seconds":100,"hub.secret":"client-secret"}'
])->shouldBeCalled();

$this->subscribeToUserFollows('12345', '54321', 1, 'https://redirect.url', null, 100);
}

function it_unsubscribes_from_a_stream(Client $guzzleClient)
{
$guzzleClient->post('webhooks/hub', [
'headers' => [
'Authorization' => 'Bearer bearer-token',
'Client-ID' => 'client-id',
],
'body' => '{"hub.callback":"https:\/\/redirect.url","hub.mode":"unsubscribe","hub.topic":"https:\/\/api.twitch.tv\/helix\/streams?user_id=12345"}'
])->shouldBeCalled();

$this->unsubscribeFromStream('12345', 'https://redirect.url');
$this->unsubscribeFromStream('12345', 'https://redirect.url', 'bearer-token');
}

function it_unsubscribes_from_a_user(Client $guzzleClient)
{
$guzzleClient->post('webhooks/hub', [
'headers' => [
'Authorization' => 'Bearer bearer-token',
'Client-ID' => 'client-id',
],
'body' => '{"hub.callback":"https:\/\/redirect.url","hub.mode":"unsubscribe","hub.topic":"https:\/\/api.twitch.tv\/helix\/users?id=12345"}'
])->shouldBeCalled();

$this->unsubscribeFromUser('12345', 'https://redirect.url');
$this->unsubscribeFromUser('12345', 'https://redirect.url', 'bearer-token');
}

function it_unsubscribes_from_user_follows(Client $guzzleClient)
{
$guzzleClient->post('webhooks/hub', [
'headers' => [
'Authorization' => 'Bearer bearer-token',
'Client-ID' => 'client-id',
],
'body' => '{"hub.callback":"https:\/\/redirect.url","hub.mode":"unsubscribe","hub.topic":"https:\/\/api.twitch.tv\/helix\/users\/follows?from_id=12345&to_id=54321&first=1"}'
])->shouldBeCalled();

$this->unsubscribeFromUserFollows('12345', '54321', 1, 'https://redirect.url');
$this->unsubscribeFromUserFollows('12345', '54321', 1, 'https://redirect.url', 'bearer-token');
}
}
32 changes: 18 additions & 14 deletions src/NewTwitchApi/Webhooks/WebhooksSubscriptionApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(string $clientId, string $secret, Client $guzzleClie
$this->guzzleClient = $guzzleClient ?? new HelixGuzzleClient($clientId);
}

public function subscribeToStream(string $twitchId, string $callback, string $bearer = null, int $leaseSeconds = 0): void
public function subscribeToStream(string $twitchId, string $callback, string $bearer, int $leaseSeconds = 0): void
{
$this->subscribe(
sprintf('https://api.twitch.tv/helix/streams?user_id=%s', $twitchId),
Expand All @@ -43,7 +43,7 @@ public function subscribeToSubscriptionEvents(string $twitchId, string $callback
);
}

public function subscribeToUser(string $twitchId, string $callback, string $bearer = null, int $leaseSeconds = 0): void
public function subscribeToUser(string $twitchId, string $callback, string $bearer, int $leaseSeconds = 0): void
{
$this->subscribe(
sprintf('https://api.twitch.tv/helix/users?id=%s', $twitchId),
Expand All @@ -53,7 +53,7 @@ public function subscribeToUser(string $twitchId, string $callback, string $bear
);
}

public function subscribeToUserFollows(string $followerId, string $followedUserId, int $first, string $callback, string $bearer = null, int $leaseSeconds = 0): void
public function subscribeToUserFollows(string $followerId, string $followedUserId, int $first, string $callback, string $bearer, int $leaseSeconds = 0): void
{
$queryParams = [];
if ($followerId) {
Expand All @@ -73,23 +73,25 @@ public function subscribeToUserFollows(string $followerId, string $followedUserI
);
}

public function unsubscribeFromStream(string $twitchId, string $callback): void
public function unsubscribeFromStream(string $twitchId, string $callback, string $bearer): void
{
$this->unsubscribe(
sprintf('https://api.twitch.tv/helix/streams?user_id=%s', $twitchId),
$callback
$callback,
$bearer
);
}

public function unsubscribeFromUser(string $twitchId, string $callback)
public function unsubscribeFromUser(string $twitchId, string $callback, string $bearer)
{
$this->unsubscribe(
sprintf('https://api.twitch.tv/helix/users?id=%s', $twitchId),
$callback
$callback,
$bearer
);
}

public function unsubscribeFromUserFollows(string $followerId, string $followedUserId, int $first, string $callback)
public function unsubscribeFromUserFollows(string $followerId, string $followedUserId, int $first, string $callback, string $bearer)
{
$queryParams = [];
if ($followerId) {
Expand All @@ -103,7 +105,8 @@ public function unsubscribeFromUserFollows(string $followerId, string $followedU
}
$this->unsubscribe(
sprintf('https://api.twitch.tv/helix/users/follows?%s', http_build_query($queryParams)),
$callback
$callback,
$bearer
);
}

Expand All @@ -115,14 +118,13 @@ public function validateWebhookEventCallback(string $xHubSignature, string $cont
return $expectedHash === $generatedHash;
}

private function subscribe(string $topic, string $callback, string $bearer = null, int $leaseSeconds = 0): void
private function subscribe(string $topic, string $callback, string $bearer, int $leaseSeconds = 0): void
{
$headers = [
'Client-ID' => $this->clientId,
];
if (!is_null($bearer)) {
$headers['Authorization'] = sprintf('Bearer %s', $bearer);
}

$headers['Authorization'] = sprintf('Bearer %s', $bearer);

$body = [
'hub.callback' => $callback,
Expand All @@ -138,12 +140,14 @@ private function subscribe(string $topic, string $callback, string $bearer = nul
]);
}

private function unsubscribe(string $topic, string $callback): void
private function unsubscribe(string $topic, string $callback, string $bearer): void
{
$headers = [
'Client-ID' => $this->clientId,
];

$headers['Authorization'] = sprintf('Bearer %s', $bearer);

$body = [
'hub.callback' => $callback,
'hub.mode' => self::UNSUBSCRIBE,
Expand Down

0 comments on commit de953bf

Please sign in to comment.