[CORE][HTTPClient] Add some shortcut functions inspired by pre-v3
This commit is contained in:
parent
3227e1f919
commit
3cdaf6671a
|
@ -21,6 +21,9 @@ declare(strict_types = 1);
|
||||||
|
|
||||||
namespace App\Core;
|
namespace App\Core;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Symfony\Component\HttpClient\Exception\ClientException as HTTPClientException;
|
||||||
|
use Symfony\Component\HttpClient\Exception\TransportException;
|
||||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||||
|
|
||||||
|
@ -46,6 +49,28 @@ abstract class HTTPClient
|
||||||
self::$client = $client;
|
self::$client = $client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function statusCodeIsOkay(int|ResponseInterface $status): bool
|
||||||
|
{
|
||||||
|
if (!\is_int($status)) {
|
||||||
|
$status = $status->getStatusCode();
|
||||||
|
}
|
||||||
|
return $status >= 200 && $status < 300;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getEffectiveUrl(ResponseInterface $head): string
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
// This must come before getInfo given that Symfony HTTPClient is lazy (thus forcing curl exec)
|
||||||
|
$head->getHeaders();
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
|
} catch (HTTPClientException|TransportException $e) {
|
||||||
|
throw new InvalidArgumentException(previous: $e);
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
}
|
||||||
|
// The last effective url (after getHeaders, so it follows redirects)
|
||||||
|
return $head->getInfo('url');
|
||||||
|
}
|
||||||
|
|
||||||
public static function __callStatic(string $name, array $args)
|
public static function __callStatic(string $name, array $args)
|
||||||
{
|
{
|
||||||
if (\in_array(mb_strtoupper($name), ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'])) {
|
if (\in_array(mb_strtoupper($name), ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'])) {
|
||||||
|
|
Loading…
Reference in New Issue