Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Provide implementation for PSR-18 HTTP Client
The implementation of the PSR-18 ClientInterface is provided as an adapter to the existing GuzzleHTTP Client. Therefore existing configuraton settings will be reused. As our current Guzzle wrapper (RequestFactory->request) has support for passing custom guzzle per-request options, we do not deprecate this method but add the PSR-18 implementation as a more generic alternative. Once GuzzleHTTP supports PSR-18 natively we can (and will) drop our adapter and point to Guzzles native implementation in our dependency injection configuration. Therefore, this adapter is marked as internal and extensions are being instructed to depend on the PSR-18 interfaces only. composer require psr/http-client:^1.0 Releases: master Resolves: #89216 Change-Id: I0f2c81916a2f5e4b40abd6f0b146440ef155cf00 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/61567 Tested-by: TYPO3com <noreply@typo3.com> Tested-by: Benni Mack <benni@typo3.org> Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de> Reviewed-by: Benni Mack <benni@typo3.org> Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
- Loading branch information
Showing
12 changed files
with
577 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
namespace TYPO3\CMS\Core\Http; | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
use GuzzleHttp\ClientInterface as GuzzleClientInterface; | ||
use GuzzleHttp\Exception\ConnectException; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use GuzzleHttp\Exception\RequestException; | ||
use GuzzleHttp\RequestOptions; | ||
use Psr\Http\Client\ClientExceptionInterface; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Client\NetworkExceptionInterface; | ||
use Psr\Http\Client\RequestExceptionInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* PSR-18 adapter for Guzzle\ClientInterface | ||
* | ||
* Will be removed once GuzzleHTTP implements PSR-18. | ||
* | ||
* @internal | ||
*/ | ||
class Client implements ClientInterface | ||
{ | ||
/** | ||
* @var GuzzleClientInterface | ||
*/ | ||
private $guzzle; | ||
|
||
public function __construct(GuzzleClientInterface $guzzle) | ||
{ | ||
$this->guzzle = $guzzle; | ||
} | ||
|
||
/** | ||
* Sends a PSR-7 request and returns a PSR-7 response. | ||
* | ||
* @param RequestInterface $request | ||
* @return ResponseInterface | ||
* @throws ClientExceptionInterface If an error happens while processing the request. | ||
* @throws NetworkExceptionInterface If the request cannot be sent due to a network failure of any kind | ||
* @throws RequestExceptionInterface If the request message is not a well-formed HTTP request | ||
*/ | ||
public function sendRequest(RequestInterface $request): ResponseInterface | ||
{ | ||
try { | ||
return $this->guzzle->send($request, [ | ||
RequestOptions::HTTP_ERRORS => false, | ||
RequestOptions::ALLOW_REDIRECTS => false, | ||
]); | ||
} catch (ConnectException $e) { | ||
throw new Client\NetworkException($e->getMessage(), 1566909446, $e->getRequest(), $e); | ||
} catch (RequestException $e) { | ||
throw new Client\RequestException($e->getMessage(), 1566909447, $e->getRequest(), $e); | ||
} catch (GuzzleException $e) { | ||
throw new Client\ClientException($e->getMessage(), 1566909448, $e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace TYPO3\CMS\Core\Http\Client; | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
use GuzzleHttp\Exception\GuzzleException; | ||
use Psr\Http\Client\ClientExceptionInterface; | ||
use RuntimeException; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class ClientException extends RuntimeException implements ClientExceptionInterface, GuzzleException | ||
{ | ||
public function __construct(string $message, int $code, GuzzleException $previous) | ||
{ | ||
parent::__construct($message, $code, $previous); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
typo3/sysext/core/Classes/Http/Client/GuzzleClientFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
namespace TYPO3\CMS\Core\Http\Client; | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\ClientInterface; | ||
use GuzzleHttp\HandlerStack; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class GuzzleClientFactory | ||
{ | ||
/** | ||
* Creates the client to do requests | ||
* @return ClientInterface | ||
*/ | ||
public static function getClient(): ClientInterface | ||
{ | ||
$httpOptions = $GLOBALS['TYPO3_CONF_VARS']['HTTP']; | ||
$httpOptions['verify'] = filter_var($httpOptions['verify'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? $httpOptions['verify']; | ||
|
||
if (isset($GLOBALS['TYPO3_CONF_VARS']['HTTP']['handler']) && is_array($GLOBALS['TYPO3_CONF_VARS']['HTTP']['handler'])) { | ||
$stack = HandlerStack::create(); | ||
foreach ($GLOBALS['TYPO3_CONF_VARS']['HTTP']['handler'] ?? [] as $handler) { | ||
$stack->push($handler); | ||
} | ||
$httpOptions['handler'] = $stack; | ||
} | ||
|
||
return GeneralUtility::makeInstance(Client::class, $httpOptions); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
typo3/sysext/core/Classes/Http/Client/NetworkException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace TYPO3\CMS\Core\Http\Client; | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
use GuzzleHttp\Exception\ConnectException as GuzzleConnectException; | ||
use Psr\Http\Client\NetworkExceptionInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class NetworkException extends GuzzleConnectException implements NetworkExceptionInterface | ||
{ | ||
public function __construct( | ||
string $message, | ||
int $code, | ||
RequestInterface $request, | ||
GuzzleConnectException $previous | ||
) { | ||
parent::__construct($message, $request, $previous); | ||
$this->code = $code; | ||
} | ||
|
||
public function getRequest(): RequestInterface | ||
{ | ||
parent::getRequest(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
typo3/sysext/core/Classes/Http/Client/RequestException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace TYPO3\CMS\Core\Http\Client; | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
use GuzzleHttp\Exception\RequestException as GuzzleRequestException; | ||
use Psr\Http\Client\RequestExceptionInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class RequestException extends GuzzleRequestException implements RequestExceptionInterface | ||
{ | ||
public function __construct( | ||
string $message, | ||
int $code, | ||
RequestInterface $request, | ||
GuzzleRequestException $previous | ||
) { | ||
parent::__construct($message, $request, null, $previous); | ||
$this->code = $code; | ||
} | ||
|
||
public function getRequest(): RequestInterface | ||
{ | ||
parent::getRequest(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.