mirror of
https://github.com/wallabag/wallabag.git
synced 2025-03-13 22:52:39 +00:00
Migrate from Guzzle to Symfony HttpClient - 4
This commit is contained in:
parent
f42d530491
commit
11c273fe83
5 changed files with 90 additions and 95 deletions
|
@ -32,7 +32,6 @@ class AppKernel extends Kernel
|
|||
new BabDev\PagerfantaBundle\BabDevPagerfantaBundle(),
|
||||
new FOS\JsRoutingBundle\FOSJsRoutingBundle(),
|
||||
new OldSound\RabbitMqBundle\OldSoundRabbitMqBundle(),
|
||||
new Http\HttplugBundle\HttplugBundle(),
|
||||
new Sentry\SentryBundle\SentryBundle(),
|
||||
new Twig\Extra\TwigExtraBundle\TwigExtraBundle(),
|
||||
];
|
||||
|
|
|
@ -453,17 +453,6 @@ sensio_framework_extra:
|
|||
router:
|
||||
annotations: false
|
||||
|
||||
httplug:
|
||||
clients:
|
||||
wallabag:
|
||||
factory: Wallabag\Helper\HttpClientFactory
|
||||
config:
|
||||
defaults:
|
||||
timeout: 10
|
||||
plugins: ['httplug.plugin.logger']
|
||||
discovery:
|
||||
client: false
|
||||
|
||||
# define custom entity so we can override length attribute to fix utf8mb4 issue
|
||||
craue_config:
|
||||
entity_name: Wallabag\Entity\InternalSetting
|
||||
|
|
|
@ -187,11 +187,17 @@ services:
|
|||
tags:
|
||||
- { name: doctrine.event_subscriber }
|
||||
|
||||
psr18.wallabag.client:
|
||||
class: Symfony\Component\HttpClient\Psr18Client
|
||||
arguments:
|
||||
$client: '@Wallabag\HttpClient\WallabagClient'
|
||||
|
||||
Graby\Graby:
|
||||
arguments:
|
||||
$config:
|
||||
error_message: '%wallabag.fetching_error_message%'
|
||||
error_message_title: '%wallabag.fetching_error_message_title%'
|
||||
$client: '@psr18.wallabag.client'
|
||||
calls:
|
||||
- [ setLogger, [ "@logger" ] ]
|
||||
tags:
|
||||
|
@ -201,9 +207,6 @@ services:
|
|||
arguments:
|
||||
$config: {}
|
||||
|
||||
wallabag.http_client:
|
||||
alias: 'httplug.client.wallabag'
|
||||
|
||||
Wallabag\SiteConfig\GrabySiteConfigBuilder:
|
||||
tags:
|
||||
- { name: monolog.logger, channel: graby }
|
||||
|
@ -212,16 +215,10 @@ services:
|
|||
Wallabag\SiteConfig\SiteConfigBuilder:
|
||||
alias: Wallabag\SiteConfig\GrabySiteConfigBuilder
|
||||
|
||||
GuzzleHttp\Cookie\CookieJar: ~
|
||||
|
||||
Symfony\Component\BrowserKit\HttpBrowser:
|
||||
arguments:
|
||||
$client: '@browser.client'
|
||||
|
||||
Wallabag\Helper\HttpClientFactory:
|
||||
calls:
|
||||
- ['addSubscriber', ['@Wallabag\HttpClient\Authenticator']]
|
||||
|
||||
RulerZ\RulerZ:
|
||||
alias: rulerz
|
||||
|
||||
|
|
|
@ -1,74 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Wallabag\Helper;
|
||||
|
||||
use GuzzleHttp\Client as GuzzleClient;
|
||||
use GuzzleHttp\Cookie\CookieJar;
|
||||
use GuzzleHttp\Event\SubscriberInterface;
|
||||
use Http\Adapter\Guzzle5\Client as GuzzleAdapter;
|
||||
use Http\Client\HttpClient;
|
||||
use Http\HttplugBundle\ClientFactory\ClientFactory;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Builds and configures the HTTP client.
|
||||
*/
|
||||
class HttpClientFactory implements ClientFactory
|
||||
{
|
||||
/** @var SubscriberInterface[] */
|
||||
private $subscribers = [];
|
||||
|
||||
/** @var CookieJar */
|
||||
private $cookieJar;
|
||||
|
||||
private $restrictedAccess;
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* HttpClientFactory constructor.
|
||||
*
|
||||
* @param string $restrictedAccess This param is a kind of boolean. Values: 0 or 1
|
||||
*/
|
||||
public function __construct(CookieJar $cookieJar, $restrictedAccess, LoggerInterface $logger)
|
||||
{
|
||||
$this->cookieJar = $cookieJar;
|
||||
$this->restrictedAccess = $restrictedAccess;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a subscriber to the HTTP client.
|
||||
*/
|
||||
public function addSubscriber(SubscriberInterface $subscriber)
|
||||
{
|
||||
$this->subscribers[] = $subscriber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Input an array of configuration to be able to create a HttpClient.
|
||||
*
|
||||
* @return HttpClient
|
||||
*/
|
||||
public function createClient(array $config = [])
|
||||
{
|
||||
$this->logger->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess]);
|
||||
|
||||
if (0 === (int) $this->restrictedAccess) {
|
||||
return new GuzzleAdapter(new GuzzleClient($config));
|
||||
}
|
||||
|
||||
// we clear the cookie to avoid websites who use cookies for analytics
|
||||
$this->cookieJar->clear();
|
||||
if (!isset($config['defaults']['cookies'])) {
|
||||
// need to set the (shared) cookie jar
|
||||
$config['defaults']['cookies'] = $this->cookieJar;
|
||||
}
|
||||
|
||||
$guzzle = new GuzzleClient($config);
|
||||
foreach ($this->subscribers as $subscriber) {
|
||||
$guzzle->getEmitter()->attach($subscriber);
|
||||
}
|
||||
|
||||
return new GuzzleAdapter($guzzle);
|
||||
}
|
||||
}
|
84
src/HttpClient/WallabagClient.php
Normal file
84
src/HttpClient/WallabagClient.php
Normal file
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
|
||||
namespace Wallabag\HttpClient;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\BrowserKit\HttpBrowser;
|
||||
use Symfony\Component\HttpClient\HttpClient;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
|
||||
|
||||
class WallabagClient implements HttpClientInterface
|
||||
{
|
||||
private $restrictedAccess;
|
||||
private HttpClientInterface $httpClient;
|
||||
private HttpBrowser $browser;
|
||||
private Authenticator $authenticator;
|
||||
private LoggerInterface $logger;
|
||||
|
||||
public function __construct($restrictedAccess, HttpBrowser $browser, Authenticator $authenticator, LoggerInterface $logger)
|
||||
{
|
||||
$this->restrictedAccess = $restrictedAccess;
|
||||
$this->browser = $browser;
|
||||
$this->authenticator = $authenticator;
|
||||
$this->logger = $logger;
|
||||
|
||||
$this->httpClient = HttpClient::create([
|
||||
'timeout' => 10,
|
||||
]);
|
||||
}
|
||||
|
||||
public function request(string $method, string $url, array $options = []): ResponseInterface
|
||||
{
|
||||
$this->logger->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess]);
|
||||
|
||||
if (0 === (int) $this->restrictedAccess) {
|
||||
return $this->httpClient->request($method, $url, $options);
|
||||
}
|
||||
|
||||
$login = $this->authenticator->loginIfRequired($url);
|
||||
|
||||
if (!$login) {
|
||||
return $this->httpClient->request($method, $url, $options);
|
||||
}
|
||||
|
||||
if (null !== $cookieHeader = $this->getCookieHeader($url)) {
|
||||
$options['headers']['cookie'] = $cookieHeader;
|
||||
}
|
||||
|
||||
$response = $this->httpClient->request($method, $url, $options);
|
||||
|
||||
$login = $this->authenticator->loginIfRequested($response);
|
||||
|
||||
if (!$login) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
if (null !== $cookieHeader = $this->getCookieHeader($url)) {
|
||||
$options['headers']['cookie'] = $cookieHeader;
|
||||
}
|
||||
|
||||
return $this->httpClient->request($method, $url, $options);
|
||||
}
|
||||
|
||||
public function stream($responses, ?float $timeout = null): ResponseStreamInterface
|
||||
{
|
||||
return $this->httpClient->stream($responses, $timeout);
|
||||
}
|
||||
|
||||
private function getCookieHeader(string $url): ?string
|
||||
{
|
||||
$cookies = [];
|
||||
|
||||
foreach ($this->browser->getCookieJar()->allRawValues($url) as $name => $value) {
|
||||
$cookies[] = $name . '=' . $value;
|
||||
}
|
||||
|
||||
if ($cookies === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return implode('; ', $cookies);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue