Skip to content

Commit

Permalink
Switch to async HTTP client internally
Browse files Browse the repository at this point in the history
It will still be converted to a dual async/sync one by `PluginClient`.
  • Loading branch information
jtojnar committed Apr 18, 2023
1 parent c974bf5 commit 64862a0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
"j0k3r/httplug-ssrf-plugin": "^3.0",
"j0k3r/php-readability": "^1.2.9",
"monolog/monolog": "^1.18.0|^2.3",
"php-http/async-client-implementation": "^1.0",
"php-http/client-common": "^2.5",
"php-http/discovery": "^1.14",
"php-http/httplug": "^2.2",
"php-http/message": "^1.12",
"psr/http-client-implementation": "^1.0",
"simplepie/simplepie": "^1.8",
"smalot/pdfparser": "^2.0",
"symfony/options-resolver": "^3.4|^4.4|^5.3|^6.0",
Expand Down
27 changes: 16 additions & 11 deletions maintenance/Rector/Helpers/RecordingHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,41 @@

namespace Maintenance\Graby\Rector\Helpers;

use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Http\Client\HttpAsyncClient;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class RecordingHttpClient implements ClientInterface
class RecordingHttpClient implements HttpAsyncClient
{
/**
* @var ResponseInterface[]
*/
private array $responses;

private ClientInterface $httpClient;
private HttpAsyncClient $httpClient;

public function __construct(ClientInterface $httpClient)
public function __construct(HttpAsyncClient $httpClient)
{
$this->httpClient = $httpClient;
}

/**
* Sends a PSR-7 request and returns a PSR-7 response.
* Sends a PSR-7 request in an asynchronous way.
*
* @throws ClientExceptionInterface if an error happens while processing the request
* Exceptions related to processing the request are available from the returned Promise.
*
* @throws \Exception If processing the request is impossible (eg. bad configuration).
*
* @return Promise resolves a PSR-7 Response or fails with an Http\Client\Exception
*/
public function sendRequest(RequestInterface $request): ResponseInterface
public function sendAsyncRequest(RequestInterface $request): Promise
{
$response = $this->httpClient->sendRequest($request);
$this->responses[] = $response;
return $this->httpClient->sendAsyncRequest($request)->then(function (ResponseInterface $response): ResponseInterface {
$this->responses[] = $response;

return $response;
return $response;
});
}

/**
Expand Down
4 changes: 2 additions & 2 deletions maintenance/Rector/MockGrabyResponseRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Graby\Graby;
use Graby\HttpClient\Plugin\CookiePlugin;
use Http\Client\Common\PluginClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\HttpAsyncClientDiscovery;
use Http\Message\CookieJar;
use Maintenance\Graby\Rector\Helpers\RecordingHttpClient;
use PhpParser\Comment;
Expand Down Expand Up @@ -259,7 +259,7 @@ private function fetchResponses(string $url, array $config): array
{
// Wrap the same HTTP client internally created by Graby class with a proxy
// that captures the returned responses.
$httpClient = new RecordingHttpClient(new PluginClient(HttpClientDiscovery::find(), [new CookiePlugin(new CookieJar())]));
$httpClient = new RecordingHttpClient(new PluginClient(HttpAsyncClientDiscovery::find(), [new CookiePlugin(new CookieJar())]));
$graby = new Graby($config, $httpClient);
$graby->fetchContent($url);

Expand Down
6 changes: 3 additions & 3 deletions src/Extractor/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
use Http\Client\Common\Plugin\RedirectPlugin;
use Http\Client\Common\PluginClient;
use Http\Client\Exception\TransferException;
use Http\Client\HttpAsyncClient as Client;
use Http\Discovery\Psr17FactoryDiscovery;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
Expand All @@ -32,9 +32,9 @@ class HttpClient
private ?ContentExtractor $extractor;

/**
* @param ClientInterface $client Http client
* @param Client $client Http client
*/
public function __construct(ClientInterface $client, array $config = [], LoggerInterface $logger = null, ContentExtractor $extractor = null)
public function __construct(Client $client, array $config = [], LoggerInterface $logger = null, ContentExtractor $extractor = null)
{
$this->config = new HttpClientConfig($config);

Expand Down
8 changes: 4 additions & 4 deletions src/Graby.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\Psr7\UriResolver;
use Http\Client\Common\PluginClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Client\HttpAsyncClient as Client;
use Http\Discovery\HttpAsyncClientDiscovery;
use Http\Message\CookieJar;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Http\Client\ClientInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Readability\Readability;
Expand All @@ -35,7 +35,7 @@ class Graby

private ?string $prefetchedContent = null;

public function __construct(array $config = [], ?ClientInterface $client = null, ConfigBuilder $configBuilder = null)
public function __construct(array $config = [], ?Client $client = null, ConfigBuilder $configBuilder = null)
{
$this->config = new GrabyConfig($config);

Expand Down Expand Up @@ -75,7 +75,7 @@ public function __construct(array $config = [], ?ClientInterface $client = null,
);

$this->httpClient = new HttpClient(
$client ?: new PluginClient(HttpClientDiscovery::find(), [new CookiePlugin(new CookieJar())]),
$client ?: new PluginClient(HttpAsyncClientDiscovery::find(), [new CookiePlugin(new CookieJar())]),
$this->config->getHttpClient(),
$this->logger,
$this->extractor
Expand Down

0 comments on commit 64862a0

Please sign in to comment.