Skip to content

Commit

Permalink
Ability to get the last response (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
ejunker authored May 18, 2021
1 parent 738a2b5 commit 1ca2b9c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Contract/Http/SyncClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Atymic\Twitter\Contract\Http;

use Atymic\Twitter\Exception\ClientException;
use Psr\Http\Message\ResponseInterface;

interface SyncClient extends Client
{
Expand All @@ -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();
}
5 changes: 5 additions & 0 deletions src/Contract/Querier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 11 additions & 1 deletion src/Http/Client/SyncClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions src/Service/Querier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Http/Client/SyncClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down

0 comments on commit 1ca2b9c

Please sign in to comment.