Make AuthenticatorProvider use Symfony HTTP Client

This commit is contained in:
Yassine Guedidi
2024-12-23 00:02:59 +01:00
parent c2197bd020
commit 1e1d58da7f
5 changed files with 47 additions and 20 deletions

View File

@ -2,21 +2,18 @@
namespace Wallabag\ExpressionLanguage;
use GuzzleHttp\ClientInterface;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class AuthenticatorProvider implements ExpressionFunctionProviderInterface
{
/**
* @var ClientInterface
*/
private $guzzle;
private HttpClientInterface $requestHtmlFunctionClient;
public function __construct(ClientInterface $guzzle)
public function __construct(HttpClientInterface $requestHtmlFunctionClient)
{
$this->guzzle = $guzzle;
$this->requestHtmlFunctionClient = $requestHtmlFunctionClient;
}
public function getFunctions(): array
@ -38,7 +35,7 @@ class AuthenticatorProvider implements ExpressionFunctionProviderInterface
throw new \Exception('Not supported');
},
function (array $arguments, $uri) {
return $this->guzzle->get($uri, $options)->getBody();
return $this->requestHtmlFunctionClient->request('GET', $uri)->getContent();
}
);
}

View File

@ -10,6 +10,13 @@ use Wallabag\ExpressionLanguage\AuthenticatorProvider;
class LoginFormAuthenticator
{
private AuthenticatorProvider $authenticatorProvider;
public function __construct(AuthenticatorProvider $authenticatorProvider)
{
$this->authenticatorProvider = $authenticatorProvider;
}
/**
* Logs the configured user on the given Guzzle client.
*
@ -20,7 +27,7 @@ class LoginFormAuthenticator
$postFields = [
$siteConfig->getUsernameField() => $siteConfig->getUsername(),
$siteConfig->getPasswordField() => $siteConfig->getPassword(),
] + $this->getExtraFields($siteConfig, $guzzle);
] + $this->getExtraFields($siteConfig);
$guzzle->post(
$siteConfig->getLoginUri(),
@ -77,13 +84,13 @@ class LoginFormAuthenticator
*
* @return array
*/
private function getExtraFields(SiteConfig $siteConfig, ClientInterface $guzzle)
private function getExtraFields(SiteConfig $siteConfig)
{
$extraFields = [];
foreach ($siteConfig->getExtraFields() as $fieldName => $fieldValue) {
if ('@=' === substr($fieldValue, 0, 2)) {
$expressionLanguage = $this->getExpressionLanguage($guzzle);
$expressionLanguage = $this->getExpressionLanguage();
$fieldValue = $expressionLanguage->evaluate(
substr($fieldValue, 2),
[
@ -101,11 +108,11 @@ class LoginFormAuthenticator
/**
* @return ExpressionLanguage
*/
private function getExpressionLanguage(ClientInterface $guzzle)
private function getExpressionLanguage()
{
return new ExpressionLanguage(
null,
[new AuthenticatorProvider($guzzle)]
[$this->authenticatorProvider]
);
}
}