Skip to content

Commit

Permalink
Merge pull request #91 from brandinarsenault/large-patch
Browse files Browse the repository at this point in the history
Add bodyParams and Function Updates
  • Loading branch information
echosa committed Dec 11, 2020
2 parents 9c29be0 + 8917e44 commit c3d8ef3
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 18 deletions.
6 changes: 6 additions & 0 deletions spec/NewTwitchApi/Resources/UsersApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,10 @@ function it_should_create_a_follow_without_notifications(Client $guzzleClient, R
$guzzleClient->send(new Request('POST', 'users/follows?from_id=123&to_id=321&allow_notifications=0', ['Authorization' => 'Bearer TEST_TOKEN']))->willReturn($response);
$this->createUserFollow('TEST_TOKEN', '123', '321', false)->shouldBeAnInstanceOf(ResponseInterface::class);
}

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);
}
}
46 changes: 33 additions & 13 deletions src/NewTwitchApi/Resources/AbstractResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,47 @@ public function __construct(Client $guzzleClient)
/**
* @throws GuzzleException
*/
protected function callApi(string $uriEndpoint, string $bearer, array $queryParamsMap = []): ResponseInterface
protected function callApi(string $uriEndpoint, string $bearer, array $queryParamsMap = [], array $bodyParams = []): ResponseInterface
{
$request = new Request(
'GET',
sprintf('%s%s', $uriEndpoint, $this->generateQueryParams($queryParamsMap)),
['Authorization' => sprintf('Bearer %s', $bearer)]
);
return $this->sendToApi('GET', $uriEndpoint, $bearer, $queryParamsMap, $bodyParams);
}

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

/**
* @throws GuzzleException
*/
protected function postApi(string $uriEndpoint, string $bearer, array $queryParamsMap = []): ResponseInterface
protected function postApi(string $uriEndpoint, string $bearer, array $queryParamsMap = [], array $bodyParams = []): ResponseInterface
{
$request = new Request(
'POST',
sprintf('%s%s', $uriEndpoint, $this->generateQueryParams($queryParamsMap)),
['Authorization' => sprintf('Bearer %s', $bearer)]
);
return $this->sendToApi('POST', $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)),
['Authorization' => sprintf('Bearer %s', $bearer), 'Accept' => 'application/json'],
json_encode($bodyParams)
);
} else {
$request = new Request(
$httpMethod,
sprintf('%s%s',
$uriEndpoint,
$this->generateQueryParams($queryParamsMap)),
['Authorization' => sprintf('Bearer %s', $bearer)]
);
}

return $this->guzzleClient->send($request);
}
Expand Down
8 changes: 4 additions & 4 deletions src/NewTwitchApi/Resources/AdsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class AdsApi extends AbstractResource
*/
public function startCommercial(string $bearer, string $broadcasterId, int $length): ResponseInterface
{
$queryParamsMap = [];
$bodyParamsMap = [];

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

$queryParamsMap[] = ['key' => 'length', 'value' => $length];
$bodyParamsMap[] = ['length' => $length];

return $this->postApi('channels/commercial', $bearer, $queryParamsMap);
return $this->postApi('channels/commercial', $bearer, [], $bodyParamsMap);
}
}
2 changes: 1 addition & 1 deletion src/NewTwitchApi/Resources/SubscriptionsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getBroadcasterSubscriptions(string $bearer, string $broadcasterI

/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference/#get-broadcaster-s-subscribers
* @link https://dev.twitch.tv/docs/api/reference/#get-broadcaster-subscriptions
*/
public function getBroadcasterSubscribers(string $bearer, string $broadcasterId, array $ids = []): ResponseInterface
{
Expand Down
15 changes: 15 additions & 0 deletions src/NewTwitchApi/Resources/UsersApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,19 @@ public function createUserFollow(string $bearer, string $fromId, string $toId, b

return $this->postApi('users/follows', $bearer, $queryParamsMap);
}

/**
* @throws GuzzleException
* @link https://dev.twitch.tv/docs/api/reference#delete-user-follows
*/
public function deleteUserFollow(string $bearer, string $fromId, string $toId): ResponseInterface
{
$queryParamsMap = [];

$queryParamsMap[] = ['key' => 'from_id', 'value' => $fromId];

$queryParamsMap[] = ['key' => 'to_id', 'value' => $toId];

return $this->deleteApi('users/follows', $bearer, $queryParamsMap);
}
}

0 comments on commit c3d8ef3

Please sign in to comment.