ogcore/src/Controller/OgBoot/AbstractOgBootController.php

124 lines
5.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Controller\OgBoot;
use App\Controller\OgBoot\PxeBootFile\PostAction;
use App\Service\Trace\CreateService;
use App\Service\Utils\ExtractOgLiveFilenameDateService;
use App\Service\Utils\SimplifyOgLiveFilenameService;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Exception\HttpException;
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\HttpClientInterface;
#[AsController]
abstract class AbstractOgBootController extends AbstractController
{
public function __construct(
#[Autowire(env: 'OG_BOOT_IP')]
protected string $ogBootIp,
#[Autowire(env: 'OG_BOOT_PXE_PORT')]
protected string $ogBootPxePort,
#[Autowire(env: 'OG_BOOT_API_PORT')]
protected string $ogBootApiPort,
#[Autowire(env: 'OG_CORE_IP')]
protected string $ogCoreIP,
#[Autowire(env: 'OG_LOG_IP')]
protected string $ogLogIp,
protected readonly EntityManagerInterface $entityManager,
protected readonly HttpClientInterface $httpClient,
protected readonly CreateService $createService,
protected readonly SimplifyOgLiveFilenameService $simplifyOgLiveFilenameService,
protected readonly ExtractOgLiveFilenameDateService $extractOgLiveFilenameDateService,
#[Autowire(env: 'OGCLIENT_API_KEY')]
protected string $ogClientApiKey,
protected readonly LoggerInterface $logger,
)
{
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function createRequest (string $method, string $url, array $params = []): JsonResponse|array
{
$params = array_merge($params, [
'headers' => [
'accept' => 'application/json',
'Content-Type' => 'application/json'
],
]);
try {
$response = $this->httpClient->request($method, 'http://'.$this->ogBootIp.':'.$this->ogBootApiPort.$url, $params);
return json_decode($response->getContent(), true);
} catch (ClientExceptionInterface | ServerExceptionInterface $e) {
$response = $e->getResponse();
$statusCode = $response->getStatusCode();
$content = json_decode($response->getContent(false), true);
$errorMessage = $content['error'] ?? $content['message'] ?? $e->getMessage();
if (isset($content['details'])) {
$errorMessage .= ' - ' . (is_array($content['details']) ? json_encode($content['details']) : $content['details']);
}
$this->logger->error(sprintf('Client/Server error in request to %s: %s', $url, $errorMessage));
throw new HttpException($statusCode, "External API Error ($statusCode): $errorMessage");
} catch (TransportExceptionInterface $e) {
$this->logger->error(sprintf('Transport error in request to %s: %s', $url, $e->getMessage()));
throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR, "Transport Error: " . $e->getMessage());
}
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function createRequestFromBootServer (\App\Entity\BootServer $bootServer, string $method, string $url, array $params = []): JsonResponse|array
{
$params = array_merge($params, [
'headers' => [
'accept' => 'application/json',
'Content-Type' => 'application/json'
],
]);
try {
$response = $this->httpClient->request($method, 'http://'.$bootServer->getIp().':'.$bootServer->getApiPort().$url, $params);
return json_decode($response->getContent(), true);
} catch (ClientExceptionInterface | ServerExceptionInterface $e) {
$response = $e->getResponse();
$statusCode = $response->getStatusCode();
$content = json_decode($response->getContent(false), true);
$errorMessage = $content['error'] ?? $content['message'] ?? $e->getMessage();
if (isset($content['details']) && $content['details'] !== '') {
$errorMessage .= ' - ' . (is_array($content['details']) ? json_encode($content['details']) : $content['details']);
}
$this->logger->error(sprintf('Client/Server error in request to %s: %s', $url, $errorMessage));
throw new HttpException($statusCode, "External API Error ($statusCode): $errorMessage");
} catch (TransportExceptionInterface $e) {
$this->logger->error(sprintf('Transport error in request to %s: %s', $url, $e->getMessage()));
throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR, "Transport Error: " . $e->getMessage());
}
}
}