<?php
namespace Nellapp\Bundle\SDKBundle\ApiClientInterApp\Proxy;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
abstract class AbstractProxyClient implements ProxyClientInterface
{
/**
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function get(string $app, string $path, array $query = [], array $headers = []): Response
{
$response = $this->doGet($app, $path, $query, $headers);
if (401 === $response->getStatusCode()) {
throw new UnauthorizedHttpException('');
}
if (404 === $response->getStatusCode()) {
throw new NotFoundHttpException();
}
$statusCode = $response->getStatusCode();
if ($statusCode >= 500) {
throw new ServiceUnavailableHttpException();
}
$headers = array_filter($response->getHeaders(false), function (string $key) {
return in_array(strtolower($key), [
'content-type',
]);
}, ARRAY_FILTER_USE_KEY);
return new Response(
$response->getContent(),
$response->getStatusCode(),
$headers
);
}
abstract public function doGet(string $app, string $path, array $query = [], array $headers = []): ?ResponseInterface;
}