Skip to content

Commit

Permalink
Merge pull request #39 from padhie/multiple_stream_search
Browse files Browse the repository at this point in the history
implement multiple stream searches and error response for twitch api …
  • Loading branch information
padhie authored Jan 5, 2023
2 parents a3b2fe4 + 73e330f commit 84ba4a6
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "padhie/twitch-api-bundle",
"version": "2.0.0",
"version": "2.0.1",
"type": "library",
"keywords": [
"twitch",
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ services:
volumes:
- .:/application
working_dir: "/application"
environment:
PHP_EXTENSION_XDEBUG: 1
PHP_INI_XDEBUG__REMOTE_AUTOSTART: 1
19 changes: 16 additions & 3 deletions src/Request/Streams/GetStreamsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ final class GetStreamsRequest implements PaginationRequestInterface
private ?string $gameId = null;
private ?string $language = null;
private ?string $userId = null;
private ?string $userLogin = null;
/** @var array<string> */
private array $userLogins = [];

public function getMethod(): string
{
Expand All @@ -38,14 +39,26 @@ public function getHeader(): array

public function getParameter(): array
{
$userLoginString = '';
if (count($this->userLogins) > 1) {
$userLoginString = $this->userLogins[0];
if (count($this->userLogins) > 2) {
$userLogins = $this->userLogins;
array_shift($userLogins);
$userLoginString .= implode('&', array_map(static function (string $item) {
return 'userLogin=' . $item;
}, $userLogins));
}
}

return [
'after' => $this->after,
'before' => $this->before,
'first' => $this->first,
'game_id' => $this->gameId,
'language' => $this->language,
'user_id' => $this->userId,
'user_login' => $this->userLogin,
'user_login' => $userLoginString,
];
}

Expand Down Expand Up @@ -110,7 +123,7 @@ public function withUserId(string $userId): self
public function withUserLogin(string $userLogin): self
{
$self = clone $this;
$self->userLogin = $userLogin;
$self->userLogins[] = $userLogin;

return $self;
}
Expand Down
32 changes: 32 additions & 0 deletions src/Response/ErrorResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Padhie\TwitchApiBundle\Response;

final class ErrorResponse implements ResponseInterface
{
private string $error;
private int $status;
private string $message;

public static function createFromArray(array $data): self
{
$self = new self();

$self->error = $data['error'];
$self->status = $data['status'];
$self->message = $data['message'];

return $self;
}

public function jsonSerialize(): array
{
return [
'error' => $this->error,
'status' => $this->status,
'message' => $this->message,
];
}
}
36 changes: 36 additions & 0 deletions src/Response/ResponseGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Padhie\TwitchApiBundle\Response;

use GuzzleHttp\Exception\ClientException;
use Padhie\TwitchApiBundle\Request\RequestInterface;

use function call_user_func;
Expand All @@ -16,9 +17,35 @@ public function generateFromString(RequestInterface $request, string $response):
$response = $response !== '' ? $response : '{}';
$jsonResponse = json_decode($response, true, 512, JSON_THROW_ON_ERROR);

if ($this->isErrorResponse($jsonResponse)) {
return ErrorResponse::createFromArray($jsonResponse);
}

return $this->generateFromArray($request, $jsonResponse);
}

public function generateErrorResponseFromException(\Throwable $exception): ErrorResponse
{
if ($exception instanceof ClientException) {
$message = $exception->getMessage();
$posOfResponse = strpos($message, 'response:') + 9;

if ($posOfResponse >= 9) {
$response = substr($message, $posOfResponse);
$jsonResponse = json_decode($response, true, 512, JSON_THROW_ON_ERROR);

return ErrorResponse::createFromArray($jsonResponse);
}
}

return ErrorResponse::createFromArray([
'error' => get_class($exception),
'status' => $exception->getCode(),
'message' => $exception->getMessage(),
]);
}


/**
* @param array<mixed> $response
*/
Expand All @@ -31,4 +58,13 @@ public function generateFromArray(RequestInterface $request, array $response): R
$response
);
}

/**
* @param array<mixed> $response
*/
private function isErrorResponse(array $response): bool
{
return array_key_exists('error', $response)
&& !empty($response['error']);
}
}
22 changes: 18 additions & 4 deletions src/TwitchClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Padhie\TwitchApiBundle;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Promise\Utils;
use JsonException;
use Padhie\TwitchApiBundle\Exception\InvalidRequestException;
Expand Down Expand Up @@ -44,7 +45,12 @@ public function __construct(ClientInterface $client, RequestGenerator $requestGe
public function send(RequestInterface $request): ResponseInterface
{
$prsRequest = $this->requestGenerator->generate($request);
$response = $this->executeRequest($prsRequest);

try {
$response = $this->executeRequest($prsRequest);
} catch (ClientException $exception) {
return $this->responseGenerator->generateErrorResponseFromException($exception);
}

return $this->responseGenerator->generateFromString($request, $response);
}
Expand All @@ -60,7 +66,13 @@ public function sendWithPagination(PaginationRequestInterface $request): Respons
}

$prsRequest = $this->requestGenerator->generate($request);
$responseString = $this->executeRequest($prsRequest);

try {
$responseString = $this->executeRequest($prsRequest);
} catch (ClientException $exception) {
return $this->responseGenerator->generateErrorResponseFromException($exception);
}

$jsonResponse = json_decode($responseString, true, 512, JSON_THROW_ON_ERROR);

$dataCollection = array_merge($dataCollection, $jsonResponse['data'] ?? []);
Expand Down Expand Up @@ -101,8 +113,10 @@ function($response) use ($request, $key, &$responses): void {
$responseString = $this->loadBody($response);
$responses[$key] = $this->responseGenerator->generateFromString($request, $responseString);
},
function($response) use ($key, &$responses): void {
$responses[$key] = null;
function($response) use ($key, &$responses): void {
$responses[$key] = $response instanceof \Throwable
? $this->responseGenerator->generateErrorResponseFromException($response)
: null;
}
);

Expand Down

0 comments on commit 84ba4a6

Please sign in to comment.