diff --git a/src/Contract/Http/SyncClient.php b/src/Contract/Http/SyncClient.php index 3496026..29d7f80 100644 --- a/src/Contract/Http/SyncClient.php +++ b/src/Contract/Http/SyncClient.php @@ -5,6 +5,7 @@ namespace Atymic\Twitter\Contract\Http; use Atymic\Twitter\Exception\ClientException; +use Psr\Http\Message\ResponseInterface; interface SyncClient extends Client { @@ -13,4 +14,9 @@ interface SyncClient extends Client * @throws ClientException */ public function request(string $method, string $url, array $data = []); + + /** + * @return ResponseInterface|null + */ + public function getLastResponse(); } diff --git a/src/Contract/Querier.php b/src/Contract/Querier.php index 29f40e2..3e39ec7 100644 --- a/src/Contract/Querier.php +++ b/src/Contract/Querier.php @@ -6,6 +6,7 @@ use Atymic\Twitter\Contract\Http\AsyncClient; use Atymic\Twitter\Contract\Http\Client as HttpClient; +use Atymic\Twitter\Contract\Http\SyncClient; use Atymic\Twitter\Exception\ClientException as TwitterClientException; use GuzzleHttp\RequestOptions; use InvalidArgumentException; @@ -105,4 +106,8 @@ public function delete(string $endpoint, array $parameters = []); public function getStream(string $endpoint, callable $onData, array $parameters = []): void; public function getConfiguration(): Configuration; + + public function getSyncClient(): SyncClient; + + public function getAsyncClient(): AsyncClient; } diff --git a/src/Http/Client.php b/src/Http/Client.php index 9bc9dd2..e5a6606 100644 --- a/src/Http/Client.php +++ b/src/Http/Client.php @@ -12,6 +12,7 @@ use Atymic\Twitter\Exception\Request\RateLimitedException; use Atymic\Twitter\Exception\Request\UnauthorizedRequestException; use GuzzleHttp\Psr7\Response; +use Psr\Http\Message\ResponseInterface; use Psr\Log\InvalidArgumentException as InvalidLogArgumentException; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; @@ -21,6 +22,7 @@ abstract class Client implements ClientContract { protected bool $debug; protected ?LoggerInterface $logger = null; + protected ?ResponseInterface $response = null; public function __construct(bool $debug, ?LoggerInterface $logger) { @@ -61,6 +63,7 @@ final protected function deduceClientException(Throwable $exception): TwitterCli { /** @var null|Response $response */ $response = method_exists($exception, 'getResponse') ? $exception->getResponse() : null; + $this->response = $response; $responseCode = $response !== null ? $response->getStatusCode() : null; switch ($responseCode) { diff --git a/src/Http/Client/SyncClient.php b/src/Http/Client/SyncClient.php index 0cbacd6..edac832 100644 --- a/src/Http/Client/SyncClient.php +++ b/src/Http/Client/SyncClient.php @@ -46,13 +46,23 @@ public function request(string $method, string $url, array $data = []) ); $requestOptions = $this->getRequestOptions($method, $data, $requestFormat); + $response = $this->client->request($method, $url, $requestOptions); + $this->response = $response; - return $this->formatResponse($this->client->request($method, $url, $requestOptions), $responseFormat); + return $this->formatResponse($response, $responseFormat); } catch (Throwable $exception) { throw $this->deduceClientException($exception); } } + /** + * @return ResponseInterface|null + */ + public function getLastResponse(): ?ResponseInterface + { + return $this->response; + } + private function getRequestOptions(string $requestMethod, array $params, ?string $requestFormat): array { switch ($requestFormat) { diff --git a/src/Service/Querier.php b/src/Service/Querier.php index 46bfa19..259d796 100644 --- a/src/Service/Querier.php +++ b/src/Service/Querier.php @@ -51,6 +51,22 @@ public function getConfiguration(): Configuration return $this->config; } + /** + * @codeCoverageIgnore + */ + public function getSyncClient(): SyncClient + { + return $this->syncClient; + } + + /** + * @codeCoverageIgnore + */ + public function getAsyncClient(): AsyncClient + { + return $this->asyncClient; + } + /** * @codeCoverageIgnore * @throws InvalidArgumentException diff --git a/tests/Unit/Http/Client/SyncClientTest.php b/tests/Unit/Http/Client/SyncClientTest.php index c143406..7c809d8 100644 --- a/tests/Unit/Http/Client/SyncClientTest.php +++ b/tests/Unit/Http/Client/SyncClientTest.php @@ -80,6 +80,7 @@ public function testRequest(): void self::assertInstanceOf(stdClass::class, $result); self::assertSame('bar', $result->foo); + self::assertInstanceOf(ResponseInterface::class, $this->subject->getLastResponse()); } /**